Skip to content

Commit 438c4d2

Browse files
committed
refactor: resolve merge conflicts with main
2 parents ac670b4 + a2248a8 commit 438c4d2

25 files changed

+419
-54
lines changed

generative-ai/snippets/countTokens.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ const {VertexAI} = require('@google-cloud/vertexai');
2121
async function countTokens(
2222
projectId = 'PROJECT_ID',
2323
location = 'us-central1',
24-
model = 'gemini-pro'
24+
model = 'gemini-1.0-pro'
2525
) {
2626
// Initialize Vertex with your Cloud project and location
27-
const vertex_ai = new VertexAI({project: projectId, location: location});
27+
const vertexAI = new VertexAI({project: projectId, location: location});
2828

2929
// Instantiate the model
30-
const generativeModel = vertex_ai.preview.getGenerativeModel({
30+
const generativeModel = vertexAI.getGenerativeModel({
3131
model: model,
3232
});
3333

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// [START aiplatform_gemini_function_calling_chat]
16+
const {
17+
VertexAI,
18+
FunctionDeclarationSchemaType,
19+
} = require('@google-cloud/vertexai');
20+
21+
const functionDeclarations = [
22+
{
23+
function_declarations: [
24+
{
25+
name: 'get_current_weather',
26+
description: 'get weather in a given location',
27+
parameters: {
28+
type: FunctionDeclarationSchemaType.OBJECT,
29+
properties: {
30+
location: {type: FunctionDeclarationSchemaType.STRING},
31+
unit: {
32+
type: FunctionDeclarationSchemaType.STRING,
33+
enum: ['celsius', 'fahrenheit'],
34+
},
35+
},
36+
required: ['location'],
37+
},
38+
},
39+
],
40+
},
41+
];
42+
43+
const functionResponseParts = [
44+
{
45+
functionResponse: {
46+
name: 'get_current_weather',
47+
response: {name: 'get_current_weather', content: {weather: 'super nice'}},
48+
},
49+
},
50+
];
51+
52+
/**
53+
* TODO(developer): Update these variables before running the sample.
54+
*/
55+
async function functionCallingStreamChat(
56+
projectId = 'PROJECT_ID',
57+
location = 'us-central1',
58+
model = 'gemini-1.0-pro'
59+
) {
60+
// Initialize Vertex with your Cloud project and location
61+
const vertexAI = new VertexAI({project: projectId, location: location});
62+
63+
// Instantiate the model
64+
const generativeModel = vertexAI.preview.getGenerativeModel({
65+
model: model,
66+
});
67+
68+
// Create a chat session and pass your function declarations
69+
const chat = generativeModel.startChat({
70+
tools: functionDeclarations,
71+
});
72+
73+
const chatInput1 = 'What is the weather in Boston?';
74+
75+
// This should include a functionCall response from the model
76+
const result1 = await chat.sendMessageStream(chatInput1);
77+
for await (const item of result1.stream) {
78+
console.log(item.candidates[0]);
79+
}
80+
await result1.response;
81+
82+
// Send a follow up message with a FunctionResponse
83+
const result2 = await chat.sendMessageStream(functionResponseParts);
84+
for await (const item of result2.stream) {
85+
console.log(item.candidates[0]);
86+
}
87+
88+
// This should include a text response from the model using the response content
89+
// provided above
90+
const response2 = await result2.response;
91+
console.log(response2.candidates[0].content.parts[0].text);
92+
}
93+
// [END aiplatform_gemini_function_calling_chat]
94+
95+
functionCallingStreamChat(...process.argv.slice(2)).catch(err => {
96+
console.error(err.message);
97+
process.exitCode = 1;
98+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// [START aiplatform_gemini_function_calling_content]
16+
const {
17+
VertexAI,
18+
FunctionDeclarationSchemaType,
19+
} = require('@google-cloud/vertexai');
20+
21+
const functionDeclarations = [
22+
{
23+
function_declarations: [
24+
{
25+
name: 'get_current_weather',
26+
description: 'get weather in a given location',
27+
parameters: {
28+
type: FunctionDeclarationSchemaType.OBJECT,
29+
properties: {
30+
location: {type: FunctionDeclarationSchemaType.STRING},
31+
unit: {
32+
type: FunctionDeclarationSchemaType.STRING,
33+
enum: ['celsius', 'fahrenheit'],
34+
},
35+
},
36+
required: ['location'],
37+
},
38+
},
39+
],
40+
},
41+
];
42+
43+
const functionResponseParts = [
44+
{
45+
functionResponse: {
46+
name: 'get_current_weather',
47+
response: {name: 'get_current_weather', content: {weather: 'super nice'}},
48+
},
49+
},
50+
];
51+
52+
/**
53+
* TODO(developer): Update these variables before running the sample.
54+
*/
55+
async function functionCallingStreamChat(
56+
projectId = 'PROJECT_ID',
57+
location = 'us-central1',
58+
model = 'gemini-1.0-pro'
59+
) {
60+
// Initialize Vertex with your Cloud project and location
61+
const vertexAI = new VertexAI({project: projectId, location: location});
62+
63+
// Instantiate the model
64+
const generativeModel = vertexAI.preview.getGenerativeModel({
65+
model: model,
66+
});
67+
68+
const request = {
69+
contents: [
70+
{role: 'user', parts: [{text: 'What is the weather in Boston?'}]},
71+
{
72+
role: 'model',
73+
parts: [
74+
{
75+
functionCall: {
76+
name: 'get_current_weather',
77+
args: {location: 'Boston'},
78+
},
79+
},
80+
],
81+
},
82+
{role: 'function', parts: functionResponseParts},
83+
],
84+
tools: functionDeclarations,
85+
};
86+
const streamingResp = await generativeModel.generateContentStream(request);
87+
for await (const item of streamingResp.stream) {
88+
console.log(item.candidates[0].content.parts[0].text);
89+
}
90+
}
91+
// [END aiplatform_gemini_function_calling_content]
92+
93+
functionCallingStreamChat(...process.argv.slice(2)).catch(err => {
94+
console.error(err.message);
95+
process.exitCode = 1;
96+
});

generative-ai/snippets/nonStreamingChat.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ const {VertexAI} = require('@google-cloud/vertexai');
2121
async function createNonStreamingChat(
2222
projectId = 'PROJECT_ID',
2323
location = 'us-central1',
24-
model = 'gemini-pro'
24+
model = 'gemini-1.0-pro'
2525
) {
2626
// Initialize Vertex with your Cloud project and location
2727
const vertexAI = new VertexAI({project: projectId, location: location});
2828

2929
// Instantiate the model
30-
const generativeModel = vertexAI.preview.getGenerativeModel({
30+
const generativeModel = vertexAI.getGenerativeModel({
3131
model: model,
3232
});
3333

@@ -37,20 +37,23 @@ async function createNonStreamingChat(
3737
console.log(`User: ${chatInput1}`);
3838

3939
const result1 = await chat.sendMessage(chatInput1);
40-
const response1 = result1.response.candidates[0].content.parts[0].text;
41-
console.log('Chat bot: ', response1);
40+
const response1 = result1.response;
41+
const text1 = response1.candidates[0].content.parts[0].text;
42+
console.log('Chat bot: ', text1);
4243

4344
const chatInput2 = 'Can you tell me a scientific fun fact?';
4445
console.log(`User: ${chatInput2}`);
4546
const result2 = await chat.sendMessage(chatInput2);
46-
const response2 = result2.response.candidates[0].content.parts[0].text;
47-
console.log('Chat bot: ', response2);
47+
const response2 = await result2.response;
48+
const text2 = response2.candidates[0].content.parts[0].text;
49+
console.log('Chat bot: ', text2);
4850

4951
const chatInput3 = 'How can I learn more about that?';
5052
console.log(`User: ${chatInput3}`);
5153
const result3 = await chat.sendMessage(chatInput3);
52-
const response3 = result3.response.candidates[0].content.parts[0].text;
53-
console.log('Chat bot: ', response3);
54+
const response3 = await result3.response;
55+
const text3 = response3.candidates[0].content.parts[0].text;
56+
console.log('Chat bot: ', text3);
5457
}
5558
// [END aiplatform_gemini_multiturn_chat_nonstreaming]
5659

generative-ai/snippets/nonStreamingContent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ const {VertexAI} = require('@google-cloud/vertexai');
2121
async function createNonStreamingContent(
2222
projectId = 'PROJECT_ID',
2323
location = 'us-central1',
24-
model = 'gemini-pro'
24+
model = 'gemini-1.0-pro'
2525
) {
2626
// Initialize Vertex with your Cloud project and location
2727
const vertexAI = new VertexAI({project: projectId, location: location});
2828

2929
// Instantiate the model
30-
const generativeModel = vertexAI.preview.getGenerativeModel({
30+
const generativeModel = vertexAI.getGenerativeModel({
3131
model: model,
3232
});
3333

generative-ai/snippets/nonStreamingMultipartContent.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ const {VertexAI} = require('@google-cloud/vertexai');
2121
async function createNonStreamingMultipartContent(
2222
projectId = 'PROJECT_ID',
2323
location = 'us-central1',
24-
model = 'gemini-pro-vision',
24+
model = 'gemini-1.0-pro-vision',
2525
image = 'gs://generativeai-downloads/images/scones.jpg',
2626
mimeType = 'image/jpeg'
2727
) {
2828
// Initialize Vertex with your Cloud project and location
2929
const vertexAI = new VertexAI({project: projectId, location: location});
3030

3131
// Instantiate the model
32-
const generativeVisionModel = vertexAI.preview.getGenerativeModel({
32+
const generativeVisionModel = vertexAI.getGenerativeModel({
3333
model: model,
3434
});
3535

@@ -50,7 +50,7 @@ async function createNonStreamingMultipartContent(
5050
};
5151

5252
console.log('Prompt Text:');
53-
console.log(request.contents[0].parts[0].text);
53+
console.log(request.contents[0].parts[1].text);
5454

5555
console.log('Non-Streaming Response Text:');
5656
// Create the response stream

generative-ai/snippets/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"test": "c8 mocha -p -j 2 --timeout 2400000 test/*.test.js"
1414
},
1515
"dependencies": {
16-
"@google-cloud/aiplatform": "^3.0.0",
16+
"@google-cloud/aiplatform": "^3.12.0",
1717
"@google-cloud/vertexai": "github:googleapis/nodejs-vertexai",
1818
"axios": "^1.6.2",
1919
"supertest": "^6.3.3"

generative-ai/snippets/safetySettings.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ const {
2525
async function setSafetySettings(
2626
projectId = 'PROJECT_ID',
2727
location = 'us-central1',
28-
model = 'gemini-pro'
28+
model = 'gemini-1.0-pro'
2929
) {
3030
// Initialize Vertex with your Cloud project and location
3131
const vertexAI = new VertexAI({project: projectId, location: location});
3232

3333
// Instantiate the model
34-
const generativeModel = vertexAI.preview.getGenerativeModel({
34+
const generativeModel = vertexAI.getGenerativeModel({
3535
model: model,
3636
// The following parameters are optional
3737
// They can also be passed to individual content generation requests
@@ -59,14 +59,15 @@ async function setSafetySettings(
5959
for await (const item of responseStream.stream) {
6060
if (item.candidates[0].finishReason === 'SAFETY') {
6161
console.log('This response stream terminated due to safety concerns.');
62+
break;
6263
} else {
6364
process.stdout.write(item.candidates[0].content.parts[0].text);
6465
}
6566
}
6667
}
6768
// [END aiplatform_gemini_safety_settings]
6869

69-
setSafetySettings(...process.argv.slice(3)).catch(err => {
70+
setSafetySettings(...process.argv.slice(2)).catch(err => {
7071
console.error(err.message);
7172
process.exitCode = 1;
7273
});

generative-ai/snippets/sendMultiModalPromptWithImage.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async function getBase64(url) {
2727
async function sendMultiModalPromptWithImage(
2828
projectId = 'PROJECT_ID',
2929
location = 'us-central1',
30-
model = 'gemini-pro-vision'
30+
model = 'gemini-1.0-pro-vision'
3131
) {
3232
// For images, the SDK supports base64 strings
3333
const landmarkImage1 = await getBase64(
@@ -43,7 +43,7 @@ async function sendMultiModalPromptWithImage(
4343
// Initialize Vertex with your Cloud project and location
4444
const vertexAI = new VertexAI({project: projectId, location: location});
4545

46-
const generativeVisionModel = vertexAI.preview.getGenerativeModel({
46+
const generativeVisionModel = vertexAI.getGenerativeModel({
4747
model: model,
4848
});
4949

generative-ai/snippets/sendMultiModalPromptWithVideo.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ const {VertexAI} = require('@google-cloud/vertexai');
2121
async function sendMultiModalPromptWithVideo(
2222
projectId = 'PROJECT_ID',
2323
location = 'us-central1',
24-
model = 'gemini-pro-vision'
24+
model = 'gemini-1.0-pro-vision'
2525
) {
2626
// Initialize Vertex with your Cloud project and location
2727
const vertexAI = new VertexAI({project: projectId, location: location});
2828

29-
const generativeVisionModel = vertexAI.preview.getGenerativeModel({
29+
const generativeVisionModel = vertexAI.getGenerativeModel({
3030
model: model,
3131
});
3232

generative-ai/snippets/streamChat.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ const {VertexAI} = require('@google-cloud/vertexai');
2121
async function createStreamChat(
2222
projectId = 'PROJECT_ID',
2323
location = 'us-central1',
24-
model = 'gemini-pro'
24+
model = 'gemini-1.0-pro'
2525
) {
2626
// Initialize Vertex with your Cloud project and location
2727
const vertexAI = new VertexAI({project: projectId, location: location});
2828

2929
// Instantiate the model
30-
const generativeModel = vertexAI.preview.getGenerativeModel({
30+
const generativeModel = vertexAI.getGenerativeModel({
3131
model: model,
3232
});
3333

0 commit comments

Comments
 (0)