-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (30 loc) · 1.2 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
import openai from './config/open_ai.js';
import readlineSync from 'readline-sync';
import colors from 'colors';
async function main() {
console.log(colors.bold.green('Welcome to the Chatbot Program'));
console.log(colors.bold.green('You can start chatting with the bot'));
const chatLog = [];
while (true) {
const userInput = readlineSync.question(colors.yellow('You: '))
try {
const messages = chatLog.map(([role, content]) => ({ role, content }));
messages.push({ role: 'user', content: userInput });
const completion = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: messages
});
const completionText = completion.choices[0].message.content;
if (userInput.toLowerCase() === 'exit') {
console.log(colors.green('Bot: ') + completionText);
return;
}
console.log(colors.green('Bot: ') + completionText);
chatLog.push(['user', userInput]);
chatLog.push(['assistant', completionText]);
} catch (error) {
console.error(colors.red(error));
}
}
}
main();