Skip to content

Commit 73ef4c8

Browse files
committed
added function calling examples
1 parent d0d4d28 commit 73ef4c8

4 files changed

+253
-0
lines changed
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_chat]
16+
const { VertexAI, FunctionDeclarationSchemaType } = require('@google-cloud/vertexai');
17+
18+
const functionDeclarations = [
19+
{
20+
function_declarations: [
21+
{
22+
name: "get_current_weather",
23+
description: 'get weather in a given location',
24+
parameters: {
25+
type: FunctionDeclarationSchemaType.OBJECT,
26+
properties: {
27+
location: {type: FunctionDeclarationSchemaType.STRING},
28+
unit: {
29+
type: FunctionDeclarationSchemaType.STRING,
30+
enum: ['celsius', 'fahrenheit'],
31+
},
32+
},
33+
required: ['location'],
34+
},
35+
},
36+
],
37+
},
38+
];
39+
40+
const functionResponseParts = [
41+
{
42+
functionResponse: {
43+
name: "get_current_weather",
44+
response:
45+
{name: "get_current_weather", content: {weather: "super nice"}},
46+
},
47+
},
48+
];
49+
50+
/**
51+
* TODO(developer): Update these variables before running the sample.
52+
*/
53+
async function functionCallingStreamChat(
54+
projectId = 'PROJECT_ID',
55+
location = 'us-central1',
56+
model = 'gemini-pro'
57+
) {
58+
// Initialize Vertex with your Cloud project and location
59+
const vertexAI = new VertexAI({ project: projectId, location: location });
60+
61+
// Instantiate the model
62+
const generativeModel = vertexAI.preview.getGenerativeModel({
63+
model: model,
64+
});
65+
66+
// Create a chat session and pass your function declarations
67+
const chat = generativeModel.startChat({
68+
tools: functionDeclarations,
69+
});
70+
71+
const chatInput1 = 'What is the weather in Boston?';
72+
73+
// This should include a functionCall response from the model
74+
const result1 = await chat.sendMessageStream(chatInput1);
75+
for await (const item of result1.stream) {
76+
console.log(item.candidates[0]);
77+
}
78+
const response1 = await result1.response;
79+
80+
// Send a follow up message with a FunctionResponse
81+
const result2 = await chat.sendMessageStream(functionResponseParts);
82+
for await (const item of result2.stream) {
83+
console.log(item.candidates[0]);
84+
}
85+
86+
// This should include a text response from the model using the response content
87+
// provided above
88+
const response2 = await result2.response;
89+
console.log(response2.candidates[0].content.parts[0].text);
90+
}
91+
// [END aiplatform_gemini_function_calling_chat]
92+
93+
functionCallingStreamChat(...process.argv.slice(2)).catch(err => {
94+
console.error(err.message);
95+
process.exitCode = 1;
96+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 { VertexAI, FunctionDeclarationSchemaType } = require('@google-cloud/vertexai');
17+
18+
const functionDeclarations = [
19+
{
20+
function_declarations: [
21+
{
22+
name: "get_current_weather",
23+
description: 'get weather in a given location',
24+
parameters: {
25+
type: FunctionDeclarationSchemaType.OBJECT,
26+
properties: {
27+
location: {type: FunctionDeclarationSchemaType.STRING},
28+
unit: {
29+
type: FunctionDeclarationSchemaType.STRING,
30+
enum: ['celsius', 'fahrenheit'],
31+
},
32+
},
33+
required: ['location'],
34+
},
35+
},
36+
],
37+
},
38+
];
39+
40+
const functionResponseParts = [
41+
{
42+
functionResponse: {
43+
name: "get_current_weather",
44+
response:
45+
{name: "get_current_weather", content: {weather: "super nice"}},
46+
},
47+
},
48+
];
49+
50+
/**
51+
* TODO(developer): Update these variables before running the sample.
52+
*/
53+
async function functionCallingStreamChat(
54+
projectId = 'PROJECT_ID',
55+
location = 'us-central1',
56+
model = 'gemini-pro'
57+
) {
58+
// Initialize Vertex with your Cloud project and location
59+
const vertexAI = new VertexAI({ project: projectId, location: location });
60+
61+
// Instantiate the model
62+
const generativeModel = vertexAI.preview.getGenerativeModel({
63+
model: model,
64+
});
65+
66+
const request = {
67+
contents: [
68+
{role: 'user', parts: [{text: 'What is the weather in Boston?'}]},
69+
{role: 'model', parts: [{functionCall: {name: 'get_current_weather', args: {'location': 'Boston'}}}]},
70+
{role: 'function', parts: functionResponseParts}
71+
],
72+
tools: functionDeclarations,
73+
};
74+
const streamingResp =
75+
await generativeModel.generateContentStream(request);
76+
for await (const item of streamingResp.stream) {
77+
console.log(item.candidates[0].content.parts[0].text);
78+
}
79+
}
80+
// [END aiplatform_gemini_function_calling_content]
81+
82+
functionCallingStreamChat(...process.argv.slice(2)).catch(err => {
83+
console.error(err.message);
84+
process.exitCode = 1;
85+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
21+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
22+
23+
describe('Generative AI Function Calling Stream Chat', () => {
24+
const project = 'cloud-llm-preview1';
25+
const location = 'us-central1';
26+
const model = 'gemini-pro';
27+
28+
it('should create stream chat and begin the conversation the same in each instance', async () => {
29+
const output = execSync(
30+
`node ./functionCallingStreamChat.js ${project} ${location} ${model}`
31+
);
32+
33+
// Assert that the response is what we expect
34+
assert(output.match(/The weather in Boston is super nice./));
35+
});
36+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
21+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
22+
23+
describe('Generative AI Function Calling Stream Content', () => {
24+
const project = 'cloud-llm-preview1';
25+
const location = 'us-central1';
26+
const model = 'gemini-pro';
27+
28+
it('should create stream chat and begin the conversation the same in each instance', async () => {
29+
const output = execSync(
30+
`node ./functionCallingStreamContent.js ${project} ${location} ${model}`
31+
);
32+
33+
// Assert that the response is what we expect
34+
assert(output.match(/The weather in Boston is super nice./));
35+
});
36+
});

0 commit comments

Comments
 (0)