-
Notifications
You must be signed in to change notification settings - Fork 15
/
appscript.js
80 lines (68 loc) · 2.64 KB
/
appscript.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Constants
const API_KEY = "sk-Lixxxxx";
const MODEL_TYPE = "gpt-3.5-turbo"; //chatGPT model
// Creates a custom menu in Google Slides
function onOpen() {
SlidesApp.getUi().createMenu("ChatGPT")
.addItem("Generate Prompt", "generatePrompt")
.addItem("Generate Tweets", "generateTweets")
.addToUi();
}
// Generates prompt based on the selected text and adds it to a slide
function generateTweets() {
const presentation = SlidesApp.getActivePresentation();
const selectedText = presentation.getSelection().getCurrentPage().getShapes()[0].getText().asString();
const slide = presentation.getSelection().getCurrentPage();
const prompt = "Generate 3 Tweets on " + selectedText;
const temperature = 0;
const maxTokens = 2060;
const requestBody = {
model: MODEL_TYPE,
messages: [{role: "user", content: prompt}],
temperature,
max_tokens: maxTokens,
};
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + API_KEY,
},
payload: JSON.stringify(requestBody),
};
const response = UrlFetchApp.fetch("https://api.openai.com/v1/chat/completions", requestOptions);
const responseText = response.getContentText();
const json = JSON.parse(responseText);
const generatedText = json['choices'][0]['message']['content'];
Logger.log(generatedText);
slide.insertTextBox(generatedText.toString());
}
// Generates prompt based on the selected text and adds it to a slide
function generatePrompt() {
const presentation = SlidesApp.getActivePresentation();
const selectedText = presentation.getSelection().getCurrentPage().getShapes()[0].getText().asString();
const slide = presentation.getSelection().getCurrentPage();
const prompt = "Generate 3 points on " + selectedText;
const temperature = 0;
const maxTokens = 2060;
const requestBody = {
model: MODEL_TYPE,
messages: [{role: "user", content: prompt}],
temperature,
max_tokens: maxTokens,
};
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + API_KEY,
},
payload: JSON.stringify(requestBody),
};
const response = UrlFetchApp.fetch("https://api.openai.com/v1/chat/completions", requestOptions);
const responseText = response.getContentText();
const json = JSON.parse(responseText);
const generatedText = json['choices'][0]['message']['content'];
Logger.log(generatedText);
slide.insertTextBox(generatedText.toString());
}