1
+ /*
2
+ Copyright 2024 Google LLC
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ https://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ */
16
+
17
+ /**
18
+ * Packages prompt and necessary settings, then sends a request to the
19
+ * Generative Language API. Returns the text string response, extracted from the
20
+ * Gemini AI response object.
21
+ *
22
+ * @param {string } prompt String representing the prompt for Gemini AI call.
23
+ * @return {string } Result of Gemini AI in string format.
24
+ */
25
+ function getAiSummary ( prompt ) {
26
+ const data = {
27
+ "contents" : [ {
28
+ "parts" : [ {
29
+ "text" : prompt
30
+ } ]
31
+ } ] ,
32
+ "generationConfig" : {
33
+ "temperature" : 0.2 ,
34
+ "topK" : 1 ,
35
+ "topP" : 1 ,
36
+ "maxOutputTokens" : 2048 ,
37
+ "stopSequences" : [ ]
38
+ } ,
39
+ "safetySettings" : [
40
+ {
41
+ "category" : "HARM_CATEGORY_HARASSMENT" ,
42
+ "threshold" : "BLOCK_NONE"
43
+ } ,
44
+ {
45
+ "category" : "HARM_CATEGORY_HATE_SPEECH" ,
46
+ "threshold" : "BLOCK_NONE"
47
+ } ,
48
+ {
49
+ "category" : "HARM_CATEGORY_SEXUALLY_EXPLICIT" ,
50
+ "threshold" : "BLOCK_NONE"
51
+ } ,
52
+ {
53
+ "category" : "HARM_CATEGORY_DANGEROUS_CONTENT" ,
54
+ "threshold" : "BLOCK_NONE"
55
+ }
56
+ ]
57
+ } ;
58
+ const options = {
59
+ 'method' : 'post' ,
60
+ 'contentType' : 'application/json' ,
61
+ 'payload' : JSON . stringify ( data ) // Convert the JavaScript object to a JSON string.
62
+ } ;
63
+
64
+ const apiKey = PropertiesService . getScriptProperties ( ) . getProperty ( 'api_key' ) ;
65
+ let response = UrlFetchApp . fetch ( 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=' + apiKey , options ) ;
66
+
67
+ const payload = JSON . parse ( response . getContentText ( ) ) ;
68
+ const text = payload . candidates [ 0 ] . content . parts [ 0 ] . text ;
69
+
70
+ return text ;
71
+
72
+ }
0 commit comments