-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (34 loc) · 1.37 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
import openai from './config/openai.js';
import readlineSync from 'readline-sync';
import colors from 'colors';
async function main() {
console.log('Welcome to my Chatbot!'.bold.green);
console.log('You can begin a conversation with the bot.'.bold.green);
const chatHistory = []; // store conversation history
while(true) {
const userInput = readlineSync.question('You: '.yellow);
try {
// Construct messages using chat history
const messages = chatHistory.map(([role, content]) => ({role, content}));
// Add latest user input
messages.push({role: 'user', content: userInput});
// Call API with User Input
const chatCompletion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: messages,
})
// Get API response
const response = chatCompletion.choices[0].message.content;
if (userInput.toLowerCase() === 'exit') {
console.log('Bot: '.green + response);
return;
}
console.log('Bot: '.green + response);
// update history with user input and bot response
chatHistory.push(['user', userInput], ['assistant', response]);
} catch (error) {
console.log(error.red);
}
}
}
main();