|
| 1 | +/* |
| 2 | +Copyright 2024 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 VERTEX_AI_LOCATION = PropertiesService.getScriptProperties().getProperty('project_location'); |
| 18 | +const MODEL_ID = PropertiesService.getScriptProperties().getProperty('model_id'); |
| 19 | +const SERVICE_ACCOUNT_KEY = PropertiesService.getScriptProperties().getProperty('service_account_key'); |
| 20 | + |
| 21 | +/** |
| 22 | + * Packages prompt and necessary settings, then sends a request to |
| 23 | + * Vertex API. Returns the response as an JSON object extracted from the |
| 24 | + * Vertex API response object. |
| 25 | + * |
| 26 | + * @param prompt - String representing your prompt for Gemini AI. |
| 27 | + */ |
| 28 | +function getAiSummary(prompt) { |
| 29 | + |
| 30 | + const request = { |
| 31 | + "contents": [{ |
| 32 | + "role": "user", |
| 33 | + "parts": [{ |
| 34 | + "text": prompt |
| 35 | + }] |
| 36 | + }], |
| 37 | + "generationConfig": { |
| 38 | + "temperature": 0.1, |
| 39 | + "maxOutputTokens": 2048, |
| 40 | + }, |
| 41 | + "safetySettings": [ |
| 42 | + { |
| 43 | + "category": "HARM_CATEGORY_HARASSMENT", |
| 44 | + "threshold": "BLOCK_NONE" |
| 45 | + }, |
| 46 | + { |
| 47 | + "category": "HARM_CATEGORY_HATE_SPEECH", |
| 48 | + "threshold": "BLOCK_NONE" |
| 49 | + }, |
| 50 | + { |
| 51 | + "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", |
| 52 | + "threshold": "BLOCK_NONE" |
| 53 | + }, |
| 54 | + { |
| 55 | + "category": "HARM_CATEGORY_DANGEROUS_CONTENT", |
| 56 | + "threshold": "BLOCK_NONE" |
| 57 | + } |
| 58 | + ] |
| 59 | + }; |
| 60 | + |
| 61 | + const credentials = credentialsForVertexAI(); |
| 62 | + |
| 63 | + const fetchOptions = { |
| 64 | + method: 'post', |
| 65 | + headers: { |
| 66 | + 'Authorization': `Bearer ${credentials.accessToken}` |
| 67 | + }, |
| 68 | + contentType: 'application/json', |
| 69 | + muteHttpExceptions: true, |
| 70 | + payload: JSON.stringify(request) |
| 71 | + } |
| 72 | + |
| 73 | + const url = `https://${VERTEX_AI_LOCATION}-aiplatform.googleapis.com/v1/projects/${credentials.projectId}/` |
| 74 | + + `locations/${VERTEX_AI_LOCATION}/publishers/google/models/${MODEL_ID}:generateContent` |
| 75 | + |
| 76 | + const response = UrlFetchApp.fetch(url, fetchOptions); |
| 77 | + |
| 78 | + const payload = JSON.parse(response); |
| 79 | + const text = payload.candidates[0].content.parts[0].text |
| 80 | + |
| 81 | + return text |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Gets credentials required to call Vertex API using a Service Account. |
| 86 | + * Requires use of Service Account Key stored with project |
| 87 | + * |
| 88 | + * @return {!Object} Containing the Cloud Project Id and the access token. |
| 89 | + */ |
| 90 | +function credentialsForVertexAI() { |
| 91 | + const credentials = SERVICE_ACCOUNT_KEY; |
| 92 | + if (!credentials) { |
| 93 | + throw new Error("service_account_key script property must be set."); |
| 94 | + } |
| 95 | + |
| 96 | + const parsedCredentials = JSON.parse(credentials); |
| 97 | + |
| 98 | + const service = OAuth2.createService("Vertex") |
| 99 | + .setTokenUrl('https://oauth2.googleapis.com/token') |
| 100 | + .setPrivateKey(parsedCredentials['private_key']) |
| 101 | + .setIssuer(parsedCredentials['client_email']) |
| 102 | + .setPropertyStore(PropertiesService.getScriptProperties()) |
| 103 | + .setScope("https://www.googleapis.com/auth/cloud-platform"); |
| 104 | + return { |
| 105 | + projectId: parsedCredentials['project_id'], |
| 106 | + accessToken: service.getAccessToken(), |
| 107 | + } |
| 108 | +} |
0 commit comments