Skip to content

Commit b25e80d

Browse files
committed
refactor(samples): migrate function-calling samples to @google/genai using message structure for tool responses
1 parent aa40ac1 commit b25e80d

4 files changed

Lines changed: 111 additions & 142 deletions

File tree

generative-ai/snippets/function-calling/functionCallingAdvanced.js

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,28 @@
1313
// limitations under the License.
1414

1515
// [START generativeaionvertexai_function_calling_advanced]
16-
const {
17-
VertexAI,
18-
FunctionDeclarationSchemaType,
19-
} = require('@google-cloud/vertexai');
16+
const {GoogleGenAI} = require('@google/genai');
2017

21-
const functionDeclarations = [
18+
const tools = [
2219
{
23-
function_declarations: [
20+
functionDeclarations: [
2421
{
2522
name: 'get_product_sku',
26-
description:
27-
'Get the available inventory for a Google products, e.g: Pixel phones, Pixel Watches, Google Home etc',
23+
description: 'Get the available inventory for Google products',
2824
parameters: {
29-
type: FunctionDeclarationSchemaType.OBJECT,
25+
type: 'OBJECT',
3026
properties: {
31-
productName: {type: FunctionDeclarationSchemaType.STRING},
27+
productName: {type: 'STRING'},
3228
},
3329
},
3430
},
3531
{
3632
name: 'get_store_location',
3733
description: 'Get the location of the closest store',
3834
parameters: {
39-
type: FunctionDeclarationSchemaType.OBJECT,
35+
type: 'OBJECT',
4036
properties: {
41-
location: {type: FunctionDeclarationSchemaType.STRING},
37+
location: {type: 'STRING'},
4238
},
4339
},
4440
},
@@ -47,18 +43,12 @@ const functionDeclarations = [
4743
];
4844

4945
const toolConfig = {
50-
function_calling_config: {
46+
functionCallingConfig: {
5147
mode: 'ANY',
52-
allowed_function_names: ['get_product_sku'],
48+
allowedFunctionNames: ['get_product_sku'],
5349
},
5450
};
5551

56-
const generationConfig = {
57-
temperature: 0.95,
58-
topP: 1.0,
59-
maxOutputTokens: 8192,
60-
};
61-
6252
/**
6353
* TODO(developer): Update these variables before running the sample.
6454
*/
@@ -67,29 +57,25 @@ async function functionCallingAdvanced(
6757
location = 'us-central1',
6858
model = 'gemini-2.0-flash-001'
6959
) {
70-
// Initialize Vertex with your Cloud project and location
71-
const vertexAI = new VertexAI({project: projectId, location: location});
60+
// Initialize client with your Cloud project and location
61+
const client = new GoogleGenAI({
62+
vertexai: true,
63+
project: projectId,
64+
location: location,
65+
});
7266

73-
// Instantiate the model
74-
const generativeModel = vertexAI.preview.getGenerativeModel({
67+
const result = await client.models.generateContent({
7568
model: model,
69+
contents: 'Do you have the White Pixel 8 Pro 128GB in stock in the US?',
70+
config: {
71+
tools: tools,
72+
toolConfig: toolConfig,
73+
temperature: 0.95,
74+
topP: 1.0,
75+
maxOutputTokens: 8192,
76+
},
7677
});
77-
78-
const request = {
79-
contents: [
80-
{
81-
role: 'user',
82-
parts: [
83-
{text: 'Do you have the White Pixel 8 Pro 128GB in stock in the US?'},
84-
],
85-
},
86-
],
87-
tools: functionDeclarations,
88-
tool_config: toolConfig,
89-
generation_config: generationConfig,
90-
};
91-
const result = await generativeModel.generateContent(request);
92-
console.log(JSON.stringify(result.response.candidates[0].content));
78+
console.log(JSON.stringify(result.functionCalls));
9379
}
9480
// [END generativeaionvertexai_function_calling_advanced]
9581

generative-ai/snippets/function-calling/functionCallingBasic.js

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,20 @@
1313
// limitations under the License.
1414

1515
// [START generativeaionvertexai_function_calling_basic]
16-
const {
17-
VertexAI,
18-
FunctionDeclarationSchemaType,
19-
} = require('@google-cloud/vertexai');
16+
const {GoogleGenAI} = require('@google/genai');
2017

21-
const functionDeclarations = [
18+
const tools = [
2219
{
23-
function_declarations: [
20+
functionDeclarations: [
2421
{
2522
name: 'get_current_weather',
2623
description: 'get weather in a given location',
2724
parameters: {
28-
type: FunctionDeclarationSchemaType.OBJECT,
25+
type: 'OBJECT',
2926
properties: {
30-
location: {type: FunctionDeclarationSchemaType.STRING},
27+
location: {type: 'STRING'},
3128
unit: {
32-
type: FunctionDeclarationSchemaType.STRING,
29+
type: 'STRING',
3330
enum: ['celsius', 'fahrenheit'],
3431
},
3532
},
@@ -48,22 +45,21 @@ async function functionCallingBasic(
4845
location = 'us-central1',
4946
model = 'gemini-2.0-flash-001'
5047
) {
51-
// Initialize Vertex with your Cloud project and location
52-
const vertexAI = new VertexAI({project: projectId, location: location});
48+
// Initialize client with your Cloud project and location
49+
const client = new GoogleGenAI({
50+
vertexai: true,
51+
project: projectId,
52+
location: location,
53+
});
5354

54-
// Instantiate the model
55-
const generativeModel = vertexAI.preview.getGenerativeModel({
55+
const result = await client.models.generateContent({
5656
model: model,
57+
contents: 'What is the weather in Boston?',
58+
config: {
59+
tools: tools,
60+
},
5761
});
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));
62+
console.log(JSON.stringify(result.functionCalls));
6763
}
6864
// [END generativeaionvertexai_function_calling_basic]
6965

generative-ai/snippets/function-calling/functionCallingStreamChat.js

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,19 @@
1313
// limitations under the License.
1414

1515
// [START generativeaionvertexai_gemini_function_calling_chat]
16-
const {
17-
VertexAI,
18-
FunctionDeclarationSchemaType,
19-
} = require('@google-cloud/vertexai');
16+
const {GoogleGenAI} = require('@google/genai');
2017

21-
const functionDeclarations = [
18+
const tools = [
2219
{
23-
function_declarations: [
20+
functionDeclarations: [
2421
{
2522
name: 'get_current_weather',
2623
description: 'get weather in a given location',
2724
parameters: {
28-
type: FunctionDeclarationSchemaType.OBJECT,
25+
type: 'OBJECT',
2926
properties: {
30-
location: {type: FunctionDeclarationSchemaType.STRING},
31-
unit: {
32-
type: FunctionDeclarationSchemaType.STRING,
33-
enum: ['celsius', 'fahrenheit'],
34-
},
27+
location: {type: 'STRING'},
28+
unit: {type: 'STRING', enum: ['celsius', 'fahrenheit']},
3529
},
3630
required: ['location'],
3731
},
@@ -40,15 +34,6 @@ const functionDeclarations = [
4034
},
4135
];
4236

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-
5237
/**
5338
* TODO(developer): Update these variables before running the sample.
5439
*/
@@ -57,38 +42,43 @@ async function functionCallingStreamChat(
5742
location = 'us-central1',
5843
model = 'gemini-2.0-flash-001'
5944
) {
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.getGenerativeModel({
65-
model: model,
45+
// Initialize client with your Cloud project and location
46+
const client = new GoogleGenAI({
47+
vertexai: true,
48+
project: projectId,
49+
location: location,
6650
});
6751

6852
// Create a chat session and pass your function declarations
69-
const chat = generativeModel.startChat({
70-
tools: functionDeclarations,
53+
const chat = client.chats.create({
54+
model: model,
55+
config: {tools: tools},
7156
});
7257

73-
const chatInput1 = 'What is the weather in Boston?';
74-
7558
// 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;
59+
const result1 = await chat.sendMessage({
60+
message: 'What is the weather in Boston?',
61+
});
62+
console.log(
63+
'Function call requested:',
64+
JSON.stringify(result1.functionCalls, null, 2)
65+
);
8166

8267
// 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-
}
68+
const result2 = await chat.sendMessage({
69+
message: [
70+
{
71+
functionResponse: {
72+
name: 'get_current_weather',
73+
response: {result: {weather: 'super nice'}},
74+
},
75+
},
76+
],
77+
});
8778

8879
// This should include a text response from the model using the response content
8980
// provided above
90-
const response2 = await result2.response;
91-
console.log(response2.candidates[0].content.parts[0].text);
81+
console.log(result2.text);
9282
}
9383
// [END generativeaionvertexai_gemini_function_calling_chat]
9484

generative-ai/snippets/function-calling/functionCallingStreamContent.js

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,19 @@
1414

1515
// [START aiplatform_gemini_function_calling_content]
1616
// [START generativeaionvertexai_gemini_function_calling_content]
17-
const {
18-
VertexAI,
19-
FunctionDeclarationSchemaType,
20-
} = require('@google-cloud/vertexai');
17+
const {GoogleGenAI} = require('@google/genai');
2118

22-
const functionDeclarations = [
19+
const tools = [
2320
{
24-
function_declarations: [
21+
functionDeclarations: [
2522
{
2623
name: 'get_current_weather',
2724
description: 'get weather in a given location',
2825
parameters: {
29-
type: FunctionDeclarationSchemaType.OBJECT,
26+
type: 'OBJECT',
3027
properties: {
31-
location: {type: FunctionDeclarationSchemaType.STRING},
32-
unit: {
33-
type: FunctionDeclarationSchemaType.STRING,
34-
enum: ['celsius', 'fahrenheit'],
35-
},
28+
location: {type: 'STRING'},
29+
unit: {type: 'STRING', enum: ['celsius', 'fahrenheit']},
3630
},
3731
required: ['location'],
3832
},
@@ -58,35 +52,38 @@ async function functionCallingStreamContent(
5852
location = 'us-central1',
5953
model = 'gemini-2.0-flash-001'
6054
) {
61-
// Initialize Vertex with your Cloud project and location
62-
const vertexAI = new VertexAI({project: projectId, location: location});
63-
64-
// Instantiate the model
65-
const generativeModel = vertexAI.getGenerativeModel({
66-
model: model,
55+
// Initialize client with your Cloud project and location
56+
const client = new GoogleGenAI({
57+
vertexai: true,
58+
project: projectId,
59+
location: location,
6760
});
6861

69-
const request = {
70-
contents: [
71-
{role: 'user', parts: [{text: 'What is the weather in Boston?'}]},
72-
{
73-
role: 'ASSISTANT',
74-
parts: [
75-
{
76-
functionCall: {
77-
name: 'get_current_weather',
78-
args: {location: 'Boston'},
79-
},
62+
const request = [
63+
{role: 'user', parts: [{text: 'What is the weather in Boston?'}]},
64+
{
65+
role: 'model',
66+
parts: [
67+
{
68+
functionCall: {
69+
name: 'get_current_weather',
70+
args: {location: 'Boston'},
8071
},
81-
],
82-
},
83-
{role: 'USER', parts: functionResponseParts},
84-
],
85-
tools: functionDeclarations,
86-
};
87-
const streamingResp = await generativeModel.generateContentStream(request);
88-
for await (const item of streamingResp.stream) {
89-
console.log(item.candidates[0].content.parts[0].text);
72+
},
73+
],
74+
},
75+
{role: 'USER', parts: functionResponseParts},
76+
];
77+
78+
const streamingResp = await client.models.generateContentStream({
79+
model: model,
80+
contents: request,
81+
config: {tools: tools},
82+
});
83+
for await (const chunk of streamingResp) {
84+
if (chunk.text) {
85+
console.log(chunk.text);
86+
}
9087
}
9188
}
9289
// [END aiplatform_gemini_function_calling_content]

0 commit comments

Comments
 (0)