-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathchat-bot.js
62 lines (54 loc) · 1.86 KB
/
chat-bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const Discord = require("discord-user-bots");
const OpenAI = require("openai"); // `npm i openai` to install
const client = new Discord.Client();
const openai = new OpenAI({
apiKey: "OpenAI token goes here.",
});
// Config
const config = {
channel: "channel_id", // Channel to chat in
memory_limit: 20, // ChatGPT's message memory limit
model: "gpt-3.5-turbo", // Engine to use
max_tokens: 200, // Max tokens ChatGPT will use to respond with
system_text: "You are a chat-bot on the social media platform Discord.",
};
// Memory of messages
let memory = [];
client.on("ready", () => {
console.log(`${engine} chatbot is online!`);
});
client.on.message_create = async function (message) {
if (message.channel_id != channelID) return;
if (message.author.id == client.info.user.id) {
// If the message is by the bot, add it to memory
memory.push({
role: "assistant",
content: `@Chat bot: ${message.content}`,
});
} else {
// If the message is by a user, add it to memory and provoke a response
if (memory.length == config.memory_limit) memory.shift();
memory.push({
role: "user",
content: `@${message.author.username}: ${message.content}`,
});
const gptResponse = await openai.chat.completions.create({
model: config.model,
messages: [
{
role: "system",
content: config.system_text,
},
...memory,
],
temperature: 0.7,
max_tokens: config.max_tokens,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
});
// Send response
client.send(config.channel, { content: gptResponse.data.choices[0].text });
}
};
client.login("Token goes here.");