Skip to content

Commit

Permalink
Applied changes are suggested.
Browse files Browse the repository at this point in the history
  • Loading branch information
chanelgreco committed Oct 22, 2024
1 parent 8760fa1 commit 3d18bd0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 51 deletions.
7 changes: 3 additions & 4 deletions gmail-sentiment-analysis/Code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
return buildCard_GmailHome();
}

6 changes: 3 additions & 3 deletions gmail-sentiment-analysis/Gmail.gs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
33 changes: 19 additions & 14 deletions gmail-sentiment-analysis/Vertex.gs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

}

/**
Expand Down
54 changes: 24 additions & 30 deletions gmail-sentiment-analysis/appsscript.json
Original file line number Diff line number Diff line change
@@ -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"
}
"gmail": {
"homepageTrigger": {
"runFunction": "onHomepage",
"enabled": true
}
}
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}

0 comments on commit 3d18bd0

Please sign in to comment.