-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathBlogSummaryPaLM.js
55 lines (35 loc) · 1.17 KB
/
BlogSummaryPaLM.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
function onOpen(){
var ui = DocumentApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Summarize Selected Paragraph', 'summarizeSelectedParagraph')
.addToUi();
}
function DocSummary(paragraph){
var apiKey = "your_api_key";
var apiUrl = "https://generativelanguage.googleapis.com/v1beta2/models/text-bison-001:generateText";
var url = apiUrl + "?key=" + apiKey;
var headers = {
"Content-Type": "application/json"
}
var prompt = {
'text': "Please generate a short summary for :\n" + paragraph
}
var requestBody = {
"prompt": prompt
}
var options = {
"method": "POST",
"headers": headers,
"payload": JSON.stringify(requestBody)
}
var response = UrlFetchApp.fetch(url,options);
var data = JSON.parse(response.getContentText());
return data.candidates[0].output;
}
function summarizeSelectedParagraph(){
var selection = DocumentApp.getActiveDocument().getSelection();
var text = selection.getRangeElements()[0].getElement().getText();
var summary = DocSummary(text);
DocumentApp.getActiveDocument().getBody().appendParagraph("Summary");
DocumentApp.getActiveDocument().getBody().appendParagraph(summary)
}