openai davinci is a pre trained version of the model used in ChatGPT. ChatGPT does not have an official API YET. There is an unofficial api updating every day but its not useful to implement. This is the verry official openai davinci implementation with an official API key. Just create an account + API Key with 18$ usage free on https://beta.openai.com/signup
/**
* This code is designed to respond to a prompt on a Discord channel tagging the bot.
* It uses the OpenAI API to generate a response to the prompt, and then posts that response
* on the Discord channel as answer of the initial message using the Discord API.
*
* Example: @bot what day of the week is it ?
* Response: today is monday
*
*/
// Require the lib and OpenAI modules
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
// Import the Configuration and OpenAIApi classes from the openai module
const {Configuration, OpenAIApi} = require("openai");
const configuration = new Configuration({
apiKey: "PUT_YOUR_OPENAI_API_KEY_HERE",
});
/**
* Create an instance of the Configuration class with the API key,
* and use it to create an instance of the OpenAIApi class
*/
const openai = new OpenAIApi(configuration);
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: `Q:${context.params.event.content}?\nA:`,
temperature: 0,
max_tokens: 400,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
stop: ["\n"],
});
/**
* A default answer of "I don't know" is defined, and then the code checks
* if there is a valid response in the response.data.choices array.
* If there is, the first item in the array is set as the answer.
*/
let answer = 'I dont know';
if (response.data.choices.length > 0 && response.data.choices[0].text !== "") {
answer = response.data.choices[0].text;
}
/**
* Finally, the create method of the lib.discord.channels['@0.2.2'].messages object is called
* to post the answer on the Discord channel. The channel_id and message_id parameters are taken
* from the context.params.event object, which contains information about the event that triggered
* the code execution (e.g., the message on the Discord channel).
*/
await lib.discord.channels['@0.2.2'].messages.create({
channel_id: context.params.event.channel_id,
content: answer,
message_reference: {
message_id: context.params.event.id
}
});