Skip to content

Instantly share code, notes, and snippets.

@mikesprague
Last active April 24, 2023 14:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikesprague/8b3387a2ed6f7bf31ee1a8cc6a81dbcf to your computer and use it in GitHub Desktop.
Save mikesprague/8b3387a2ed6f7bf31ee1a8cc6a81dbcf to your computer and use it in GitHub Desktop.
Using OpenAI to analyze text and return relevant emojis
import { Configuration, OpenAIApi } from 'openai';
import dotenv from 'dotenv';
import { gptGetEmoji } from './helpers.js';
dotenv.config();
const { OPEN_AI_API_KEY } = process.env;
const configuration = new Configuration({
apiKey: OPEN_AI_API_KEY,
});
const openai = new OpenAIApi(configuration);
// source: https://en.wikipedia.org/wiki/Star_Wars
const text = `
The Star Wars franchise depicts the adventures of characters
"A long time ago in a galaxy far, far away",[5] in which humans
and many species of aliens (often humanoid) co-exist with robots
(typically referred to in the films as 'droids'), who may assist
them in their daily routines; space travel between planets is
common due to lightspeed hyperspace technology.[6][7][8] The planets
range from wealthy, planet-wide cities to deserts scarcely populated
by primitive tribes. Virtually any Earth biome, along with many
fictional ones, has its counterpart as a Star Wars planet which, in
most cases, teem with sentient and non-sentient alien life.[9] The
franchise also makes use of other astronomical objects such as asteroid
fields and nebulae.[10][11] Spacecraft range from small starfighters,
to huge capital ships such as the Star Destroyers, to space stations
such as the moon-sized Death Stars. Telecommunication includes two-way
audio and audiovisual screens, holographic projections, and HoloNet
(internet counterpart).
`;
const emojis = await gptGetEmoji({
textToAnalyze: text.trim(),
openAiClient: openai,
});
console.log(emojis);
[
{
"emoji": "🚀",
"short_code": ":rocket:",
"reasoning": "The text mentions space travel and spacecrafts, and the rocket emoji represents this idea."
},
{
"emoji": "🪐",
"short_code": ":ringed_planet:",
"reasoning": "The text mentions planets and astronomical objects, and the ringed planet emoji represents this idea."
},
{
"emoji": "🤖",
"short_code": ":robot:",
"reasoning": "The text mentions robots, or 'droids', and the robot emoji represents this idea."
},
{
"emoji": "👽",
"short_code": ":alien:",
"reasoning": "The text mentions aliens, and the alien emoji represents this idea."
},
{
"emoji": "🌌",
"short_code": ":milky_way:",
"reasoning": "The text mentions a galaxy far, far away, and the milky way emoji represents this idea."
}
]
export const gptAnalyzeText = async ({
systemPrompt,
textToAnalyze,
openAiClient,
model = 'gpt-3.5-turbo',
temperature = 0.5,
}) => {
const gptResponse = await openAiClient.createChatCompletion({
model,
messages: [
{
role: 'system',
content: systemPrompt.trim(),
},
{
role: 'user',
content: textToAnalyze.trim(),
},
],
temperature,
});
return gptResponse.data.choices[0].message.content;
};
export const gptGetEmoji = async ({
textToAnalyze,
openAiClient,
model = 'gpt-3.5-turbo',
temperature = 0.2,
}) => {
let emojiJson = [
{
emoji: '😞',
short_code: ':disappointed_face:',
reasoning: 'There was an error with the request.',
},
];
try {
const systemPrompt = `
You're a text to emoji translation service. Analyze the text supplied by
users and provide at least 1 emojis from unicode v15 in order of relevance
to the text. Focus on the important content and subjects and not things
like the names of weekdays or months. You should also provide the markdown
short code for each emoji and the reasoning behind your selection. The
results should be returned as a JSON array of objects with each object
containing keys for the emoji, short code, and reasoning. Return ONLY
the resulting JSON array of objects like an API.
`;
const emojiResponse = await gptAnalyzeText({
systemPrompt,
textToAnalyze,
openAiClient,
model,
temperature,
});
emojiJson = JSON.parse(emojiResponse);
} catch (error) {
console.log(error);
}
return emojiJson;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment