Artificial Intelligence is here probably for ever. It becomes more and more widespread and usual, like personal computers or smartphones. And we wonโt be able to imagine our lives without it very soon. To use it as a developer in your projects becomes incredibly easy too. I developed a simple tool using the OpenAI NPM package, and Iโm going to share my approach. Letโs go.
Idea
The main idea is pretty simple. I saw that many people on Twitter share their achievements, like MRR, subscribers numbers, or progress like lists, with emojis. For example:
๐งโโ๏ธ be fairy
๐ be gorgeous
๐ฆ be unique
๐ณ be a whale
๐ฆง be funny
I decided to create a tool that looks for appropriate emoji according to text provided.
Implementation
Letโs start from the backend. Iโm using Node.js so first of all I need to install OpenAI npm package. Then to use the package you need to obtain your API-key from open AI website.
When you finish all the preparation itโs time to get you hands dirty. Init OpenAI package:
const { Configuration, OpenAIApi } = require('openai');
// Replace YOUR_API_KEY with your actual OpenAI API key
const API_KEY = process.env.OPENAI_API_KEY;
// Replace MODEL_ID with the ID of the model you want to use
// we are gonna use the chat completion functions so the following model id
// works there properly. Check in the API documentation if a model you wanna try
// works with selected API functions.
const MODEL_ID = 'gpt-3.5-turbo';
const configuration = new Configuration({
apiKey: API_KEY,
});
const openai = new OpenAIApi(configuration);
When we initialised the OpenAI object, we can call the API then:
const completion = await openai.createChatCompletion({
model: MODEL_ID,
messages: [{ "role": "user", "content": `generate exactly 5 funny and playful emojis for each paragraph of the input following the meaning of them.
Return it as JSON with an array of non empty strings containing exactly one emoji arrays.
It shoudln't contain any additional keys in JSON. Only array of arrays.
Length of the array should be equal to ${count}.
No additional texts or explanations or extra array entities.
The input: "${input.trim()}"` }]
});
const output = completion.data.choices[0].message.content;
I tried to use Edits API first, but the result was not really predictable so I switched to ChatCompletion. Itโs not perfect, but responses much more suitable.
Our output should look like a stringified JSON, but sometimes the AI returns not really expected response. So we need to be sure that we have a plan B if it could not be parsed properly.
let outputArray = [];
try {
outputArray = JSON.parse(output
.replaceAll(' ', '')
.replaceAll('\n', '')
.replaceAll('],]', ']]')
.match(/\[(.|\n)*\]/g)[0] // get only array. There might be other info generated
.trim()
);
outputArray = outputArray.map(arr => {
if (arr.length) return arr;
return getNRandomEmojis(5); // there might be an empty array
});
while (outputArray.length < input.length) {
outputArray.push(getNRandomEmojis(5)); // length of any array might be less then we expeced (5)
}
} catch (e) {
console.error(e);
const paragrapthsLength = input.length;
for (let i = 0; i < paragrapthsLength; i++) { // just generate random emojis as a backup
outputArray.push(getNRandomEmojis(5));
}
}
Now letโs summarise everything we did and combine it into one file. The full code of the service looks as below.
async generateResponse(input) {
// Replace YOUR_API_KEY with your actual OpenAI API key
const API_KEY = process.env.OPENAI_API_KEY;
// Replace MODEL_ID with the ID of the model you want to use
// we are gonna use the chat completion functions so the following model id
// works there properly. Check in the API documentation if a model you wanna try
// works with selected API functions.
const MODEL_ID = 'gpt-3.5-turbo';
const configuration = new Configuration({
apiKey: API_KEY,
});
const openai = new OpenAIApi(configuration);
const inputStringified = input.map(inp => inp.val.trim()).join('\n');
const completion = await openai.createChatCompletion({
model: MODEL_ID,
messages: [{ "role": "user", "content": getMessageContent(inputStringified, input.length) }]
});
const output = completion.data.choices[0].message.content;
let outputArray = [];
try {
outputArray = JSON.parse(output
.replaceAll(' ', '')
.replaceAll('\n', '')
.replaceAll('],]', ']]')
.match(/\[(.|\n)*\]/g)[0] // get only array. There might be other info generated
.trim()
);
outputArray = outputArray.map(arr => {
if (arr.length) return arr;
return getNRandomEmojis(5);
});
while (outputArray.length < input.length) {
outputArray.push(getNRandomEmojis(5));
}
} catch (e) {
console.error(e);
const paragrapthsLength = input.length;
for (let i = 0; i < paragrapthsLength; i++) {
outputArray.push(getNRandomEmojis(5));
}
}
return outputArray;
}
function getMessageContent(input, count) {
return `generate exactly 5 funny and playful emojis for each paragraph of the input following the meaning of them.
Return it as JSON with an array of non empty strings containing exactly one emoji arrays.
It shoudln't contain any additional keys in JSON. Only array of arrays.
Length of the array should be equal to ${count}.
No additional texts or explanations or extra array entities.
The input: "${input.trim()}"`;
}
function getNRandomEmojis(n) {
const res = [];
for (let i = 0; i < n; i++) {
res.push(getRandomEmoji());
}
return res;
}
function getRandomEmoji() {
const emojis = [
'๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ณ', '๐', '๐', '๐', '๐', '๐', '๐ฃ', '๐ข', '๐', '๐ญ', '๐ช', '๐ฅ', '๐ฐ', '๐
', '๐', '๐ฉ', '๐ซ', '๐จ', '๐ฑ', '๐ ', '๐ก', '๐ค', '๐', '๐', '๐', '๐ท', '๐', '๐ด', '๐ต', '๐ฒ', '๐', '๐ฆ', '๐ง', '๐', '๐ฟ', '๐ฎ', '๐ฌ', '๐', '๐', '๐ฏ', '๐ถ', '๐', '๐', '๐', '๐ฒ', '๐ณ', '๐ฎ', '๐ท', '๐', '๐ถ', '๐ฆ', '๐ง', '๐จ', '๐ฉ', '๐ด', '๐ต', '๐ฑ', '๐ผ', '๐ธ', '๐บ', '๐ธ', '๐ป', '๐ฝ', '๐ผ', '๐', '๐ฟ', '๐น', '๐พ', '๐น', '๐บ', '๐', '๐', '๐', '๐', '๐ฝ', '๐ฉ', '๐ฅ', 'โจ', '๐', '๐ซ', '๐ฅ', '๐ข', '๐ฆ', '๐ง', '๐ค', '๐จ', '๐', '๐', '๐', '๐
', '๐', '๐', '๐', '๐', '๐', 'โ', 'โ', '๐', 'โ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', 'โ', '๐', '๐ช', '๐ถ', '๐', '๐', '๐ซ', '๐ช', '๐ฌ', '๐ญ', '๐', '๐', '๐ฏ', '๐', '๐
', '๐', '๐', '๐', '๐', '๐
', '๐ฐ', '๐', '๐', '๐', '๐ฉ', '๐', '๐', '๐', '๐', '๐ก', '๐ ', '๐ข', '๐', '๐', '๐', '๐', '๐ฝ', '๐', '๐', '๐', '๐ผ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', 'โค', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ค', '๐ฅ', '๐ฌ', '๐ฃ', '๐ญ', '๐ถ', '๐บ', '๐ฑ', '๐ญ', '๐น', '๐ฐ', '๐ธ', '๐ฏ', '๐จ', '๐ป', '๐ท', '๐ฝ', '๐ฎ', '๐', '๐ต', '๐', '๐ด', '๐', '๐', '๐ผ', '๐ง', '๐ฆ', '๐ค', '๐ฅ', '๐ฃ', '๐', '๐', '๐ข', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ ', '๐', '๐ฌ', '๐ณ', '๐', '๐', '๐', '๐', '๐', '๐
', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ฒ', '๐ก', '๐', '๐ซ', '๐ช', '๐', '๐', '๐ฉ', '๐พ', '๐', '๐ธ', '๐ท', '๐', '๐น', '๐ป', '๐บ', '๐', '๐', '๐', '๐ฟ', '๐พ', '๐', '๐ต', '๐ด', '๐ฒ', '๐ณ', '๐ฐ', '๐ฑ', '๐ผ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ ', 'โญ', 'โ', 'โ
', 'โ', 'โก', 'โ', 'โ', 'โ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ป', '๐
', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ฎ', '๐ฅ', '๐ท', '๐น', '๐ผ', '๐ฟ', '๐', '๐ฝ', '๐พ', '๐ป', '๐ฑ', 'โ', '๐', '๐', '๐ ', '๐ก', '๐บ', '๐ป', '๐', '๐', '๐', '๐', '๐', '๐', '๐ข', '๐ฃ', 'โณ', 'โ', 'โฐ', 'โ', '๐', '๐', '๐', '๐', '๐', '๐', '๐ก', '๐ฆ', '๐', '๐
', '๐', '๐', '๐', '๐', '๐', '๐ฟ', '๐ฝ', '๐ง', '๐ฉ', '๐จ', '๐ช', '๐ฌ', '๐ฃ', '๐ซ', '๐ช', '๐', '๐', '๐ฐ', '๐ด', '๐ต', '๐ท', '๐ถ', '๐ณ', '๐ธ', '๐ฒ', '๐ง', '๐ฅ', '๐ค', 'โ', '๐ฉ', '๐จ', '๐ฏ', '๐ซ', '๐ช', '๐ฌ', '๐ญ', '๐ฎ', '๐ฆ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐
', '๐', '๐', '๐', '๐', 'โ', '๐', '๐', 'โ', 'โ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ฌ', '๐ญ', '๐ฐ', '๐จ', '๐ฌ', '๐ค', '๐ง', '๐ผ', '๐ต', '๐ถ', '๐น', '๐ป', '๐บ', '๐ท', '๐ธ', '๐พ', '๐ฎ', '๐', '๐ด', '๐', '๐ฒ', '๐ฏ', '๐', '๐', 'โฝ', 'โพ', '๐พ', '๐ฑ', '๐', '๐ณ', 'โณ', '๐ต', '๐ด', '๐', '๐', '๐', '๐ฟ', '๐', '๐', '๐', '๐ฃ', 'โ', '๐ต', '๐ถ', '๐ผ', '๐บ', '๐ป', '๐ธ', '๐น', '๐ท', '๐ด', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ค', '๐ฑ', '๐ฃ', '๐ฅ', '๐', '๐', '๐', '๐', '๐ฒ', '๐ข', '๐ก', '๐ณ', '๐', '๐ฉ', '๐ฎ', '๐ฆ', '๐จ', '๐ง', '๐', '๐ฐ', '๐ช', '๐ซ', '๐ฌ', '๐ญ', '๐ฏ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ ', '๐', '๐
', '๐ฝ', '๐ ', '๐ก', '๐ซ', '๐ข', '๐ฃ', '๐ฅ', '๐ฆ', '๐ช', '๐ฉ', '๐จ', '๐', 'โช', '๐ฌ', '๐ค', '๐', '๐', '๐ฏ', '๐ฐ', 'โบ', '๐ญ', '๐ผ', '๐พ', '๐ป', '๐', '๐
', '๐', '๐ฝ', '๐', '๐ ', '๐ก', 'โฒ', '๐ข', '๐ข', 'โต', '๐ค', '๐ฃ', 'โ', '๐', 'โ', '๐บ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐
', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐จ', '๐', '๐', '๐', '๐', '๐', '๐ฒ', '๐ก', '๐', '๐ ', '๐', '๐', '๐', '๐ซ', '๐ฆ', '๐ฅ', 'โ ', '๐ง', '๐ฐ', 'โฝ', '๐ฎ', '๐ฐ', 'โจ', '๐ฟ', '๐ช', '๐ญ', '๐', '๐ฉ', 'โฌ', 'โฌ', 'โฌ
', 'โก', '๐ ', '๐ก', '๐ค', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', '๐', 'โ', 'โถ', '๐ผ', '๐ฝ', 'โฉ', 'โช', 'โน', 'โช', 'โฉ', 'โซ', 'โฌ', 'โคต', 'โคด', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ถ', '๐ฆ', '๐', '๐ฏ', '๐ณ', '๐ต', '๐ด', '๐ฒ', '๐', '๐น', '๐บ', '๐ถ', '๐', '๐ป', '๐น', '๐บ', '๐ผ', '๐พ', '๐ฐ', '๐ฎ', '๐
ฟ', 'โฟ', '๐ญ', '๐ท', '๐ธ', '๐', 'โ', '๐', '๐', '๐
', '๐', '๐', 'ใ', 'ใ', '๐', '๐', '๐', '๐ซ', '๐', '๐ต', '๐ฏ', '๐ฑ', '๐ณ', '๐ท', '๐ธ', 'โ', 'โณ', 'โ', 'โ', 'โ
', 'โด', '๐', '๐', '๐ณ', '๐ด', '๐
ฐ', '๐
ฑ', '๐', '๐
พ', '๐ ', 'โฟ', 'โป', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', '๐ฏ', '๐ง', '๐น', '๐ฒ', '๐ฑ', 'ยฉ', 'ยฎ', 'โข', 'ใฝ', 'ใฐ', '๐', '๐', '๐', '๐', '๐', 'โ', 'โญ', 'โ', 'โ', 'โ', 'โ', '๐', '๐', '๐ง', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ ', '๐', '๐', '๐', '๐', '๐', '๐', '๐ก', '๐ข', '๐ฃ', '๐ค', '๐ฅ', '๐ฆ', 'โ', 'โ', 'โ', 'โ', 'โ ', 'โฅ', 'โฃ', 'โฆ', '๐ฎ', '๐ฏ', 'โ', 'โ', '๐', '๐', 'โฐ', '๐ฑ', '๐ฒ', '๐ณ', 'โผ', 'โป', 'โพ', 'โฝ', 'โช', 'โซ', '๐บ', 'โฌ', 'โฌ', 'โซ', 'โช', '๐ด', '๐ต', '๐ป', '๐ถ', '๐ท', '๐ธ', '๐น'
];
return emojis[Math.floor(Math.random() * emojis.length)];
}
Your front-end might look as you prefer. Example of mine is on the GIF
The tool itself - Emoji Me
Instead of a conclusion.
Itโs was a tiny example of an AI application for some routine automations, but itโs a step into very promising and intriguing AI world. There are so much interesting to discover so do it and enjoy.
If you like the article, follow me on social media โค๏ธ
Top comments (0)