-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
200 lines (194 loc) · 8.1 KB
/
main.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* GitHub https://github.com/tanaikech/ImageBotApp<br>
*
* Application name. This name is used in the notification email.
* @type {string}
* @const {string}
* @readonly
*/
var appName = "ImageBotApp";
// Please use this when you want to directly copy and paste these scripts ahd HTML instead of the library.
const ImageBotApp = { main };
/**
* ### Description
* Main of image bot.
*
* @param {Object} obj Object for running image bot.
* @returns {(HtmlService.HtmlOutput|Object))} HtmlService.HtmlOutput or object.
*/
function main(obj) {
try {
if (obj.run) {
return functionList_[obj.run](obj);
}
return HtmlService.createHtmlOutputFromFile("index");
} catch ({ stack }) {
throw new Error(stack);
}
}
/**
* ### Description
* Function listLibrary name
*
* @type {Object}
* @const {Object}
* @readonly
*/
const functionList_ = {
doGemini: doGemini_,
saveFiles: saveFiles_,
}
/**
* ### Description
* Save uploaded image data.
*
* @param {Object} object Object for saving image data.
* @returns {Object} Array
*/
function saveFiles_(object) {
const { obj, folderId, newDocumentName } = object;
if (obj.length == 0) {
return { ret: "No files" };
}
const folder = DriveApp.getFolderById(folderId);
const headers = { authorization: "Bearer " + ScriptApp.getOAuthToken() };
const { requests, ret } = obj.reduce((o, e) => {
const file = folder.createFile(Utilities.newBlob(...e));
const fileId = file.getId();
const base64 = Utilities.base64Encode(UrlFetchApp.fetch(`https://drive.google.com/thumbnail?sz=w1000&id=${fileId}`, { headers }).getContent());
const url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision:generateContent";
const payload = { contents: [{ parts: [{ text: "Explain about this picture within 50 words." }, { inline_data: { mime_type: "image/png", data: base64 } }] }] };
const options = { payload: JSON.stringify(payload), contentType: "application/json", headers };
const res = UrlFetchApp.fetch(url, options);
const obj = JSON.parse(res.getContentText());
if (obj.candidates && obj.candidates[0].content && obj.candidates[0].content.parts) {
const description = obj.candidates[0].content.parts[0].text;
o.ret.push({ fileUrl: file.getUrl(), dataUrl: `data:image/png;base64,${base64}`, description });
o.requests.push({
parent: newDocumentName.name,
chunk: {
data: { stringValue: description.trim() },
customMetadata: [{ key: "fileId", stringValue: fileId }, { key: "url", stringValue: file.getUrl() }]
}
});
}
return o;
}, { requests: [], ret: [] });
if (requests.length > 0) {
CorporaApp.setChunks(newDocumentName.name, { requests });
}
const pseudoModel = { parts: [{ text: "URLs and descriptions of the uploaded image files are as follows.\n" + ret.map(({ fileUrl, description }) => `URL is ${fileUrl}. Description of ${fileUrl} is ${description}.`).join(",") }], role: "model" };
return { ret, pseudoModel };
}
/**
* ### Description
* Object including the functions of function calling.
*
* @param {Object} object Object for using function calling.
* @returns {Object} Object
*/
function functions_({ newDocumentName }) {
return {
params_: {
searchImages: {
description: "Search images and descriptions of images from the image stock. Get images and descriptions of images from the image stock by searching.",
parameters: {
type: "object",
properties: {
searchText: {
type: "string",
description: "Search text for searching images and descriptions of the images from the image stock. Texts. Words."
},
numberOfImages: {
type: "number",
description: "Number of output images. Default number is 2."
},
},
required: ["searchText", "numberOfImages"]
}
},
uploadImages: {
description: "Upload images. Upload image files. Make users upload image files. Upload a single image file and multiple image files. Add image and file.",
},
},
searchImages: function ({ searchText, numberOfImages = 2 }) {
const res = CorporaApp.searchQueryFromDocument(newDocumentName.name, { query: searchText, resultsCount: numberOfImages });
const obj = JSON.parse(res.getContentText());
const data = obj.relevantChunks.map(({ chunk }) => ({
chunk, ...chunk.customMetadata.reduce((o, { key, stringValue }) => (o[key] = stringValue, o), {})
}));
const text = "Searched images are as follows. " + data.map(({ url, chunk: { data: { stringValue } } }) => `URL and description of searched image are ${url} and ${stringValue}.`).join("\n");
const pseudoModel = { parts: [{ text }], role: "model" };
return { stopAtFunction: true, data, pseudoModel };
},
uploadImages: function () {
return { uploadImage: HtmlService.createHtmlOutputFromFile("uploadImages").getContent() };
}
};
}
/**
* ### Description
* Request Gemini.
*
* @param {Object} object Object for requesting Gemini.
* @returns {Object} Object
*/
function doGemini_(object) {
const { text, history } = object;
const functions = functions_(object);
const function_declarations = Object.keys(functions).flatMap(k => (k != "params_" ? { name: k, description: functions.params_[k].description, parameters: functions.params_[k].parameters } : []));
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent`;
const contents = [{ parts: [{ text }], role: "user" }];
let check = true;
let useFunction = false;
const token = ScriptApp.getOAuthToken();
const results = [];
do {
const payload = { contents, tools: [{ function_declarations }] };
const options = { payload: JSON.stringify(payload), contentType: "application/json", headers: { authorization: "Bearer " + token }, muteHttpExceptions: true };
const res = UrlFetchApp.fetch(url, options);
if (res.getResponseCode() != 200) {
throw new Error(res.getContentText());
}
const { candidates } = JSON.parse(res.getContentText());
if (!candidates[0].content?.parts) {
results.push(candidates[0]);
break
}
const parts = candidates[0].content?.parts;
contents.push({ parts: parts.slice(), role: "model" });
check = parts.find(o => o.hasOwnProperty("functionCall"));
if (check) {
const functionName = check.functionCall.name;
const res2 = functions[functionName](check.functionCall.args || null);
contents.push({ parts: [{ functionResponse: { name: functionName, response: { name: functionName, content: res2 } } }], role: "function" });
parts.push({ functionResponse: res2 });
useFunction = true;
if (res2.stopAtFunction) {
check = false;
if (res2.pseudoModel) {
contents.push(res2.pseudoModel);
}
}
}
results.push(...parts);
} while (check);
const newHistory = [...history, ...contents];
const lastResponse = useFunction ? results.reverse().find(r => r.functionResponse) : results.pop();
if (lastResponse.functionResponse) {
if (lastResponse.functionResponse.uploadImage) {
return { response: "uploadImage", value: lastResponse.functionResponse.uploadImage, history: newHistory };
} else if (lastResponse.functionResponse.data) {
const urls = lastResponse.functionResponse.data.map(({ url }) => url);
const dataUrls = lastResponse.functionResponse.data.map(({ fileId }) => {
const file = DriveApp.getFileById(fileId);
const base64 = Utilities.base64Encode(file.getBlob().getBytes());
return `data:${file.getMimeType()};base64,${base64}`;
});
return { response: "url", value: urls, dataUrls, history: newHistory };
}
} else if (lastResponse.text) {
return { response: "text", value: lastResponse.text, history: newHistory };
}
return { response: "text", value: "No response.", history: newHistory };
}