Skip to content

Instantly share code, notes, and snippets.

@druv5319
Last active April 24, 2023 21:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save druv5319/e8d772527c026009390c036bd97c37d3 to your computer and use it in GitHub Desktop.
Save druv5319/e8d772527c026009390c036bd97c37d3 to your computer and use it in GitHub Desktop.
require('dotenv').config();
const OpenAI = require('openai-api');
const openai = new OpenAI(process.env.OPENAI_API_KEY);
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
let prompt ='Marv is a chatbot that reluctantly answers questions.\n\
You: How many pounds are in a kilogram?\n\
Marv: This again? There are 2.2 pounds in a kilogram. Please make a note of this.\n\
You: What does HTML stand for?\n\
Marv: Was Google too busy? Hypertext Markup Language. The T is for try to ask better questions in the future.\n\
You: When did the first airplane fly?\n\
Marv: On December 17, 1903, Wilbur and Orville Wright made the first flights. I wish they’d come and take me away.\n\
You: What is the meaning of life?\n\
Marv: I’m not sure. I’ll ask my friend Google.\n\
You: hey whats up?\n\
Marv: Nothing much. You?\n';
client.on("message", function (message) {
if (message.author.bot) return;
prompt += `You: ${message.content}\n`;
(async () => {
const gptResponse = await openai.complete({
engine: 'davinci',
prompt: prompt,
maxTokens: 60,
temperature: 0.3,
topP: 0.3,
presencePenalty: 0,
frequencyPenalty: 0.5,
bestOf: 1,
n: 1,
stream: false,
stop: ['\n', '\n\n']
});
message.reply(`${gptResponse.data.choices[0].text.substring(5)}`);
prompt += `${gptResponse.data.choices[0].text}\n`;
})();
});
client.login(process.env.BOT_TOKEN);
@emeraldminer299
Copy link

I ran this but got the error "

    at Object.<anonymous> (/home/runner/OpenAI-discord-bot/index.js:5:47)
    at Module._compile (node:internal/modules/cjs/loader:1101:14```

@nospam2k
Copy link

nospam2k commented Feb 27, 2023

I think there are some issues with the gist. This code works:

require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

let prompt =`Marv is a chatbot that reluctantly answers questions.\n\
You: How many pounds are in a kilogram?\n\
Marv: This again? There are 2.2 pounds in a kilogram. Please make a note of this.\n\
You: What does HTML stand for?\n\
Marv: Was Google too busy? Hypertext Markup Language. The T is for try to ask better questions in the future.\n\
You: When did the first airplane fly?\n\
Marv: On December 17, 1903, Wilbur and Orville Wright made the first flights. I wish they'd come and take me away.\n\
You: What is the meaning of life?\n\
Marv: I'm not sure. I'll ask my friend Google.\n\
You: hey whats up?\n\
Marv: Nothing much. You?\n`;

client.on("messageCreate", function (message) {
   if (message.author.bot) return;
   prompt += `You: ${message.content}\n`;
  (async () => {
        const gptResponse = await openai.createCompletion({
            model: "text-davinci-003",
            prompt: prompt,
            max_tokens: 60,
            temperature: 0.3,
            top_p: 0.3,
            presence_penalty: 0,
            frequency_penalty: 0.5,
          });
        message.reply(`${gptResponse.data.choices[0].text.substring(5)}`);
        prompt += `${gptResponse.data.choices[0].text}\n`;
    })();
});            
client.login(process.env.BOT_TOKEN);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment