Skip to content

Commit 3d18bd0

Browse files
committed
Applied changes are suggested.
1 parent 8760fa1 commit 3d18bd0

File tree

4 files changed

+49
-51
lines changed

4 files changed

+49
-51
lines changed

gmail-sentiment-analysis/Code.gs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ limitations under the License.
1919
* @return {CardService.Card} The card to show to the user.
2020
*/
2121
function onHomepage(e) {
22-
if(e.hostApp =="gmail"){
23-
return buildCard_GmailHome();
24-
}
25-
}
22+
return buildCard_GmailHome();
23+
}
24+

gmail-sentiment-analysis/Gmail.gs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ function analyzeSentiment(){
2828
* Gets the last 10 threads in the inbox and the corresponding messages.
2929
* Fetches the label that should be applied to negative messages.
3030
* The processSentiment is called on each message
31-
* and testet with RegExp to check for a negative answer from the model
31+
* and tested with RegExp to check for a negative answer from the model
3232
*/
3333

3434
function emailSentiment() {
3535
const threads = GmailApp.getInboxThreads(0, 10);
3636
const msgs = GmailApp.getMessagesForThreads(threads);
3737
const label_upset = GmailApp.getUserLabelByName("UPSET TONE 😡");
38-
const regex = new RegExp('N');
3938
let currentPrediction;
4039

4140
for (let i = 0 ; i < msgs.length; i++) {
4241
for (let j = 0; j < msgs[i].length; j++) {
4342
let emailText = msgs[i][j].getPlainBody();
4443
currentPrediction = processSentiment(emailText);
45-
if(regex.test(currentPrediction)){
44+
if(currentPrediction === true){
45+
console.log("In condition:", currentPrediction)
4646
label_upset.addToThread(msgs[i][j].getThread());
4747
}
4848
}

gmail-sentiment-analysis/Vertex.gs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,33 @@ limitations under the License.
1616

1717
const PROJECT_ID = [ADD YOUR GCP PROJECT ID HERE];
1818
const VERTEX_AI_LOCATION = 'europe-west2';
19-
const MODEL_ID = 'text-bison';
19+
const MODEL_ID = 'gemini-1.5-pro-002';
2020
const SERVICE_ACCOUNT_KEY = PropertiesService.getScriptProperties().getProperty('service_account_key');
2121

2222
/**
2323
* Packages prompt and necessary settings, then sends a request to
24-
* Vertex API. Returns the response as an JSON object extracted from the
25-
* Vertex API response object.
24+
* Vertex API.
25+
* A check is performed to see if the response from Vertex AI contains FALSE as a value.
26+
* Returns the outcome of that check which is a boolean.
2627
*
2728
* @param emailText - Email message that is sent to the model.
2829
*/
2930

3031
function processSentiment(emailText) {
31-
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!`;
32+
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!`;
33+
3234
const request = {
33-
"instances": [{
34-
"prompt": prompt
35+
"contents": [{
36+
"role": "user",
37+
"parts": [{
38+
"text": prompt
39+
}]
3540
}],
36-
"parameters": {
41+
"generationConfig": {
3742
"temperature": 0.9,
3843
"maxOutputTokens": 1024,
39-
"topK": 1,
40-
"topP": 1
41-
},
42-
44+
45+
}
4346
};
4447

4548
const credentials = credentialsForVertexAI();
@@ -55,13 +58,15 @@ function processSentiment(emailText) {
5558
}
5659

5760
const url = `https://${VERTEX_AI_LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/`
58-
+ `locations/${VERTEX_AI_LOCATION}/publishers/google/models/${MODEL_ID}:predict`
61+
+ `locations/${VERTEX_AI_LOCATION}/publishers/google/models/${MODEL_ID}:generateContent`
5962

6063
const response = UrlFetchApp.fetch(url, fetchOptions);
6164
const payload = JSON.parse(response.getContentText());
62-
console.log(payload.predictions[0].content);
6365

64-
return payload.predictions[0].content;
66+
const regex = /FALSE/;
67+
68+
return regex.test(payload.candidates[0].content.parts[0].text);
69+
6570
}
6671

6772
/**
Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
{
2-
"timeZone": "Europe/Madrid",
3-
"dependencies": {
4-
"libraries": [
5-
{
6-
"userSymbol": "OAuth2",
7-
"version": "43",
8-
"libraryId": "1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF"
9-
}
10-
]
11-
},
12-
"addOns": {
13-
"common": {
14-
"name": "Productivity toolbox",
15-
"logoUrl": "https://icons.iconarchive.com/icons/roundicons/100-free-solid/64/spy-icon.png",
16-
"useLocaleFromApp": true,
17-
"homepageTrigger": {
18-
"runFunction": "onHomepage",
19-
"enabled": true
20-
}
21-
},
22-
"gmail": {
23-
"contextualTriggers": [
24-
{
25-
"unconditional": {},
26-
"onTriggerFunction": "onGmailMessage"
27-
}
28-
]
2+
"timeZone": "Europe/Madrid",
3+
"dependencies": {
4+
"libraries": [
5+
{
6+
"userSymbol": "OAuth2",
7+
"version": "43",
8+
"libraryId": "1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF"
299
}
10+
]
11+
},
12+
"addOns": {
13+
"common": {
14+
"name": "Productivity toolbox",
15+
"logoUrl": "https://icons.iconarchive.com/icons/roundicons/100-free-solid/64/spy-icon.png",
16+
"useLocaleFromApp": true
3017
},
31-
"exceptionLogging": "STACKDRIVER",
32-
"runtimeVersion": "V8"
33-
}
18+
"gmail": {
19+
"homepageTrigger": {
20+
"runFunction": "onHomepage",
21+
"enabled": true
22+
}
23+
}
24+
},
25+
"exceptionLogging": "STACKDRIVER",
26+
"runtimeVersion": "V8"
27+
}

0 commit comments

Comments
 (0)