-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_openai.js
34 lines (27 loc) · 1.13 KB
/
test_openai.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
const config = require('./config.json');
const OPENAI_API_KEY = config.OPENAI_API_KEY;
const OPENAI_ENDPOINT = 'https://api.openai.com/v1/chat/completions';
async function main() {
const fetch = await import('node-fetch').then(module => module.default);
async function fetchOpenAIResponse(prompt) {
const response = await fetch(OPENAI_ENDPOINT, {
method: 'POST',
headers: {
'Authorization': `Bearer ${OPENAI_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: "model: "gpt-4-1106-preview",
messages: [{"role": "user", "content": prompt}],
temperature: 0.7
}),
});
const data = await response.json();
return data.choices[0].message.content.trim();
}
const userInput = "Who is the CEO of Tesla?";
const openAIResponse = await fetchOpenAIResponse(userInput);
console.log("User Input:", userInput);
console.log("OpenAI Response:", openAIResponse);
}
main().catch(error => console.error(error));