-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathGemini_Functions.js
More file actions
179 lines (153 loc) · 5.02 KB
/
Copy pathGemini_Functions.js
File metadata and controls
179 lines (153 loc) · 5.02 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// appsscript.json
{
"timeZone": "Europe/Dublin",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"oauthScopes": [
"https://www.googleapis.com/auth/spreadsheets.currentonly",
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/cloud-platform"
],
"runtimeVersion": "V8"
}
// code.js
project = 'REPLACE WITH PROJECT ID';
function auth() {
cache = CacheService.getUserCache();
token = ScriptApp.getOAuthToken();
cache.put("token", token);
}
function askGemini(inputText) {
cache = CacheService.getUserCache();
token = cache.get("token");
if (token == "") return "ERROR";
Logger.log(`Token = ${token}`);
url = `https://us-central1-aiplatform.googleapis.com/v1/projects/${project}/locations/us-central1/publishers/google/models/gemini-1.0-pro:generateContent`
data = {
contents: {
role: "USER",
parts: { "text": inputText }
},
generation_config: {
temperature: 0.3,
topP: 1,
maxOutputTokens: 256
}
}
const options = {
method: "post",
contentType: 'application/json',
headers: {
Authorization: `Bearer ${token}`,
},
payload: JSON.stringify(data)
};
const response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() == 200) {
json = JSON.parse(response.getContentText());
answer = json.candidates[0].content.parts[0].text;
return answer;
}
return "ERROR";
}
function translate(inputText) {
cache = CacheService.getUserCache();
token = cache.get("token");
if (token == "") return "ERROR";
Logger.log(`Token = ${token}`);
url = `https://us-central1-aiplatform.googleapis.com/v1/projects/${project}/locations/us-central1/publishers/google/models/gemini-1.0-pro:generateContent`
textWithInstruction = "translate to Spanish: " + inputText;
data = {
contents: {
role: "USER",
parts: { "text": textWithInstruction }
},
generation_config: {
temperature: 0.3,
topP: 1,
maxOutputTokens: 256
}
}
const options = {
method: "post",
contentType: 'application/json',
headers: {
Authorization: `Bearer ${token}`,
},
payload: JSON.stringify(data)
};
const response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() == 200) {
json = JSON.parse(response.getContentText());
answer = json.candidates[0].content.parts[0].text;
return answer;
}
return "ERROR";
}
function report(inputText) {
cache = CacheService.getUserCache();
token = cache.get("token");
if (token == "") return "ERROR";
Logger.log(`Token = ${token}`);
url = `https://us-central1-aiplatform.googleapis.com/v1/projects/${project}/locations/us-central1/publishers/google/models/gemini-1.0-pro:generateContent`
prompt = "Role: You are a financial analyst and you are required to summarise the key insights of given numerical tables.Task: Step 1: List important, but no more than five, highlights from the figures provided in the given table.Step 2: Write a paragraph about the main movers of net income comparing each year from the figures provided. (For a dataset with three years of figures compare Year 1 to Year 2 and Year 2 to Year 3) Further Instructions: Please write in a professional and business-neutral tone similar to the financial times.The summary should only be based on the information presented in the table and only contain facts form that table."
text = prompt + inputText;
data = {
contents: {
role: "USER",
parts: { "text": text }
},
generation_config: {
temperature: 0.3,
topP: 1,
maxOutputTokens: 2000
}
}
const options = {
method: "post",
contentType: 'application/json',
headers: {
Authorization: `Bearer ${token}`,
},
payload: JSON.stringify(data)
};
const response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() == 200) {
json = JSON.parse(response.getContentText());
answer = json.candidates[0].content.parts[0].text;
return answer;
}
return "ERROR";
}
function reported(cellRange) {
const sheet = SpreadsheetApp.getActiveSheet();
// Get the values from the cell range
const values = sheet.getRange(cellRange).getValues();
// Check if the range is empty or doesn't have headers
if (!values || !values.length || !values[0].length) {
return "ERROR: Empty range or missing headers";
}
// Build the markdown table header row
let markdownTable = "|";
for (const header of values[0]) {
markdownTable += ` ${header} |`;
}
markdownTable += "\n";
// Add a separator line
markdownTable += "|";
for (let i = 0; i < values[0].length; i++) {
markdownTable += " --- |";
}
markdownTable += "\n";
// Build the remaining rows
for (let i = 1; i < values.length; i++) {
markdownTable += "|";
for (const value of values[i]) {
markdownTable += ` ${value} |`;
}
markdownTable += "\n";
}
// Call the original translate function with the markdown table
return report(markdownTable);
}