-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.js
41 lines (33 loc) · 1.09 KB
/
chat.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
const readline = require('readline');
const { OpenAI } = require('openai');
require('dotenv').config();
//console.log(process.env.OPENAI_API_KEY);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
// Assuming the new method is named differently, replace 'createChatCompletion' with the correct method name
async function getResponse(prompt) {
try {
const response = await openai.chat.completions.create({
messages: [{role: "user", content: prompt}],
model: "gpt-3.5-turbo",
});
//console.log(response.choices[0]); // Correct logging to show the complete choice
return response.choices[0].message.content; // Adjusted to correctly access the message content
} catch (error) {
console.error("Error during API call:", error);
return "Failed to fetch response from OpenAI.";
}
}
async function chat() {
rl.question('You: ', async function (input) {
const response = await getResponse(input);
console.log(`AI: ${response}`);
chat();
});
}
chat();