Skip to content

Commit ff51616

Browse files
authored
feat(genai): add migrated functionCallingStreamContent sample from generative-ai (#4367)
* feat(genai): rename and migrate functionCallingStreamContent sample * fix(genai): update function response * fix: update region tag
1 parent d4c13c2 commit ff51616

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
'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.GOOGLE_CLOUD_PROJECT;
23+
const location = process.env.GOOGLE_CLOUD_LOCATION || 'global';
24+
const model = 'gemini-2.5-flash';
25+
26+
describe('tools-func-calling-stream-content', () => {
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-2.5-flash';
34+
35+
it('should create stream chat and begin the conversation the same in each instance', async () => {
36+
const output = execSync(
37+
`node ./tools-func-calling-stream-content.js ${projectId} ${location} ${model}`
38+
);
39+
40+
// Assert that the response is what we expect
41+
assert(output.match(/super nice/), output);
42+
});
43+
});
Lines changed: 96 additions & 0 deletions
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_genai_function_calling_stream_content]
16+
const {GoogleGenAI} = require('@google/genai');
17+
18+
const tools = [
19+
{
20+
functionDeclarations: [
21+
{
22+
name: 'get_current_weather',
23+
description: 'get weather in a given location',
24+
parameters: {
25+
type: 'OBJECT',
26+
properties: {
27+
location: {type: 'STRING'},
28+
unit: {type: 'STRING', enum: ['celsius', 'fahrenheit']},
29+
},
30+
required: ['location'],
31+
},
32+
},
33+
],
34+
},
35+
];
36+
37+
const functionResponseParts = [
38+
{
39+
functionResponse: {
40+
name: 'get_current_weather',
41+
response: {weather: 'super nice'},
42+
},
43+
},
44+
];
45+
46+
/**
47+
* TODO(developer): Update these variables before running the sample.
48+
*/
49+
async function functionCallingStreamContent(
50+
projectId = 'PROJECT_ID',
51+
location = 'us-central1',
52+
model = 'gemini-2.5-flash'
53+
) {
54+
// Initialize client with your Cloud project and location
55+
const client = new GoogleGenAI({
56+
vertexai: true,
57+
project: projectId,
58+
location: location,
59+
});
60+
61+
const request = [
62+
{role: 'user', parts: [{text: 'What is the weather in Boston?'}]},
63+
{
64+
role: 'model',
65+
parts: [
66+
{
67+
functionCall: {
68+
name: 'get_current_weather',
69+
args: {location: 'Boston'},
70+
},
71+
},
72+
],
73+
},
74+
{role: 'user', parts: functionResponseParts},
75+
];
76+
77+
const streamingResp = await client.models.generateContentStream({
78+
model: model,
79+
contents: request,
80+
config: {tools: tools},
81+
});
82+
83+
let completeResponseText = '';
84+
for await (const chunk of streamingResp) {
85+
if (chunk.text) {
86+
completeResponseText += chunk.text;
87+
}
88+
}
89+
console.log(completeResponseText);
90+
}
91+
// [END aiplatform_genai_function_calling_stream_content]
92+
93+
functionCallingStreamContent(...process.argv.slice(2)).catch(err => {
94+
console.error(err.message);
95+
process.exitCode = 1;
96+
});

0 commit comments

Comments
 (0)