-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgpt.js
40 lines (33 loc) · 855 Bytes
/
gpt.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 axios = require("axios");
const openai = axios.create({
baseURL: "https://api.openai.com/v1",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
},
});
const createChatCompletion = async (messages, options = {}) => {
try {
const response = await openai.post("/chat/completions", {
model: options.model || "gpt-3.5-turbo",
messages,
...options,
});
return response.data.choices;
} catch (error) {
console.error("Error creating chat completion:", error);
}
};
const gpt = async (message) => {
const options = {
temperature: 0.7,
max_tokens: 250,
};
const choices = await createChatCompletion(message, options);
try {
return choices[0].message.content;
} catch (error) {
return false;
}
};
module.exports = gpt;