-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (45 loc) · 1.76 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
import openai from './config/open-ai.js'
import readlineSync from 'readline-sync'
import colors from 'colors'
import ora from 'ora';
import cliSpinners from 'cli-spinners';
// Create a new spinner instance
const spinner = ora({
text: 'Loading...',
spinner: cliSpinners.dots,
});
async function main() {
console.log(colors.bold.white('What\'s up ?'));
const chatHistory = []; //Store conv history
while(true) {
const userInput = readlineSync.question(colors.yellow('\nYou: '));
try {
if (userInput === 'cls' || userInput == 'clear') {
process.stdout.write('\x1Bc');
} else {
const messages = chatHistory.map(([role, content]) => ({role, content}))
messages.push({role: 'user', content: userInput})
spinner.start();
//Call API with user input
const completion = await openai.createChatCompletion({
model: 'gpt-3.5-turbo',
messages: messages
})
spinner.stop()
//Get completion text
const completionText = completion.data.choices[0].message.content;
if (userInput.toLowerCase() === 'exit') {
console.log(colors.white('\nAI: ' + completionText));
return;
}
console.log(colors.white('\nAI: ' + completionText));
//Update history with user input and chat response
chatHistory.push(['user', userInput])
chatHistory.push(['assistant', completionText])
}
} catch (error) {
console.log(colors.red(error))
}
}
}
main();