-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenAI_Google_Slides.js
51 lines (44 loc) · 1.46 KB
/
OpenAI_Google_Slides.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
// API KEY and variables
const apiKey = "";
const model = "text-davinci-003"
const temperature = 0.2
const maxTokens = 200
// UI MENU
function onOpen() {
SlidesApp.getUi().createMenu("ChatGPT")
.addItem("Completion", "textCompletion")
.addToUi();
}
function createRequestBody(prompt) {
const requestBody = {
"model": model,
"prompt": prompt,
"temperature": temperature,
"max_tokens": maxTokens,
};
return requestBody;
}
function createRequestPayload(requestBody) {
const requestPayload = {
"method": "POST",
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer "+apiKey
},
"payload": JSON.stringify(requestBody)
}
return requestPayload;
}
// Completion
function textCompletion() {
const presentation = SlidesApp.getActivePresentation();
const selectedText = presentation.getSelection().getCurrentPage().getShapes()[0].getText().asString();
const slide = presentation.getSelection().getCurrentPage();
const requestBody = createRequestBody(selectedText)
const requestPayload = createRequestPayload(requestBody)
const response = UrlFetchApp.fetch("https://api.openai.com/v1/completions", requestPayload);
const responseText = response.getContentText();
const json = JSON.parse(responseText);
const generatedText = json['choices'][0]['text'];
slide.insertTextBox(generatedText.toString());
}