-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemini_sheets_api.js
More file actions
425 lines (388 loc) · 13.4 KB
/
Copy pathgemini_sheets_api.js
File metadata and controls
425 lines (388 loc) · 13.4 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
const { GoogleAuth } = require("google-auth-library");
const { google } = require("googleapis");
const { VertexAI } = require("@google-cloud/vertexai");
const readlineSync = require("readline-sync");
const path = require("path");
const fs = require("fs");
// --- Configuration ---
const SERVICE_ACCOUNT_FILE = path.join(__dirname, "credentials.json");
const SPREADSHEET_ID = "1uyWTJ6sWMcoSu7o4bAiJ0eXWauJKC0car3yyWbEk3Vc"; // <--- IMPORTANT: REPLACE THIS!
const WORKSHEET_NAME = "Sheet1"; // The exact name of your sheet within the spreadsheet
// --- Vertex AI / Gemini Configuration ---
const PROJECT_ID = "sheet-gemini-poc"; // <--- IMPORTANT: REPLACE WITH YOUR GCP PROJECT ID
const LOCATION = "asia-south1"; // Or your preferred region for Vertex AI
const MODEL_NAME = "gemini-1.5-flash";
// Scopes for Google Sheets API (read/write access)
const SHEETS_SCOPES = ["https://www.googleapis.com/auth/spreadsheets"];
class SheetsService {
constructor(authClient) {
this.sheets = google.sheets({ version: "v4", auth: authClient });
this.spreadsheetId = SPREADSHEET_ID;
this.worksheetName = WORKSHEET_NAME;
}
async getSheetInstance() {
try {
const response = await this.sheets.spreadsheets.get({
spreadsheetId: this.spreadsheetId,
});
console.log(
`Connected to Spreadsheet: ${response.data.properties.title}`
);
return true;
} catch (err) {
console.error(
`Error connecting to spreadsheet ${this.spreadsheetId}:`,
err.message
);
if (err.code === 403) {
console.error(
"Please ensure the service account has 'Editor' access to this Google Sheet."
);
} else if (err.code === 404) {
console.error("Spreadsheet not found. Check the ID.");
}
process.exit(1);
}
}
async readInventory() {
console.log("\n--- Reading Current Inventory ---");
try {
const range = `${this.worksheetName}!A:D`;
const response = await this.sheets.spreadsheets.values.get({
spreadsheetId: this.spreadsheetId,
range: range,
});
const rows = response.data.values;
if (!rows || rows.length === 0) {
return "No data found in inventory.";
}
const headers = rows[0];
const data = rows.slice(1).map((row) => {
const item = {};
headers.forEach((header, index) => {
item[header] = row[index] || "";
});
return item;
});
let output = "Current Inventory:\n";
data.forEach((item) => {
output += `- Item: ${item["Name"]}, Quantity: ${item["Quantity"]}, Price: $${item["Price"]}, Last Updated: ${item["Last Updated"]}\n`;
});
return output;
} catch (err) {
console.error("The API returned an error reading data:", err.message);
return "Failed to read inventory.";
}
}
async addRow(itemName, quantity, price) {
console.log(`\n--- Attempting to add new item: ${itemName} ---`);
try {
const currentTime = new Date()
.toLocaleString("en-CA", {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
})
.replace(/,/, "");
const values = [itemName, quantity, price, currentTime];
const range = this.worksheetName;
await this.sheets.spreadsheets.values.append({
spreadsheetId: this.spreadsheetId,
range: range,
valueInputOption: "USER_ENTERED",
resource: {
values: [values],
},
});
return `Successfully added '${itemName}' to the inventory.`;
} catch (err) {
console.error("The API returned an error adding row:", err.message);
return `Failed to add '${itemName}'.`;
}
}
async updateItemQuantity(itemName, newQuantity) {
console.log(
`\n--- Attempting to update item: ${itemName} to quantity ${newQuantity} ---`
);
try {
// First, get all values to find the row index
const allValuesResponse = await this.sheets.spreadsheets.values.get({
spreadsheetId: this.spreadsheetId,
range: `${this.worksheetName}!A:D`, // Get enough columns to find item name and quantity
});
const rows = allValuesResponse.data.values;
if (!rows || rows.length === 0) {
return `Item '${itemName}' not found (sheet is empty).`;
}
const headers = rows[0];
const itemColIndex = headers.indexOf("Name");
const quantityColIndex = headers.indexOf("Quantity");
const lastUpdatedColIndex = headers.indexOf("Last Updated");
if (
itemColIndex === -1 ||
quantityColIndex === -1 ||
lastUpdatedColIndex === -1
) {
return "Error: Missing expected column (Item Name, Quantity, or Last Updated) in your sheet headers. Please ensure the sheet has these exact headers.";
}
let rowIndex = -1;
for (let i = 1; i < rows.length; i++) {
// Start from 1 to skip headers
if (rows[i][itemColIndex]?.toLowerCase() === itemName.toLowerCase()) {
rowIndex = i;
break;
}
}
if (rowIndex === -1) {
return `Item '${itemName}' not found in inventory.`;
}
const currentRowNumber = rowIndex + 1; // Google Sheets is 1-indexed
// Batch update for Quantity and Last Updated timestamp
const currentTime = new Date()
.toLocaleString("en-CA", {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
})
.replace(/,/, "");
const requests = [
{
range: `${this.worksheetName}!${String.fromCharCode(
65 + quantityColIndex
)}${currentRowNumber}`,
values: [[newQuantity]],
},
{
range: `${this.worksheetName}!${String.fromCharCode(
65 + lastUpdatedColIndex
)}${currentRowNumber}`,
values: [[currentTime]],
},
];
await this.sheets.spreadsheets.values.batchUpdate({
spreadsheetId: this.spreadsheetId,
resource: {
valueInputOption: "USER_ENTERED",
data: requests,
},
});
return `Updated quantity of '${itemName}' to ${newQuantity}.`;
} catch (err) {
console.error("The API returned an error updating item:", err.message);
return `Failed to update '${itemName}'.`;
}
}
}
// --- Main Application Logic ---
async function main() {
// --- Authenticate Google Sheets API ---
let authClient;
try {
const auth = new GoogleAuth({
keyFile: SERVICE_ACCOUNT_FILE,
scopes: SHEETS_SCOPES,
});
authClient = await auth.getClient();
console.log("Sheets API Authentication successful!");
} catch (err) {
console.error("Sheets API Authentication failed:", err);
console.error(
"Please ensure the service account has appropriate roles (e.g., Editor) and credentials.json is valid."
);
process.exit(1);
}
const sheetsService = new SheetsService(authClient);
await sheetsService.getSheetInstance(); // Verify connection to spreadsheet
// --- Initialize Vertex AI for Gemini ---
const vertexAI = new VertexAI({ project: PROJECT_ID, location: LOCATION });
const model = vertexAI.getGenerativeModel({ model: MODEL_NAME });
console.log("Vertex AI Authentication successful!");
console.log(`\n--- Gemini as Sheets Interface (${MODEL_NAME}) ---`);
console.log(
"You can now tell me what to do with your 'Inventory PoC' spreadsheet."
);
console.log("Try commands like:");
console.log("- 'Read the inventory'");
console.log("- 'Add a new item called Laptop, quantity 5, price 1200'");
console.log("- 'Update the quantity of Mouse to 55'");
console.log("- 'Exit' to quit.\n");
// --- Define Tools for Gemini ---
const tools = [
{
functionDeclarations: [
{
name: "readInventory",
description:
"Reads and lists all items currently in the inventory spreadsheet.",
parameters: {
type: "object",
properties: {}, // No parameters needed
},
},
{
name: "addRow",
description:
"Adds a new item with its quantity and price to the inventory spreadsheet.",
parameters: {
type: "object",
properties: {
itemName: {
type: "string",
description: "The name of the item to add.",
},
quantity: {
type: "number",
description: "The quantity of the new item.",
},
price: {
type: "number",
description: "The price of a single unit of the new item.",
},
},
required: ["itemName", "quantity", "price"],
},
},
{
name: "updateItemQuantity",
description:
"Updates the quantity of an existing item in the inventory spreadsheet.",
parameters: {
type: "object",
properties: {
itemName: {
type: "string",
description:
"The name of the item whose quantity needs to be updated.",
},
newQuantity: {
type: "number",
description: "The new quantity for the item.",
},
},
required: ["itemName", "newQuantity"],
},
},
],
},
];
const chat = model.startChat({ tools: tools });
// --- Function to handle tool calls ---
async function callTool(functionCall) {
const { name, args } = functionCall;
console.log(
`\nGemini requested to call function: ${name} with args:`,
args
);
if (sheetsService[name] && typeof sheetsService[name] === "function") {
try {
const result = await sheetsService[name](...Object.values(args));
return {
functionResponse: {
name: name,
response: { content: result },
},
};
} catch (error) {
console.error(`Error executing tool ${name}:`, error);
return {
functionResponse: {
name: name,
response: {
content: `Error: Failed to execute tool ${name}. ${error.message}`,
},
},
};
}
} else {
console.error(`Error: Function ${name} not found in sheetsService.`);
return {
functionResponse: {
name: name,
response: { content: `Error: Function ${name} is not implemented.` },
},
};
}
}
// --- Conversational Loop ---
while (true) {
const prompt = readlineSync.question("You: ");
if (prompt.toLowerCase() === "exit") {
console.log("Exiting chat.");
break;
}
try {
const result = await chat.sendMessage(prompt);
const response = result.response;
if (
response.candidates &&
response.candidates[0] &&
response.candidates[0].content &&
response.candidates[0].content.parts &&
response.candidates[0].content.parts[0] &&
response.candidates[0].content.parts[0].functionCall
) {
// Gemini wants to call a tool
const functionCall =
response.candidates[0].content.parts[0].functionCall;
// Call the tool and get its response
const toolResponse = await callTool(functionCall);
// Send the tool's result back to Gemini to get the final human-readable answer
const apiResponse = await chat.sendMessage([toolResponse]);
// Access the text from the final API response
if (
apiResponse.response.candidates &&
apiResponse.response.candidates[0] &&
apiResponse.response.candidates[0].content &&
apiResponse.response.candidates[0].content.parts &&
apiResponse.response.candidates[0].content.parts[0] &&
apiResponse.response.candidates[0].content.parts[0].text
) {
console.log(
"Gemini:",
apiResponse?.response?.candidates?.[0]?.content?.parts?.[0].text
);
} else {
console.log(
"Gemini: (after tool call) No final text response or unexpected format."
);
console.log(
"Gemini API Response Structure (after tool call):",
JSON.stringify(apiResponse.response, null, 2)
);
}
} else if (
response.candidates &&
response.candidates[0] &&
response.candidates[0].content &&
response.candidates[0].content.parts &&
response.candidates[0].content.parts[0] &&
response.candidates[0].content.parts[0].text
) {
console.log("---- No Function Calls ----");
console.log("Gemini:", response.candidates[0].content.parts[0].text);
} else {
console.log(
"Gemini: No specific response or tool call generated, or text content is not in the expected format."
);
console.log(
"Gemini Raw Response (first call - no tool):",
JSON.stringify(response, null, 2)
);
}
} catch (error) {
console.error("Error communicating with Gemini:", error);
console.log(
"Gemini: I encountered an error. Please try rephrasing your request."
);
}
}
}
main().catch(console.error);