-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpt.ts
91 lines (76 loc) · 2.1 KB
/
gpt.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
declare type GPTEngines = "davinci" | "ada" | "babbage" | "curie";
declare interface GPTRequest {
prompt: string;
max_tokens: number;
temperature: number;
top_p?: number;
n: number;
echo?: boolean;
stop?: string[] | null;
}
declare interface GPTResult {
id: string;
object: string;
model: string;
choices: GPTChoice[];
}
declare interface GPTChoice {
text: string;
index: number;
finish_reason: string;
temperature?: number;
}
const callGPT3 = (engine: GPTEngines = "davinci", req: GPTRequest) => {
if (req.max_tokens > 1000 || req.max_tokens < 0 || req?.n > 15) {
throw new Error("Incorrect GPT3 request");
}
if (req.stop?.[0] === "") {
req.stop = null;
}
const data: GPTRequest = req;
const url = `https://api.openai.com/v1/engines/${engine}/completions`;
const result = UrlFetchApp.fetch(url, {
method: "post",
payload: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${getApiKey()}`,
},
});
const parsed = JSON.parse(result.getContentText()) as GPTResult;
return parsed.choices;
};
const gpt = async (
selection: { text: string; lastElem: GoogleAppsScript.Document.Element },
template = DIS_ENG,
req: GPTRequest = {
prompt: template.text.replace("{{INPUT}}", selection.text),
max_tokens: 120,
temperature: 0.1,
stop: ["###"],
n: 1,
}
) => {
try {
const [result] = callGPT3("davinci", req);
if (!result || !result?.text) {
throw new Error("Missing GPT3 reply");
}
const gptTextOutput = result.text;
Logger.log(gptTextOutput);
//createPrompt((gptTextOutput));
const gptText = gptTextOutput
.split(template.end)[1]
.replace(/\n/g, "")
.trim();
const body = doc.getBody();
const idx = body.getChildIndex(selection.lastElem.getParent());
const p = body.insertParagraph(idx + 1, "\n" + gptText + "\n");
p.setAttributes({
[DocumentApp.Attribute.FOREGROUND_COLOR]: "#5465FF",
[DocumentApp.Attribute.FONT_FAMILY]: "Roboto Mono",
});
} catch (error) {
throw new Error(error);
}
};