forked from notunderctrl/gpt-3.5-chat-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (66 loc) · 2.19 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require('dotenv/config');
const { Client, IntentsBitField } = require('discord.js');
const { Configuration, OpenAIApi } = require('openai');
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
],
});
client.on('ready', () => {
console.log(`${client.user.username} is online!`);
});
const configuration = new Configuration({
apiKey: process.env.API_KEY,
});
const openai = new OpenAIApi(configuration);
const userConversations = {};
function limitInput(input, maxLength) {
return input.slice(0, maxLength);
}
client.on('messageCreate', async (message) => {
if (message.author.bot) return;
if (message.channel.id !== process.env.CHANNEL_ID) return;
if (message.content.startsWith('!')) return;
const userId = message.author.id;
if (!userConversations[userId]) {
userConversations[userId] = [
{
role: 'system',
content: `You are a helpful asistant`,
},
];
}
let conversationLog = userConversations[userId];
try {
await message.channel.sendTyping();
let prevMessages = await message.channel.messages.fetch({ limit: 15 });
prevMessages.reverse();
prevMessages.forEach((msg) => {
if (message.content.startsWith('!')) return;
if (msg.author.id !== client.user.id && message.author.bot) return;
if (msg.author.id !== message.author.id) return;
let userName = msg.author.username;
conversationLog.push({
role: 'user',
content: `You are speaking to ${userName}, this is your conversation history: ` + msg.content ,
});
});
const limitedConversationLog = limitInput(conversationLog, 2000);
const result = await openai
.createChatCompletion({
model: 'gpt-3.5-turbo',
messages: limitedConversationLog,
max_tokens: 140, // limit token usage
})
.catch((error) => {
console.log(`OPENAI ERR: ${error}`);
message.reply(`Sorry I'm having trouble thinking, try again later?`);
});
message.reply(result.data.choices[0].message);
} catch (error) {
console.log(`ERR: ${error}`);
}
});
client.login(process.env.TOKEN);