Skip to content

Instantly share code, notes, and snippets.

@mikesprague
Last active April 24, 2023 14:49
Show Gist options
  • 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(text.trim(), openai);
console.log(emojis);
[
{
"emoji": "πŸš€",
"short_code": ":rocket:",
"reasoning": "Represents space travel and spacecraft"
},
{
"emoji": "πŸ‘½",
"short_code": ":alien:",
"reasoning": "Represents the many species of aliens in the franchise"
},
{
"emoji": "🌌",
"short_code": ":milky_way:",
"reasoning": "Represents the galaxy far, far away"
},
{
"emoji": "πŸ€–",
"short_code": ":robot:",
"reasoning": "Represents the robots, or 'droids', in the franchise"
},
{
"emoji": "🌡",
"short_code": ":cactus:",
"reasoning": "Represents the deserts scarcely populated by primitive tribes"
}
]
export const gptGetEmoji = async (textToAnalyze, openAiClient, temperature = 0.3) => {
let emojiJson = [
{
emoji: '😞',
short_code: ':disappointed_face:',
reasoning: 'There was an error with the request.',
},
];
try {
const emojiPrompt = `
Analyze the following text and provide at least 1 emojis from unicode
v15 in order of relevance, and their markdown short codes, that best
represent it as JSON with keys for emoji, short code, and reasoning.
Only return the resulting JSON array of objects: ${textToAnalyze.trim()}
`;
const emojiResponse = await openAiClient.createChatCompletion({
model: 'gpt-3.5-turbo',
messages: [
{
role: 'assistant',
content: emojiPrompt.trim(),
},
],
temperature,
});
emojiJson = emojiResponse.data.choices[0].message.content
.replace('```json', '')
.replace('```', '')
.trim();
} catch (error) {
console.log(error);
}
return JSON.parse(emojiJson);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment