From 53625be91f3f91483d4f4e1adb2236236372bbc8 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sun, 28 May 2023 10:08:51 -0400 Subject: [PATCH] Basic Dall-e implementation --- functions.js | 36 +++++++++++++++++++++++++++++++++++- slash-commands/dalle.js | 25 +++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 slash-commands/dalle.js diff --git a/functions.js b/functions.js index 2d2650e..3a0ebe2 100644 --- a/functions.js +++ b/functions.js @@ -28,6 +28,14 @@ const configuration = new Configuration({ apiKey: process.env.OPENAI_API_KEY, }); const openai = new OpenAIApi(configuration); +async function openAIStatus(o) { + const response = await o.listModels(); + const models = response.data.data; + models.forEach(e => { + console.log(`Model ID: ${e.id}`); + }); +}; +openAIStatus(openai); // Various imports from other files const config = require('./config.json'); @@ -544,7 +552,7 @@ const functions = { const response = await openai.createCompletion({ model: 'text-davinci-003', prompt: userPrompt, - temperature: 0, + temperature: 0.7, max_tokens: 250 }).catch(e => { reject(e); @@ -552,6 +560,20 @@ const functions = { }); resolve(response); }); + }, + imagePrompt(userPrompt, userId) { + return new Promise(async (resolve, reject) => { + try { + const response = await openai.createImage({ + prompt: userPrompt, + user: userId + }); + resolve(response.data.data[0].url); + } catch (e) { + reject(e); + return; + } + }); } }, // Parent-Level functions (miscellaneuous) @@ -595,6 +617,18 @@ const functions = { return newText + ' <:spongebob:1053398825965985822>'; }, + generateErrorId() { + const digitCount = 10; + const digits = []; + for (let i = 0; i < digitCount; i++) { + const randBase = Math.random(); + const randNumRaw = randBase * 10; + const randNumRound = Math.floor(randNumRaw); + digits.push(randNumRound); + } + const errorId = digits.join(""); + return errorId; + } }; module.exports = functions; \ No newline at end of file diff --git a/slash-commands/dalle.js b/slash-commands/dalle.js new file mode 100644 index 0000000..eb97f9d --- /dev/null +++ b/slash-commands/dalle.js @@ -0,0 +1,25 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const fn = require('../functions.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('dalle') + .setDescription('Generate an image with DALL-e') + .addStringOption(o => + o.setName("prompt") + .setDescription("Prompt to send to DALL-e") + .setRequired(true) + ), + async execute(interaction) { + try { + await interaction.deferReply(); + const userPrompt = interaction.options.getString("prompt"); + const response = await fn.openAI.imagePrompt(userPrompt); + await interaction.editReply(`${response}`); + } catch (err) { + const errorId = fn.generateErrorId(); + console.error(`${errorId}: ${err}`); + await interaction.editReply(`An error has occured. Error ID: ${errorId}\n${err}`); + } + }, +}; \ No newline at end of file