1- {
2- "timeZone" : "America/Toronto" ,
3- "oauthScopes" : [
4- "https://www.googleapis.com/auth/script.external_request" ,
5- "https://www.googleapis.com/auth/cloud-platform" ,
6- "https://www.googleapis.com/auth/script.locale" ,
7- "https://www.googleapis.com/auth/userinfo.email" ,
8- "https://www.googleapis.com/auth/gmail.addons.execute" ,
9- "https://www.googleapis.com/auth/gmail.labels" ,
10- "https://www.googleapis.com/auth/gmail.modify"
11- ] ,
12- "addOns" : {
13- "common" : {
14- "name" : "Sentiment Analysis" ,
15- "logoUrl" : "https://fonts.gstatic.com/s/i/googlematerialicons/dynamic_feed/v6/black-24dp/1x/gm_dynamic_feed_black_24dp.png" ,
16- "useLocaleFromApp" : true
17- } ,
18- "gmail" : {
19- "homepageTrigger" : {
20- "runFunction" : "onHomepageTrigger" ,
21- "enabled" : true
1+ /*
2+ Copyright 2024-2025 Google LLC
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ https://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+ */
16+
17+ const PROJECT_ID = 'qwiklabs-gcp-02-66afcfbf5a65' ;
18+ const VERTEX_AI_LOCATION = 'us-central1' ;
19+ const MODEL_ID = 'gemini-1.5-flash' ;
20+
21+ /**
22+ * Packages prompt and necessary settings, then sends a request to
23+ * Vertex API.
24+ * A check is performed to see if the response from Vertex AI contains FALSE as a value.
25+ * Returns the outcome of that check which is a boolean.
26+ *
27+ * @param emailText - Email message that is sent to the model.
28+ */
29+
30+ function processSentiment ( emailText ) {
31+ const prompt = `Analyze the sentiment of the following message: ${ emailText } ` ;
32+
33+ const request = {
34+ "contents" : [ {
35+ "role" : "user" ,
36+ "parts" : [ {
37+ "text" : prompt ,
38+ } ]
39+ } ] ,
40+ "generationConfig" : {
41+ "temperature" : 0.9 ,
42+ "maxOutputTokens" : 1024 ,
43+ "responseMimeType" : "application/json" ,
44+ "responseSchema" : {
45+ "type" : "object" ,
46+ "properties" : {
47+ "response" : {
48+ "type" : "string" ,
49+ "enum" : [
50+ "positive" ,
51+ "negative" ,
52+ "neutral" ,
53+ ]
54+ }
55+ }
2256 }
2357 }
24- } ,
25- "exceptionLogging" : "STACKDRIVER" ,
26- "runtimeVersion" : "V8"
58+ } ;
59+
60+ const fetchOptions = {
61+ method : 'POST' ,
62+ headers : {
63+ 'Authorization' : `Bearer ${ ScriptApp . getOAuthToken ( ) } `
64+ } ,
65+ contentType : 'application/json' ,
66+ muteHttpExceptions : true ,
67+ payload : JSON . stringify ( request ) ,
68+ }
69+
70+ const url = `https://${ VERTEX_AI_LOCATION } -aiplatform.googleapis.com/v1/projects/${ PROJECT_ID } /`
71+ + `locations/${ VERTEX_AI_LOCATION } /publishers/google/models/${ MODEL_ID } :generateContent`
72+
73+ const response = UrlFetchApp . fetch ( url , fetchOptions ) ;
74+ const payload = JSON . parse ( response . getContentText ( ) ) ;
75+ const text = JSON . parse ( payload . candidates [ 0 ] . content . parts [ 0 ] . text ) ;
76+
77+ return text . response ;
2778}
0 commit comments