forked from juliendorra/esquisse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpt.js
56 lines (44 loc) · 1.37 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import "https://deno.land/x/dotenv/load.ts";
const apiKey = Deno.env.get("OPENAI_API_KEY");
if (!apiKey) {
throw new Error("missing OPEN_API_KEY environment variable");
}
const apiUrl = 'https://api.openai.com/v1/chat/completions';
export async function callGPT(data, transform, qualityEnabled = false) {
let model = qualityEnabled ? "gpt-4" : "gpt-3.5-turbo";
console.log("calling ChatGPT using model: ", model)
const payload = {
model: model,
messages: [
{
role: "system",
content: "Answer in 500 characters or less.",
},
{
role: "user",
content: data + " " + transform,
},
],
temperature: 0.2,
max_tokens: 600,
};
const options = {
'method': 'POST',
'headers': {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + apiKey,
},
'body': JSON.stringify(payload),
};
try {
const response = await fetch(apiUrl, options);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.json();
return result.choices[0].message.content.trim();
} catch (error) {
console.error(`Fetch failed: ${error}`);
return ""
}
}