-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
56 lines (49 loc) · 1.17 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
const axios = require('axios');
const readline = require('readline');
require('dotenv').config()
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const chatgptURL = process.env.OPENAI_END_POINT
const prompt = process.env.DEFAULT_USER_PROMPT
const apiKey = process.env.OPENAI_KEY
const chatgpt_ans = process.env.DEFAULT_RESPONSE_PROMT
const model_name = process.env.OPENAI_MODEL
console.log('Welcome to ChatGPT!');
console.log('Start typing to interact with the ChatGPT server.');
rl.setPrompt(prompt);
rl.prompt();
function closeIt(){
console.log('Goodbye!');
process.exit(0);
}
rl.on('line', line => {
if(line=='exit'){
closeIt();
}
axios
.post(chatgptURL, {
prompt: line,
model: model_name,
max_tokens: 1024,
temperature: 0
}, {
headers: {
'Content-Type' : 'application/json',
'Authorization': `Bearer ${apiKey}`
}
})
.then(res => {
console.log(`${chatgpt_ans} ${res.data.choices[0].text}`);
rl.prompt();
})
.catch(err => {
console.error(err);
rl.prompt();
});
});
rl.on('close', () => {
console.log('Goodbye!');
process.exit(0);
});