-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (36 loc) · 1.06 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
import OpenAI from "openai";
import dotenv from "dotenv";
dotenv.config();
const apiKey = process.argv[2];
const message = process.argv[3];
if (!apiKey || !message) {
console.error("Usage: node index.js <API_KEY> <MESSAGE>");
process.exit(1);
}
const openai = new OpenAI({
apiKey: apiKey,
});
async function getChatGPTResponse(message) {
const completion = await openai.chat.completions.create({
model: "gpt-4",
messages: [{
role: "system",
content: "You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.",
},
{
role: "user",
content: message,
},
],
});
return completion.choices[0].message.content;
}
// Example usage
(async() => {
try {
const response = await getChatGPTResponse(message);
console.log("##" + JSON.stringify({ output_message: response }) + "##");
} catch (error) {
console.error("##" + JSON.stringify({ error: error }) + "##");
}
})();