-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathBlogSummaryDoc.gs
56 lines (38 loc) · 1.29 KB
/
BlogSummaryDoc.gs
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
function onOpen() {
var ui = DocumentApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Summarize selected paragraph', 'summarizeSelectedParagraph')
.addToUi();
}
var API_key = "your_api_key";
function generateSummary(paragraph){
var Model_ID = "text-davinci-002";
var maxtokens = 200;
var temperature = 0.7;
var payload = {
'model': Model_ID,
'prompt': 'Please generate a short summary for:\n' +paragraph,
'temperature': temperature,
'max_tokens': maxtokens,
"presence_penalty": 0.5,
"frequency_penalty": 0.5
}
var options = {
"method": "post",
"headers": {
"Content-Type": "application/json",
"Authorization" : "Bearer " + API_key
},
"payload": JSON.stringify(payload)
};
var response = UrlFetchApp.fetch("https://api.openai.com/v1/completions",options);
var summary = JSON.parse(response.getContentText());
return summary.choices[0].text.trim();
}
function summarizeSelectedParagraph() {
var selection = DocumentApp.getActiveDocument().getSelection();
var text = selection.getRangeElements()[0].getElement().getText();
var summary = generateSummary(text);
DocumentApp.getActiveDocument().getBody().appendParagraph("Summary")
DocumentApp.getActiveDocument().getBody().appendParagraph(summary);
}