-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
46 lines (40 loc) · 1 KB
/
script.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
import { config } from 'dotenv'
config()
import { Configuration, OpenAIApi } from 'openai'
import readline from 'readline'
import chalk from 'chalk'
const openai = new OpenAIApi(
new Configuration({
apiKey: process.env.API_KEY,
})
)
const userInterface = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
// Ask the user for input with this text "User: "
userInterface.setPrompt(chalk.blue('User: '))
userInterface.prompt()
userInterface.on('line', async (input) => {
// exit prompt
if (input === 'exit' || input === 'quit' || input === 'q' || input === 'e') {
userInterface.close()
return
}
const res = await openai.createChatCompletion({
// models: 'gpt-3.5-turbo', 'gpt-4'
model: 'gpt-3.5-turbo',
temperature: 0.7, // 0.0 - 1.0
messages: [
{
role: 'user',
content: input,
},
],
})
console.log(
chalk.magenta('Assistant: '),
chalk.green(res.data.choices[0].message.content)
)
userInterface.prompt()
})