From 3d18bd0c1b168d7695092d3d0f8a8f8a7c787246 Mon Sep 17 00:00:00 2001 From: Chanel Greco Date: Tue, 22 Oct 2024 16:51:13 +0200 Subject: [PATCH] Applied changes are suggested. --- gmail-sentiment-analysis/Code.gs | 7 ++- gmail-sentiment-analysis/Gmail.gs | 6 +-- gmail-sentiment-analysis/Vertex.gs | 33 +++++++++------ gmail-sentiment-analysis/appsscript.json | 54 +++++++++++------------- 4 files changed, 49 insertions(+), 51 deletions(-) diff --git a/gmail-sentiment-analysis/Code.gs b/gmail-sentiment-analysis/Code.gs index 143a5e1d9..7b353e735 100644 --- a/gmail-sentiment-analysis/Code.gs +++ b/gmail-sentiment-analysis/Code.gs @@ -19,7 +19,6 @@ limitations under the License. * @return {CardService.Card} The card to show to the user. */ function onHomepage(e) { - if(e.hostApp =="gmail"){ - return buildCard_GmailHome(); - } -} \ No newline at end of file + return buildCard_GmailHome(); +} + diff --git a/gmail-sentiment-analysis/Gmail.gs b/gmail-sentiment-analysis/Gmail.gs index e3fab9463..b8ac5b3d5 100644 --- a/gmail-sentiment-analysis/Gmail.gs +++ b/gmail-sentiment-analysis/Gmail.gs @@ -28,21 +28,21 @@ function analyzeSentiment(){ * Gets the last 10 threads in the inbox and the corresponding messages. * Fetches the label that should be applied to negative messages. * The processSentiment is called on each message - * and testet with RegExp to check for a negative answer from the model + * and tested with RegExp to check for a negative answer from the model */ function emailSentiment() { const threads = GmailApp.getInboxThreads(0, 10); const msgs = GmailApp.getMessagesForThreads(threads); const label_upset = GmailApp.getUserLabelByName("UPSET TONE 😡"); - const regex = new RegExp('N'); let currentPrediction; for (let i = 0 ; i < msgs.length; i++) { for (let j = 0; j < msgs[i].length; j++) { let emailText = msgs[i][j].getPlainBody(); currentPrediction = processSentiment(emailText); - if(regex.test(currentPrediction)){ + if(currentPrediction === true){ + console.log("In condition:", currentPrediction) label_upset.addToThread(msgs[i][j].getThread()); } } diff --git a/gmail-sentiment-analysis/Vertex.gs b/gmail-sentiment-analysis/Vertex.gs index 9703c97e7..f6299ecde 100644 --- a/gmail-sentiment-analysis/Vertex.gs +++ b/gmail-sentiment-analysis/Vertex.gs @@ -16,30 +16,33 @@ limitations under the License. const PROJECT_ID = [ADD YOUR GCP PROJECT ID HERE]; const VERTEX_AI_LOCATION = 'europe-west2'; -const MODEL_ID = 'text-bison'; +const MODEL_ID = 'gemini-1.5-pro-002'; const SERVICE_ACCOUNT_KEY = PropertiesService.getScriptProperties().getProperty('service_account_key'); /** * Packages prompt and necessary settings, then sends a request to - * Vertex API. Returns the response as an JSON object extracted from the - * Vertex API response object. + * Vertex API. + * A check is performed to see if the response from Vertex AI contains FALSE as a value. + * Returns the outcome of that check which is a boolean. * * @param emailText - Email message that is sent to the model. */ function processSentiment(emailText) { - const prompt = `Analyze the following message: ${emailText}. If the sentiment of this message is negative, answer with NEGATIVE. If the sentiment of this message is neutral or positive, answer with OK. Do not use any other words than the ones requested in this prompt as a response!`; + const prompt = `Analyze the following message: ${emailText}. If the sentiment of this message is negative, answer with FALSE. If the sentiment of this message is neutral or positive, answer with TRUE. Do not use any other words than the ones requested in this prompt as a response!`; + const request = { - "instances": [{ - "prompt": prompt + "contents": [{ + "role": "user", + "parts": [{ + "text": prompt + }] }], - "parameters": { + "generationConfig": { "temperature": 0.9, "maxOutputTokens": 1024, - "topK": 1, - "topP": 1 - }, - + + } }; const credentials = credentialsForVertexAI(); @@ -55,13 +58,15 @@ function processSentiment(emailText) { } const url = `https://${VERTEX_AI_LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/` - + `locations/${VERTEX_AI_LOCATION}/publishers/google/models/${MODEL_ID}:predict` + + `locations/${VERTEX_AI_LOCATION}/publishers/google/models/${MODEL_ID}:generateContent` const response = UrlFetchApp.fetch(url, fetchOptions); const payload = JSON.parse(response.getContentText()); - console.log(payload.predictions[0].content); - return payload.predictions[0].content; + const regex = /FALSE/; + + return regex.test(payload.candidates[0].content.parts[0].text); + } /** diff --git a/gmail-sentiment-analysis/appsscript.json b/gmail-sentiment-analysis/appsscript.json index acf10a04c..80b231e9a 100644 --- a/gmail-sentiment-analysis/appsscript.json +++ b/gmail-sentiment-analysis/appsscript.json @@ -1,33 +1,27 @@ { - "timeZone": "Europe/Madrid", - "dependencies": { - "libraries": [ - { - "userSymbol": "OAuth2", - "version": "43", - "libraryId": "1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF" - } - ] - }, - "addOns": { - "common": { - "name": "Productivity toolbox", - "logoUrl": "https://icons.iconarchive.com/icons/roundicons/100-free-solid/64/spy-icon.png", - "useLocaleFromApp": true, - "homepageTrigger": { - "runFunction": "onHomepage", - "enabled": true - } - }, - "gmail": { - "contextualTriggers": [ - { - "unconditional": {}, - "onTriggerFunction": "onGmailMessage" - } - ] + "timeZone": "Europe/Madrid", + "dependencies": { + "libraries": [ + { + "userSymbol": "OAuth2", + "version": "43", + "libraryId": "1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF" } + ] + }, + "addOns": { + "common": { + "name": "Productivity toolbox", + "logoUrl": "https://icons.iconarchive.com/icons/roundicons/100-free-solid/64/spy-icon.png", + "useLocaleFromApp": true }, - "exceptionLogging": "STACKDRIVER", - "runtimeVersion": "V8" - } \ No newline at end of file + "gmail": { + "homepageTrigger": { + "runFunction": "onHomepage", + "enabled": true + } + } + }, + "exceptionLogging": "STACKDRIVER", + "runtimeVersion": "V8" +} \ No newline at end of file