Basic Dall-e implementation

This commit is contained in:
Skylar Grant 2023-05-28 10:08:51 -04:00
parent 755c8510d7
commit 53625be91f
2 changed files with 60 additions and 1 deletions

View File

@ -28,6 +28,14 @@ const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY, apiKey: process.env.OPENAI_API_KEY,
}); });
const openai = new OpenAIApi(configuration); 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 // Various imports from other files
const config = require('./config.json'); const config = require('./config.json');
@ -544,7 +552,7 @@ const functions = {
const response = await openai.createCompletion({ const response = await openai.createCompletion({
model: 'text-davinci-003', model: 'text-davinci-003',
prompt: userPrompt, prompt: userPrompt,
temperature: 0, temperature: 0.7,
max_tokens: 250 max_tokens: 250
}).catch(e => { }).catch(e => {
reject(e); reject(e);
@ -552,6 +560,20 @@ const functions = {
}); });
resolve(response); 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) // Parent-Level functions (miscellaneuous)
@ -595,6 +617,18 @@ const functions = {
return newText + ' <:spongebob:1053398825965985822>'; 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; module.exports = functions;

25
slash-commands/dalle.js Normal file
View File

@ -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}`);
}
},
};