forked from najeebkhan12/chatgpt-nodejs-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
29 lines (21 loc) · 774 Bytes
/
index.js
File metadata and controls
29 lines (21 loc) · 774 Bytes
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
const openai = require('openai');
// require('dotenv').config()
// console.log(process.env)
// // Define your OpenAI API key
// const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
// Text to send to ChatGPT for completion
const prompt = "Once upon a time, ";
// Function to send request to ChatGPT API
async function Chat_Completion(prompt) {
try {
const completion = await openai.chat.completions.create({
messages: [{ role: "system", content: "You are a helpful assistant." }],
model: "gpt-3.5-turbo",
});
console.log(choices[0]);
} catch (error) {
console.error("Error generating response:", error.response ? error.response.data : error.message);
}
}
// Call the function to generate a ChatGPT response
Chat_Completion();