-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchatgpt.js
40 lines (35 loc) · 1.37 KB
/
chatgpt.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
const { OpenAI } = require("openai");
const { readConfig } = require("./filehandler");
require('dotenv').config({
path: require('path').resolve(__dirname, './.env')
});
const jsonConfig = readConfig();
const config = {
apiKey: process.env.OPENAI_API_KEY || jsonConfig.OPENAI_API_KEY,
};
const openai = new OpenAI(config);
const prompt = async (messages) => {
if(!process.env.OPENAI_API_KEY && !jsonConfig.OPENAI_API_KEY){
throw new Error('No OPEN_API_KEY detected, run bashgpt with -k parameter with your openai key to set keys or change keys')
}
try {
const stream = await openai.chat.completions.create({
model: jsonConfig.GPT_MODEL||"gpt-3.5-turbo",
messages: messages,
stream:true,
});
let accumdata = "";
let chunkdata = "";
process.stdout.write('\x1b[1m\x1b[35m'+"AI@chatgpt: "+'\x1b[0m');
for await (const chunk of stream){
chunkdata = chunk.choices[0]?.delta?.content || '';
process.stdout.write(chunkdata);
accumdata = accumdata.concat(chunkdata);
}
process.stdout.write('\n');
return accumdata;
} catch (error) {
console.error('If your openai key doesnt work and you want to change it use bashgpt with -k option along with new openai key to change it\n', error.messages);
}
}
module.exports = prompt;