From 737aa449d1d038c574670c71094c1656b6c9d7a7 Mon Sep 17 00:00:00 2001 From: louis rubeus Date: Fri, 9 Aug 2024 15:45:34 +0200 Subject: [PATCH 1/2] isSpreadsheet maj --- src/Code.gs | 70 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/src/Code.gs b/src/Code.gs index f4f05e7..22a0768 100644 --- a/src/Code.gs +++ b/src/Code.gs @@ -190,6 +190,7 @@ const ChatGPTApp = (function () { let vectorStore; let attachmentIdentificator; let assistantTools; + let isSpreadsheet; let webSearchQueries = []; let webPagesOpened = []; @@ -331,11 +332,13 @@ const ChatGPTApp = (function () { * Enable a thread run with an OpenAI assistant. * @param {string} assistantId - your assistant id * @param {string} attachmentId - the Drive ID of the document you want to attach + * @param {boolean} [spreadsheet] - if attachment is spreadsheet * @returns {Chat} - The current Chat instance. */ - this.analyzeDocumentWithAssistant = function (assistantId, attachmentId) { + this.analyzeDocumentWithAssistant = function (assistantId, attachmentId, spreadsheet=false) { assistantIdentificator = assistantId; attachmentIdentificator = attachmentId; + isSpreadsheet=spreadsheet return this; } @@ -479,7 +482,8 @@ const ChatGPTApp = (function () { if (attachmentIdentificator) { runOpenAIAssistantFunction.setDescription("To analyze a file with code interpreter") - runOpenAIAssistantFunction.addParameter("attachmentId", "string", "the Id of the file attached"); + runOpenAIAssistantFunction.addParameter("attachmentId", "string", "the Id of the file attached") + runOpenAIAssistantFunction.addParameter("isSpreadsheet", "boolean", "if the file is a spreadsheet") } if (numberOfAPICalls == 0) { @@ -490,10 +494,17 @@ const ChatGPTApp = (function () { }); if (attachmentIdentificator) { - messages.push({ - role: "system", - content: `You can use the assistant ${assistantIdentificator} to analyze this file: "${attachmentIdentificator}"` - }); + if (isSpreadsheet) { + messages.push({ + role: "system", + content: `You can use the assistant ${assistantIdentificator} to analyze this file: "${attachmentIdentificator}, and the file is a spreadsheet" + }); + } else { + messages.push({ + role: "system", + content: `You can use the assistant ${assistantIdentificator} to analyze this file: "${attachmentIdentificator}, and the file is not a spreadsheet" + }); + } } else { messages.push({ role: "system", @@ -790,7 +801,7 @@ const ChatGPTApp = (function () { } if (functionName == "runOpenAIAssistant") { if (jsonArgs.attachmentId) { - return runOpenAIAssistant(jsonArgs.assistantId, jsonArgs.prompt, jsonArgs.attachmentId); + return runOpenAIAssistant(jsonArgs.assistantId, jsonArgs.prompt, jsonArgs.attachmentId, jsonArgs.isSpreadsheet); } else { return runOpenAIAssistant(jsonArgs.assistantId, jsonArgs.prompt); } @@ -887,23 +898,28 @@ const ChatGPTApp = (function () { * Uploads a file to OpenAI and returns the file ID. * * @param {string} optionalAttachment - The optional attachment ID from Google Drive. + * @param {boolean} isSpreadsheet - if the file is a spreadsheet * @returns {string} The OpenAI file ID. */ - function uploadFileToOpenAI(optionalAttachment) { - var file = DriveApp.getFileById(optionalAttachment); - var mimeType = file.getMimeType(); - let fileBlobUrl; - - switch (mimeType) { - case "application/vnd.google-apps.spreadsheet": - fileBlobUrl = 'https://docs.google.com/spreadsheets/d/' + optionalAttachment + '/export?format=xlsx'; - break; - case "application/vnd.google-apps.document": - fileBlobUrl = 'https://docs.google.com/document/d/' + optionalAttachment + '/export?format=docx'; - break; - case "application/vnd.google-apps.presentation": - fileBlobUrl = 'https://docs.google.com/presentation/d/' + optionalAttachment + '/export/pptx'; - break; + function uploadFileToOpenAI(optionalAttachment, isSpreadsheet) { + if (isSpreadsheet) { + fileBlobUrl = 'https://docs.google.com/spreadsheets/d/' + optionalAttachment + '/export?format=xlsx'; + } else { + var file = DriveApp.getFileById(optionalAttachment); + var mimeType = file.getMimeType(); + let fileBlobUrl; + + switch (mimeType) { + case "application/vnd.google-apps.spreadsheet": + fileBlobUrl = 'https://docs.google.com/spreadsheets/d/' + optionalAttachment + '/export?format=xlsx'; + break; + case "application/vnd.google-apps.document": + fileBlobUrl = 'https://docs.google.com/document/d/' + optionalAttachment + '/export?format=docx'; + break; + case "application/vnd.google-apps.presentation": + fileBlobUrl = 'https://docs.google.com/presentation/d/' + optionalAttachment + '/export/pptx'; + break; + } } var token = ScriptApp.getOAuthToken(); @@ -947,8 +963,9 @@ const ChatGPTApp = (function () { * @param {string} threadId - The ID of the thread. * @param {string} prompt - The prompt to send to the assistant. * @param {string} [optionalAttachment] - The optional attachment ID from Google Drive. + * @param {boolean} [isSpreadsheet] - if the file is a spreadsheet */ - function addMessageToThread(threadId, prompt, optionalAttachment) { + function addMessageToThread(threadId, prompt, optionalAttachment, isSpreadsheet) { let messagePayload = { "role": "user", "content": prompt @@ -956,7 +973,7 @@ const ChatGPTApp = (function () { if (optionalAttachment) { try { - var openAiFileId = uploadFileToOpenAI(optionalAttachment); + var openAiFileId = uploadFileToOpenAI(optionalAttachment, isSpreadsheet); messagePayload.attachments = [ { "file_id": openAiFileId, @@ -1106,12 +1123,13 @@ const ChatGPTApp = (function () { * @param {string} assistantId - The ID of the OpenAI assistant to run. * @param {string} prompt - The prompt to send to the assistant. * @param {string} [optionalAttachment] - The optional attachment ID from Google Drive. + * @param {boolean} [isSpreadsheet] - if the file is a spreadsheet * @returns {string} The assistant's response and references in JSON format. */ - function runOpenAIAssistant(assistantId, prompt, optionalAttachment) { + function runOpenAIAssistant(assistantId, prompt, optionalAttachment, isSpreadsheet) { try { var threadId = createThread(); - addMessageToThread(threadId, prompt, optionalAttachment); + addMessageToThread(threadId, prompt, optionalAttachment, isSpreadsheet); var runId = runAssistant(threadId, assistantId); monitorRunStatus(threadId, runId); return getAssistantResponse(threadId, assistantId); From 792b55fdf263186f2939cdbafb2da891dad4de97 Mon Sep 17 00:00:00 2001 From: louis rubeus Date: Fri, 9 Aug 2024 15:48:26 +0200 Subject: [PATCH 2/2] Fix bug --- src/Code.gs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Code.gs b/src/Code.gs index 22a0768..2ae5db4 100644 --- a/src/Code.gs +++ b/src/Code.gs @@ -902,12 +902,12 @@ const ChatGPTApp = (function () { * @returns {string} The OpenAI file ID. */ function uploadFileToOpenAI(optionalAttachment, isSpreadsheet) { + let fileBlobUrl; if (isSpreadsheet) { fileBlobUrl = 'https://docs.google.com/spreadsheets/d/' + optionalAttachment + '/export?format=xlsx'; } else { var file = DriveApp.getFileById(optionalAttachment); var mimeType = file.getMimeType(); - let fileBlobUrl; switch (mimeType) { case "application/vnd.google-apps.spreadsheet":