I've been implemented a JavaScript AI emoji generator recently. And as OpenAI API responses are not always consistent, or maybe my prompts are far from ideal, I needed to create a backup function that generates random emojis if the response is not parsed properly.
Surprisingly, I found that functions might more look like an instruction for a person than for a machine in future ๐
. Here is what I mean:
// regular JS function
function getNEmojis(n) {
const emojis = [
'๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ณ', '๐', '๐', '๐', '๐', '๐', '๐ฃ', '๐ข', '๐', '๐ญ', '๐ช', '๐ฅ', '๐ฐ', '๐
', '๐', '๐ฉ', '๐ซ', '๐จ', '๐ฑ', '๐ ', '๐ก', '๐ค', '๐', '๐', '๐', '๐ท', '๐', '๐ด', '๐ต', '๐ฒ', '๐', '๐ฆ', '๐ง', '๐', '๐ฟ', '๐ฎ', '๐ฌ', '๐', '๐', '๐ฏ', '๐ถ', '๐', '๐', '๐', '๐ฒ', '๐ณ', '๐ฎ', '๐ท', '๐', '๐ถ', '๐ฆ', '๐ง', '๐จ', '๐ฉ', '๐ด', '๐ต', '๐ฑ', '๐ผ', '๐ธ', '๐บ', '๐ธ', '๐ป', '๐ฝ', '๐ผ', '๐', '๐ฟ', '๐น', '๐พ', '๐น', '๐บ', '๐', '๐', '๐', '๐', '๐ฝ', '๐ฉ', '๐ฅ', 'โจ', '๐', '๐ซ', '๐ฅ', '๐ข', '๐ฆ', '๐ง', '๐ค', '๐จ', '๐', '๐', '๐', '๐
', '๐', '๐', '๐', '๐', '๐', 'โ', 'โ', '๐', 'โ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', 'โ', '๐', '๐ช', '๐ถ', '๐', '๐', '๐ซ', '๐ช', '๐ฌ', '๐ญ', '๐', '๐', '๐ฏ', '๐', '๐
', '๐', '๐', '๐', '๐', '๐
', '๐ฐ', '๐', '๐', '๐', '๐ฉ', '๐', '๐', '๐', '๐', '๐ก', '๐ ', '๐ข', '๐', '๐', '๐', '๐', '๐ฝ', '๐', '๐', '๐', '๐ผ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', 'โค', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ค', '๐ฅ', '๐ฌ', '๐ฃ', '๐ญ', '๐ถ', '๐บ', '๐ฑ', '๐ญ', '๐น', '๐ฐ', '๐ธ', '๐ฏ', '๐จ', '๐ป', '๐ท', '๐ฝ', '๐ฎ', '๐', '๐ต', '๐', '๐ด', '๐', '๐', '๐ผ', '๐ง', '๐ฆ', '๐ค', '๐ฅ', '๐ฃ', '๐', '๐', '๐ข', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ ', '๐', '๐ฌ', '๐ณ', '๐', '๐', '๐', '๐', '๐', '๐
', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ฒ', '๐ก', '๐', '๐ซ', '๐ช', '๐', '๐', '๐ฉ', '๐พ', '๐', '๐ธ', '๐ท', '๐', '๐น', '๐ป', '๐บ', '๐', '๐', '๐', '๐ฟ', '๐พ', '๐', '๐ต', '๐ด', '๐ฒ', '๐ณ', '๐ฐ', '๐ฑ', '๐ผ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ ', 'โญ', 'โ', 'โ
', 'โ', 'โก', 'โ', 'โ', 'โ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ป', '๐
', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ฎ', '๐ฅ', '๐ท', '๐น', '๐ผ', '๐ฟ', '๐', '๐ฝ', '๐พ', '๐ป', '๐ฑ', 'โ', '๐', '๐', '๐ ', '๐ก', '๐บ', '๐ป', '๐', '๐', '๐', '๐', '๐', '๐', '๐ข', '๐ฃ', 'โณ', 'โ', 'โฐ', 'โ', '๐', '๐', '๐', '๐', '๐', '๐', '๐ก', '๐ฆ', '๐', '๐
', '๐', '๐', '๐', '๐', '๐', '๐ฟ', '๐ฝ', '๐ง', '๐ฉ', '๐จ', '๐ช', '๐ฌ', '๐ฃ', '๐ซ', '๐ช', '๐', '๐', '๐ฐ', '๐ด', '๐ต', '๐ท', '๐ถ', '๐ณ', '๐ธ', '๐ฒ', '๐ง', '๐ฅ', '๐ค', 'โ', '๐ฉ', '๐จ', '๐ฏ', '๐ซ', '๐ช', '๐ฌ', '๐ญ', '๐ฎ', '๐ฆ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐
', '๐', '๐', '๐', '๐', 'โ', '๐', '๐', 'โ', 'โ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ฌ', '๐ญ', '๐ฐ', '๐จ', '๐ฌ', '๐ค', '๐ง', '๐ผ', '๐ต', '๐ถ', '๐น', '๐ป', '๐บ', '๐ท', '๐ธ', '๐พ', '๐ฎ', '๐', '๐ด', '๐', '๐ฒ', '๐ฏ', '๐', '๐', 'โฝ', 'โพ', '๐พ', '๐ฑ', '๐', '๐ณ', 'โณ', '๐ต', '๐ด', '๐', '๐', '๐', '๐ฟ', '๐', '๐', '๐', '๐ฃ', 'โ', '๐ต', '๐ถ', '๐ผ', '๐บ', '๐ป', '๐ธ', '๐น', '๐ท', '๐ด', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ค', '๐ฑ', '๐ฃ', '๐ฅ', '๐', '๐', '๐', '๐', '๐ฒ', '๐ข', '๐ก', '๐ณ', '๐', '๐ฉ', '๐ฎ', '๐ฆ', '๐จ', '๐ง', '๐', '๐ฐ', '๐ช', '๐ซ', '๐ฌ', '๐ญ', '๐ฏ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ ', '๐', '๐
', '๐ฝ', '๐ ', '๐ก', '๐ซ', '๐ข', '๐ฃ', '๐ฅ', '๐ฆ', '๐ช', '๐ฉ', '๐จ', '๐', 'โช', '๐ฌ', '๐ค', '๐', '๐', '๐ฏ', '๐ฐ', 'โบ', '๐ญ', '๐ผ', '๐พ', '๐ป', '๐', '๐
', '๐', '๐ฝ', '๐', '๐ ', '๐ก', 'โฒ', '๐ข', '๐ข', 'โต', '๐ค', '๐ฃ', 'โ', '๐', 'โ', '๐บ', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐
', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐จ', '๐', '๐', '๐', '๐', '๐', '๐ฒ', '๐ก', '๐', '๐ ', '๐', '๐', '๐', '๐ซ', '๐ฆ', '๐ฅ', 'โ ', '๐ง', '๐ฐ', 'โฝ', '๐ฎ', '๐ฐ', 'โจ', '๐ฟ', '๐ช', '๐ญ', '๐', '๐ฉ', 'โฌ', 'โฌ', 'โฌ
', 'โก', '๐ ', '๐ก', '๐ค', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', '๐', 'โ', 'โถ', '๐ผ', '๐ฝ', 'โฉ', 'โช', 'โน', 'โช', 'โฉ', 'โซ', 'โฌ', 'โคต', 'โคด', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ถ', '๐ฆ', '๐', '๐ฏ', '๐ณ', '๐ต', '๐ด', '๐ฒ', '๐', '๐น', '๐บ', '๐ถ', '๐', '๐ป', '๐น', '๐บ', '๐ผ', '๐พ', '๐ฐ', '๐ฎ', '๐
ฟ', 'โฟ', '๐ญ', '๐ท', '๐ธ', '๐', 'โ', '๐', '๐', '๐
', '๐', '๐', 'ใ', 'ใ', '๐', '๐', '๐', '๐ซ', '๐', '๐ต', '๐ฏ', '๐ฑ', '๐ณ', '๐ท', '๐ธ', 'โ', 'โณ', 'โ', 'โ', 'โ
', 'โด', '๐', '๐', '๐ณ', '๐ด', '๐
ฐ', '๐
ฑ', '๐', '๐
พ', '๐ ', 'โฟ', 'โป', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', 'โ', '๐ฏ', '๐ง', '๐น', '๐ฒ', '๐ฑ', 'ยฉ', 'ยฎ', 'โข', 'ใฝ', 'ใฐ', '๐', '๐', '๐', '๐', '๐', 'โ', 'โญ', 'โ', 'โ', 'โ', 'โ', '๐', '๐', '๐ง', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐', '๐ ', '๐', '๐', '๐', '๐', '๐', '๐', '๐ก', '๐ข', '๐ฃ', '๐ค', '๐ฅ', '๐ฆ', 'โ', 'โ', 'โ', 'โ', 'โ ', 'โฅ', 'โฃ', 'โฆ', '๐ฎ', '๐ฏ', 'โ', 'โ', '๐', '๐', 'โฐ', '๐ฑ', '๐ฒ', '๐ณ', 'โผ', 'โป', 'โพ', 'โฝ', 'โช', 'โซ', '๐บ', 'โฌ', 'โฌ', 'โซ', 'โช', '๐ด', '๐ต', '๐ป', '๐ถ', '๐ท', '๐ธ', '๐น'
];
const res = [];
for (let i = 0; i < n; i++) {
res.push([]);
for (let j = 0; j < n; j++) {
res[i].push(emojis[Math.floor(Math.random() * emojis.length)]);
}
}
return JSON.stringify(res);
}
// AI powered JS funciton
function getNEmojisAI(n) {
const API_KEY = process.env.OPENAI_API_KEY;
const MODEL_ID = 'gpt-3.5-turbo';
const configuration = new Configuration({
apiKey: API_KEY,
});
const openai = new OpenAIApi(configuration);
const completion = await openai.createChatCompletion({
model: MODEL_ID,
messages: [{ "role": "user", "content": `generate exactly ${n} 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 ${n}.
No additional texts or explanations or extra array entities.
The input: "${input.trim()}"`
}]
});
return completion.data.choices[0].message.content;
}
Both functions return the same output, but as you can see, the biggest part of the second function is more like a WhatsApp message than a JavaScript function. It looks like one day, with all the AI improvements, our code might transform into a set of AI-prompts incapsulated into functions and classes. So enjoy coding and learn better prompts โ๐ฝ
If you like the article, follow me on social media โค๏ธ
Top comments (0)