Last active
          April 24, 2023 14:49 
        
      - 
      
- 
        Save mikesprague/8b3387a2ed6f7bf31ee1a8cc6a81dbcf to your computer and use it in GitHub Desktop. 
Revisions
- 
        mikesprague revised this gist Apr 24, 2023 . 2 changed files with 35 additions and 17 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ import { Configuration, OpenAIApi } from 'openai'; import dotenv from 'dotenv'; import { gptGetEmoji } from './helpers.js'; dotenv.config(); @@ -34,8 +34,8 @@ const text = ` `; const emojis = await gptGetEmoji({ textToAnalyze: text.trim(), openAiClient: openai, }); console.log(emojis); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,28 @@ 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, @@ -19,26 +44,19 @@ export const gptGetEmoji = async ({ 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); } 
- 
        mikesprague revised this gist Apr 24, 2023 . 1 changed file with 16 additions and 16 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,27 +1,27 @@ [ { "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." } ] 
- 
        mikesprague revised this gist Apr 24, 2023 . 3 changed files with 43 additions and 39 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -33,6 +33,9 @@ const text = ` (internet counterpart). `; const emojis = await gptGetEmoji({ textToAnalyze: text.trim(), openAiClient: openai, }); console.log(emojis); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,27 +1,27 @@ [ { 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.' } ] This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,9 @@ export const gptGetEmoji = async ({ textToAnalyze, openAiClient, model = 'gpt-3.5-turbo', temperature = 0.2, }) => { let emojiJson = [ { emoji: '😞', @@ -7,37 +12,33 @@ export const gptGetEmoji = async (textToAnalyze, openAiClient, temperature = 0.3 }, ]; 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 openAiClient.createChatCompletion({ model, messages: [ { role: 'system', content: systemPrompt.trim(), }, { role: 'user', content: textToAnalyze.trim(), }, ], temperature, }); emojiJson = JSON.parse(emojiResponse.data.choices[0].message.content); } catch (error) { console.log(error); } 
- 
        mikesprague revised this gist Apr 24, 2023 . 1 changed file with 14 additions and 5 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,13 +24,22 @@ export const gptGetEmoji = async (textToAnalyze, openAiClient, temperature = 0.3 ], temperature, }); let content = emojiResponse.data.choices[0].message.content; const getContent = (content, char1, char2) => { let str = content.split(char1); str = str[1].split(char2); return str[0]; }; content = content.includes('```json') ? getContent(content, '```json', '```').trim() : content; emojiJson = JSON.parse(content); } catch (error) { console.log(error); } return emojiJson; }; 
- 
        mikesprague revised this gist Apr 23, 2023 . 2 changed files with 1 addition and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ import { Configuration, OpenAIApi } from 'openai'; import dotenv from 'dotenv'; import { gptGetEmoji } from './helpers.js' dotenv.config(); File renamed without changes.
- 
        mikesprague revised this gist Apr 23, 2023 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -30,8 +30,9 @@ const text = ` 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); 
- 
        mikesprague revised this gist Apr 23, 2023 . 2 changed files with 1 addition and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ import { Configuration, OpenAIApi } from 'openai'; import dotenv from 'dotenv'; import { gptGetEmoji } from './openai.js' dotenv.config(); File renamed without changes.
- 
        mikesprague revised this gist Apr 23, 2023 . 1 changed file with 15 additions and 15 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,27 +1,27 @@ [ { "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" } ] 
- 
        mikesprague created this gist Apr 23, 2023 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ [ { 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' } ] This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ 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); }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ import { Configuration, OpenAIApi } from 'openai'; import dotenv from 'dotenv'; import { gptGetEmoji } from './helper.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, openai); console.log(emojis);