-
Notifications
You must be signed in to change notification settings - Fork 6
/
openai-handler.js
46 lines (41 loc) · 1.29 KB
/
openai-handler.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
44
45
46
require('dotenv').config();
const fetch = require('node-fetch');
async function postToOpenAIAPI(messages) {
const endpoint = process.env.OPENAI_MODEL === 'gpt-4-turbo-preview'
? 'https://api.openai.com/v1/chat/completions'
: 'https://api.openai.com/v1/completions';
const headers = {
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`,
'Content-Type': 'application/json'
};
let payload = {};
if (process.env.OPENAI_MODEL === 'gpt-4-turbo-preview') {
payload = {
model: 'gpt-4-turbo-preview',
messages: messages.map(m => {
return {role: m.role || 'user', content: m.content};
})
};
} else {
payload = {
prompt: messages.map(message => message.content).join("\n"),
max_tokens: 150
};
}
try {
const response = await fetch(endpoint, {
method: 'POST',
headers: headers,
body: JSON.stringify(payload)
});
if (!response.ok) {
const errorBody = await response.text();
throw new Error(`API request failed with status ${response.status}: ${errorBody}`);
}
return await response.json();
} catch (error) {
console.error(`OpenAI API call error: ${error.message}`);
throw new Error(`Error while calling OpenAI API: ${error.message}`);
}
}
module.exports = { postToOpenAIAPI };