Skip to content

Commit 0185fe4

Browse files
committed
feat(vertexai): Add Function calling snippets
Move existing snippets to new folder, and update tests
1 parent 9cc360b commit 0185fe4

10 files changed

+258
-5
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2024 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 generativeaionvertexai_function_calling_advanced]
16+
const {
17+
VertexAI,
18+
FunctionDeclarationSchemaType,
19+
} = require('@google-cloud/vertexai');
20+
21+
const functionDeclarations = [
22+
{
23+
function_declarations: [
24+
{
25+
name: 'get_product_sku',
26+
description:
27+
'Get the available inventory for a Google products, e.g: Pixel phones, Pixel Watches, Google Home etc',
28+
parameters: {
29+
type: FunctionDeclarationSchemaType.OBJECT,
30+
properties: {
31+
productName: {type: FunctionDeclarationSchemaType.STRING},
32+
},
33+
},
34+
},
35+
{
36+
name: 'get_store_location',
37+
description: 'Get the location of the closest store',
38+
parameters: {
39+
type: FunctionDeclarationSchemaType.OBJECT,
40+
properties: {
41+
location: {type: FunctionDeclarationSchemaType.STRING},
42+
},
43+
},
44+
},
45+
],
46+
},
47+
];
48+
49+
const toolConfig = {
50+
function_calling_config: {
51+
mode: 'ANY',
52+
allowed_function_names: ['get_product_sku'],
53+
},
54+
};
55+
56+
/**
57+
* TODO(developer): Update these variables before running the sample.
58+
*/
59+
async function functionCallingBasic(
60+
projectId = 'PROJECT_ID',
61+
location = 'us-central1',
62+
model = 'gemini-1.0-pro'
63+
) {
64+
// Initialize Vertex with your Cloud project and location
65+
const vertexAI = new VertexAI({project: projectId, location: location});
66+
67+
// Instantiate the model
68+
const generativeModel = vertexAI.preview.getGenerativeModel({
69+
model: model,
70+
});
71+
72+
const request = {
73+
contents: [
74+
{
75+
role: 'user',
76+
parts: [
77+
{text: 'Do you have the White Pixel 8 Pro 128GB in stock in the US?'},
78+
],
79+
},
80+
],
81+
tools: functionDeclarations,
82+
tool_config: toolConfig,
83+
};
84+
const result = await generativeModel.generateContent(request);
85+
console.log(JSON.stringify(result.response.candidates[0].content));
86+
}
87+
// [END generativeaionvertexai_function_calling_advanced]
88+
89+
functionCallingBasic(...process.argv.slice(2)).catch(err => {
90+
console.error(err.message);
91+
process.exitCode = 1;
92+
});
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2024 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 generativeaionvertexai_function_calling_basic]
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+
/**
44+
* TODO(developer): Update these variables before running the sample.
45+
*/
46+
async function functionCallingBasic(
47+
projectId = 'PROJECT_ID',
48+
location = 'us-central1',
49+
model = 'gemini-1.0-pro'
50+
) {
51+
// Initialize Vertex with your Cloud project and location
52+
const vertexAI = new VertexAI({project: projectId, location: location});
53+
54+
// Instantiate the model
55+
const generativeModel = vertexAI.preview.getGenerativeModel({
56+
model: model,
57+
});
58+
59+
const request = {
60+
contents: [
61+
{role: 'user', parts: [{text: 'What is the weather in Boston?'}]},
62+
],
63+
tools: functionDeclarations,
64+
};
65+
const result = await generativeModel.generateContent(request);
66+
console.log(JSON.stringify(result.response.candidates[0].content));
67+
}
68+
// [END generativeaionvertexai_function_calling_basic]
69+
70+
functionCallingBasic(...process.argv.slice(2)).catch(err => {
71+
console.error(err.message);
72+
process.exitCode = 1;
73+
});

generative-ai/snippets/inference/nonStreamMultiModalityBasic.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ async function generateContent(
4040
},
4141
{
4242
file_data: {
43-
file_uri: 'gs://cloud-samples-data/generative-ai/image/character.jpg',
43+
file_uri:
44+
'gs://cloud-samples-data/generative-ai/image/character.jpg',
4445
mime_type: 'image/jpeg',
4546
},
4647
},

generative-ai/snippets/inference/streamMultiModalityBasic.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ async function generateContent(
4040
},
4141
{
4242
file_data: {
43-
file_uri: 'gs://cloud-samples-data/generative-ai/image/character.jpg',
43+
file_uri:
44+
'gs://cloud-samples-data/generative-ai/image/character.jpg',
4445
mime_type: 'image/jpeg',
4546
},
4647
},
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2043 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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
const cp = require('child_process');
20+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
21+
22+
const projectId = process.env.CAIP_PROJECT_ID;
23+
const location = process.env.LOCATION;
24+
const model = 'gemini-1.0-pro';
25+
26+
describe('Generative AI Function Calling Advanced', () => {
27+
/**
28+
* TODO(developer): Uncomment these variables before running the sample.\
29+
* (Not necessary if passing values as arguments)
30+
*/
31+
// const projectId = 'YOUR_PROJECT_ID';
32+
// const location = 'YOUR_LOCATION';
33+
// const model = 'gemini-1.0-pro';
34+
35+
it('should define multiple functions and have the model invoke the specified one', async () => {
36+
const output = execSync(
37+
`node ./function-calling/functionCallingAdvanced.js ${projectId} ${location} ${model}`
38+
);
39+
40+
// Assert that the response is what we expect
41+
assert(output.match(/get_product_sku/));
42+
});
43+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2043 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+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
const cp = require('child_process');
20+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
21+
22+
const projectId = process.env.CAIP_PROJECT_ID;
23+
const location = process.env.LOCATION;
24+
const model = 'gemini-1.0-pro';
25+
26+
describe('Generative AI Function Calling', () => {
27+
/**
28+
* TODO(developer): Uncomment these variables before running the sample.\
29+
* (Not necessary if passing values as arguments)
30+
*/
31+
// const projectId = 'YOUR_PROJECT_ID';
32+
// const location = 'YOUR_LOCATION';
33+
// const model = 'gemini-1.0-pro';
34+
35+
it('should define a function and have the model invoke it', async () => {
36+
const output = execSync(
37+
`node ./function-calling/functionCallingBasic.js ${projectId} ${location} ${model}`
38+
);
39+
40+
// Assert that the response is what we expect
41+
assert(output.length > 0);
42+
});
43+
});

generative-ai/snippets/test/functionCallingStreamChat.test.js renamed to generative-ai/snippets/test/function-calling/functionCallingStreamChat.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2121

2222
const projectId = process.env.CAIP_PROJECT_ID;
2323
const location = process.env.LOCATION;
24-
const model = 'gemini-1.0-pro';
24+
const model = 'gemini-1.5-pro-preview-0409';
2525

2626
describe('Generative AI Function Calling Stream Chat', () => {
2727
/**
@@ -34,7 +34,7 @@ describe('Generative AI Function Calling Stream Chat', () => {
3434

3535
it('should create stream chat and begin the conversation the same in each instance', async () => {
3636
const output = execSync(
37-
`node ./functionCallingStreamChat.js ${projectId} ${location} ${model}`
37+
`node ./function-calling/functionCallingStreamChat.js ${projectId} ${location} ${model}`
3838
);
3939

4040
// Assert that the response is what we expect

generative-ai/snippets/test/functionCallingStreamContent.test.js renamed to generative-ai/snippets/test/function-calling/functionCallingStreamContent.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('Generative AI Function Calling Stream Content', () => {
3434

3535
it('should create stream chat and begin the conversation the same in each instance', async () => {
3636
const output = execSync(
37-
`node ./functionCallingStreamContent.js ${projectId} ${location} ${model}`
37+
`node ./function-calling/functionCallingStreamContent.js ${projectId} ${location} ${model}`
3838
);
3939

4040
// Assert that the response is what we expect

0 commit comments

Comments
 (0)