@@ -14,10 +14,9 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17- const PROJECT_ID = [ ADD YOUR GCP PROJECT ID HERE ] ;
17+ const PROJECT_ID = ' [ADD YOUR GCP PROJECT ID HERE]' ;
1818const VERTEX_AI_LOCATION = 'europe-west2' ;
1919const MODEL_ID = 'gemini-1.5-pro-002' ;
20- const SERVICE_ACCOUNT_KEY = PropertiesService . getScriptProperties ( ) . getProperty ( 'service_account_key' ) ;
2120
2221/**
2322 * Packages prompt and necessary settings, then sends a request to
@@ -29,69 +28,42 @@ const SERVICE_ACCOUNT_KEY = PropertiesService.getScriptProperties().getProperty(
2928 */
3029
3130function processSentiment ( emailText ) {
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!` ;
31+ const prompt = `
32+ Analyze the following message: ${ emailText } .
33+ If the sentiment of this message is negative, answer with FALSE.
34+ If the sentiment of this message is neutral or positive, answer with TRUE.
35+ Do not use any other words than the ones requested in this prompt as a response!` ;
3336
3437 const request = {
3538 "contents" : [ {
3639 "role" : "user" ,
3740 "parts" : [ {
38- "text" : prompt
41+ "text" : prompt ,
3942 } ]
4043 } ] ,
4144 "generationConfig" : {
4245 "temperature" : 0.9 ,
4346 "maxOutputTokens" : 1024 ,
44-
4547 }
4648 } ;
4749
48- const credentials = credentialsForVertexAI ( ) ;
49-
5050 const fetchOptions = {
5151 method : 'POST' ,
5252 headers : {
53- 'Authorization' : `Bearer ${ credentials . accessToken } `
53+ 'Authorization' : `Bearer ${ ScriptApp . getOAuthToken ( ) } `
5454 } ,
5555 contentType : 'application/json' ,
5656 muteHttpExceptions : true ,
57- payload : JSON . stringify ( request )
57+ payload : JSON . stringify ( request ) ,
5858 }
5959
6060 const url = `https://${ VERTEX_AI_LOCATION } -aiplatform.googleapis.com/v1/projects/${ PROJECT_ID } /`
61- + `locations/${ VERTEX_AI_LOCATION } /publishers/google/models/${ MODEL_ID } :generateContent`
61+ + `locations/${ VERTEX_AI_LOCATION } /publishers/google/models/${ MODEL_ID } :generateContent`
6262
6363 const response = UrlFetchApp . fetch ( url , fetchOptions ) ;
6464 const payload = JSON . parse ( response . getContentText ( ) ) ;
6565
6666 const regex = / F A L S E / ;
6767
6868 return regex . test ( payload . candidates [ 0 ] . content . parts [ 0 ] . text ) ;
69-
7069}
71-
72- /**
73- * Gets credentials required to call Vertex API using a Service Account.
74- * Requires use of Service Account Key stored with project
75- *
76- * @return {!Object } Containing the Cloud Project Id and the access token.
77- */
78-
79- function credentialsForVertexAI ( ) {
80- const credentials = SERVICE_ACCOUNT_KEY ;
81- if ( ! credentials ) {
82- throw new Error ( "service_account_key script property must be set." ) ;
83- }
84-
85- const parsedCredentials = JSON . parse ( credentials ) ;
86-
87- const service = OAuth2 . createService ( "Vertex" )
88- . setTokenUrl ( 'https://oauth2.googleapis.com/token' )
89- . setPrivateKey ( parsedCredentials [ 'private_key' ] )
90- . setIssuer ( parsedCredentials [ 'client_email' ] )
91- . setPropertyStore ( PropertiesService . getScriptProperties ( ) )
92- . setScope ( "https://www.googleapis.com/auth/cloud-platform" ) ;
93- return {
94- projectId : parsedCredentials [ 'project_id' ] ,
95- accessToken : service . getAccessToken ( ) ,
96- }
97- }
0 commit comments