-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiUtils.js
41 lines (35 loc) · 1.26 KB
/
apiUtils.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
const axios = require("axios");
function createPrompt(template, rawNotes) {
const instructions = "Fill in the provided template with the supplied note data. Be concise. Do not add anything beyond what the template specifies. If you are unsure of how to fit something into the template, just make your best guess.";
return `${instructions}\n\nTemplate:\n${template}\n\nNotes:\n${rawNotes}`;
}
async function sendAPIRequest(prompt) {
const endpoint = "https://api.openai.com/v1/chat/completions";
try {
const response = await axios.post(endpoint, {
messages: [{ role: "user", content: prompt }],
model: "gpt-3.5-turbo"
}, {
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.OPENAI_API_KEY}`
}
});
return response;
} catch (error) {
throw new Error("Failed to send API request");
}
}
async function extractFilledTemplate(response) {
try {
const data = response.data;
return data.choices[0].message.content;
} catch (error) {
throw new Error("Failed to extract filled template");
}
}
module.exports = {
createPrompt,
sendAPIRequest,
extractFilledTemplate
};