First basic implementation of ChatGPT

This commit is contained in:
Skylar Grant 2023-05-27 20:41:08 -04:00
parent 57238ce57a
commit 755c8510d7
3 changed files with 48 additions and 3 deletions

View File

@ -21,6 +21,14 @@ const Discord = require('discord.js');
// Fuzzy text matching for db lookups
const FuzzySearch = require('fuzzy-search');
// OpenAI
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
// Various imports from other files
const config = require('./config.json');
const strings = require('./strings.json');
@ -530,6 +538,22 @@ const functions = {
}
}
},
openAI: {
chatPrompt(userPrompt) {
return new Promise(async (resolve, reject) => {
const response = await openai.createCompletion({
model: 'text-davinci-003',
prompt: userPrompt,
temperature: 0,
max_tokens: 250
}).catch(e => {
reject(e);
return null;
});
resolve(response);
});
}
},
// Parent-Level functions (miscellaneuous)
closeRequest(requestId, interaction) {
if (interaction.user.id == ownerId) {

View File

@ -1,7 +1,7 @@
{
"name": "nodbot",
"version": "3.1.0",
"description": "Nods and Nod Accessories.",
"version": "3.2.0",
"description": "Nods and Nod Accessories, now with ChatGPT!",
"main": "main.js",
"dependencies": {
"@discordjs/builders": "^0.16.0",
@ -12,10 +12,11 @@
"dotenv": "^10.0.0",
"fuzzy-search": "^3.2.1",
"mysql": "^2.18.1",
"openai": "^3.2.1",
"tenorjs": "^1.0.10"
},
"engines": {
"node": "16.x"
"node": "18.x"
},
"devDependencies": {
"eslint": "^7.32.0"

20
slash-commands/chat.js Normal file
View File

@ -0,0 +1,20 @@
const { SlashCommandBuilder } = require('@discordjs/builders');
const fn = require('../functions.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('chat')
.setDescription('Send a message to ChatGPT')
.addStringOption(o =>
o.setName("prompt")
.setDescription("Prompt to send to ChatGPT")
.setRequired(true)
),
async execute(interaction) {
await interaction.deferReply();
const userPrompt = interaction.options.getString("prompt");
const response = await fn.openAI.chatPrompt(userPrompt).catch(e => console.error(e));
const responseText = response.data.choices[0].text;
await interaction.editReply(`${responseText}`);
},
};