-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathai.js
106 lines (84 loc) · 3.15 KB
/
ai.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const VERTEX_AI_LOCATION = PropertiesService.getScriptProperties().getProperty('project_location');
const MODEL_ID = PropertiesService.getScriptProperties().getProperty('model_id');
const SERVICE_ACCOUNT_KEY = PropertiesService.getScriptProperties().getProperty('service_account_key');
const STANDARD_PROMPT = `
Your task is to create 3 potential document names for this content.
Also, create a summary for this content, using 2 to 3 sentences, and don't include formatting.
Format the response as a JSON object with the first field called names and the summary field called summary.
The content is below:
`;
/**
* 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.
*
* @param prompt - String representing your prompt for Gemini AI.
*/
function getAiSummary(prompt) {
const request = {
"contents": [
{
"role": "user",
"parts": [{
text: STANDARD_PROMPT,
},
{
"text": prompt
}]
}
],
"generationConfig": {
"temperature": .2,
"maxOutputTokens": 2048,
"response_mime_type": "application/json"
}
}
const credentials = credentialsForVertexAI();
const fetchOptions = {
method: 'POST',
headers: {
'Authorization': `Bearer ${credentials.accessToken}`
},
contentType: 'application/json',
payload: JSON.stringify(request)
}
const url = `https://${VERTEX_AI_LOCATION}-aiplatform.googleapis.com/v1/projects/${credentials.projectId}/locations/${VERTEX_AI_LOCATION}/publishers/google/models/${MODEL_ID}:generateContent`
const response = UrlFetchApp.fetch(url, fetchOptions);
const payload = JSON.parse(response.getContentText());
const jsonPayload = JSON.parse(payload.candidates[0].content.parts[0].text)
return jsonPayload
}
/**
* Gets credentials required to call Vertex API using a Service Account.
*
*
*/
function credentialsForVertexAI() {
const credentials = SERVICE_ACCOUNT_KEY;
if (!credentials) {
throw new Error("service_account_key script property must be set.");
}
const parsedCredentials = JSON.parse(credentials);
const service = OAuth2.createService("Vertex")
.setTokenUrl('https://oauth2.googleapis.com/token')
.setPrivateKey(parsedCredentials['private_key'])
.setIssuer(parsedCredentials['client_email'])
.setPropertyStore(PropertiesService.getScriptProperties())
.setScope("https://www.googleapis.com/auth/cloud-platform");
return {
projectId: parsedCredentials['project_id'],
accessToken: service.getAccessToken(),
}
}