-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpt.js
More file actions
30 lines (26 loc) · 752 Bytes
/
gpt.js
File metadata and controls
30 lines (26 loc) · 752 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
30
const OpenAI = require("openai");
const { CONSTANTS } = require("./constants");
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
// GPT API 호출 함수
const callGPT = async (systemPrompt, userPrompt) => {
const completion = await openai.chat.completions.create({
model: CONSTANTS.GPT_MODEL,
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userPrompt },
],
});
return completion.choices[0].message.content.trim();
};
// JSON 응답 파싱 및 에러 처리
const parseJSONResponse = (content, errorMessage) => {
try {
return JSON.parse(content);
} catch (err) {
throw new Error(errorMessage);
}
};
module.exports = {
callGPT,
parseJSONResponse,
};