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.

Revisions

  1. mikesprague revised this gist Apr 24, 2023. 2 changed files with 35 additions and 17 deletions.
    6 changes: 3 additions & 3 deletions chatgpt-text-to-emojis.js
    Original 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'
    import { gptGetEmoji } from './helpers.js';

    dotenv.config();

    @@ -34,8 +34,8 @@ const text = `
    `;

    const emojis = await gptGetEmoji({
    textToAnalyze: text.trim(),
    textToAnalyze: text.trim(),
    openAiClient: openai,
    });
    });

    console.log(emojis);
    46 changes: 32 additions & 14 deletions helpers.js
    Original 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.
    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({
    const emojiResponse = await gptAnalyzeText({
    systemPrompt,
    textToAnalyze,
    openAiClient,
    model,
    messages: [
    {
    role: 'system',
    content: systemPrompt.trim(),
    },
    {
    role: 'user',
    content: textToAnalyze.trim(),
    },
    ],
    temperature,
    });

    emojiJson = JSON.parse(emojiResponse.data.choices[0].message.content);
    emojiJson = JSON.parse(emojiResponse);
    } catch (error) {
    console.log(error);
    }
  2. mikesprague revised this gist Apr 24, 2023. 1 changed file with 16 additions and 16 deletions.
    32 changes: 16 additions & 16 deletions example-results.json
    Original 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": ":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": ":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": ":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": ":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.'
    "emoji": "🌌",
    "short_code": ":milky_way:",
    "reasoning": "The text mentions a galaxy far, far away, and the milky way emoji represents this idea."
    }
    ]
    ]
  3. mikesprague revised this gist Apr 24, 2023. 3 changed files with 43 additions and 39 deletions.
    5 changes: 4 additions & 1 deletion chatgpt-text-to-emojis.js
    Original file line number Diff line number Diff line change
    @@ -33,6 +33,9 @@ const text = `
    (internet counterpart).
    `;

    const emojis = await gptGetEmoji(text.trim(), openai);
    const emojis = await gptGetEmoji({
    textToAnalyze: text.trim(),
    openAiClient: openai,
    });

    console.log(emojis);
    32 changes: 16 additions & 16 deletions example-results.json
    Original 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: ':rocket:',
    reasoning: 'The text mentions space travel and spacecrafts, and the rocket emoji represents this idea.'
    },
    {
    "emoji": "👽",
    "short_code": ":alien:",
    "reasoning": "Represents the many species of aliens in the franchise"
    emoji: '🪐',
    short_code: ':ringed_planet:',
    reasoning: 'The text mentions planets and astronomical objects, and the ringed planet emoji represents this idea.'
    },
    {
    "emoji": "🌌",
    "short_code": ":milky_way:",
    "reasoning": "Represents the galaxy far, far away"
    emoji: '🤖',
    short_code: ':robot:',
    reasoning: "The text mentions robots, or 'droids', and the robot emoji represents this idea."
    },
    {
    "emoji": "🤖",
    "short_code": ":robot:",
    "reasoning": "Represents the robots, or 'droids', in the franchise"
    emoji: '👽',
    short_code: ':alien:',
    reasoning: 'The text mentions aliens, and the alien emoji represents this idea.'
    },
    {
    "emoji": "🌵",
    "short_code": ":cactus:",
    "reasoning": "Represents the deserts scarcely populated by primitive tribes"
    emoji: '🌌',
    short_code: ':milky_way:',
    reasoning: 'The text mentions a galaxy far, far away, and the milky way emoji represents this idea.'
    }
    ]
    ]
    45 changes: 23 additions & 22 deletions helpers.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,9 @@
    export const gptGetEmoji = async (textToAnalyze, openAiClient, temperature = 0.3) => {
    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 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 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: 'gpt-3.5-turbo',
    model,
    messages: [
    {
    role: 'assistant',
    content: emojiPrompt.trim(),
    role: 'system',
    content: systemPrompt.trim(),
    },
    {
    role: 'user',
    content: textToAnalyze.trim(),
    },
    ],
    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);
    emojiJson = JSON.parse(emojiResponse.data.choices[0].message.content);
    } catch (error) {
    console.log(error);
    }
  4. mikesprague revised this gist Apr 24, 2023. 1 changed file with 14 additions and 5 deletions.
    19 changes: 14 additions & 5 deletions helpers.js
    Original 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;

    emojiJson = emojiResponse.data.choices[0].message.content
    .replace('```json', '')
    .replace('```', '')
    .trim();
    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 JSON.parse(emojiJson);
    return emojiJson;
    };
  5. mikesprague revised this gist Apr 23, 2023. 2 changed files with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.js → chatgpt-text-to-emojis.js
    Original 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'
    import { gptGetEmoji } from './helpers.js'

    dotenv.config();

    File renamed without changes.
  6. mikesprague revised this gist Apr 23, 2023. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions main.js
    Original 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).`;
    (internet counterpart).
    `;

    const emojis = await gptGetEmoji(text, openai);
    const emojis = await gptGetEmoji(text.trim(), openai);

    console.log(emojis);
  7. mikesprague revised this gist Apr 23, 2023. 2 changed files with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.js
    Original 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 './helper.js'
    import { gptGetEmoji } from './openai.js'

    dotenv.config();

    File renamed without changes.
  8. mikesprague revised this gist Apr 23, 2023. 1 changed file with 15 additions and 15 deletions.
    30 changes: 15 additions & 15 deletions example-results.json
    Original 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": ":rocket:",
    "reasoning": "Represents space travel and spacecraft"
    },
    {
    emoji: '👽',
    short_code: ':alien:',
    reasoning: 'Represents the many species of aliens in the franchise'
    "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": ":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": ":robot:",
    "reasoning": "Represents the robots, or 'droids', in the franchise"
    },
    {
    emoji: '🌵',
    short_code: ':cactus:',
    reasoning: 'Represents the deserts scarcely populated by primitive tribes'
    "emoji": "🌵",
    "short_code": ":cactus:",
    "reasoning": "Represents the deserts scarcely populated by primitive tribes"
    }
    ]
  9. mikesprague created this gist Apr 23, 2023.
    27 changes: 27 additions & 0 deletions example-results.json
    Original 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'
    }
    ]
    36 changes: 36 additions & 0 deletions helper.js
    Original 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);
    };
    37 changes: 37 additions & 0 deletions main.js
    Original 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);