Skip to content

Commit 3e856b3

Browse files
committed
Add function calling example
1 parent f34e1e1 commit 3e856b3

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2025 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+
20+
const projectId = process.env.CAIP_PROJECT_ID;
21+
const sample = require('../tools/tools-func-desc-with-txt.js');
22+
23+
describe('tools-func-desc-with-txt', async () => {
24+
it('should generate a function call', async () => {
25+
const output = await sample.generateContent(projectId);
26+
assert(output.length > 0);
27+
});
28+
});
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2025 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+
// [START googlegenaisdk_tools_func_desc_with_txt]
18+
const {GoogleGenAI, Type} = require('@google/genai');
19+
20+
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
21+
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
22+
23+
async function generateContent(
24+
projectId = GOOGLE_CLOUD_PROJECT,
25+
location = GOOGLE_CLOUD_LOCATION
26+
) {
27+
const ai = new GoogleGenAI({
28+
vertexai: true,
29+
project: projectId,
30+
location: location,
31+
});
32+
33+
const get_album_sales = {
34+
name: 'get_album_sales',
35+
description: 'Gets the number of albums sold',
36+
parameters: {
37+
type: Type.OBJECT,
38+
properties: {
39+
albums: {
40+
type: Type.ARRAY,
41+
description: 'List of albums',
42+
items: {
43+
description: 'Album and its sales',
44+
type: Type.OBJECT,
45+
properties: {
46+
album_name: {
47+
type: Type.STRING,
48+
description: 'Name of the music album',
49+
},
50+
copies_sold: {
51+
type: Type.INTEGER,
52+
description: 'Number of copies sold',
53+
},
54+
},
55+
},
56+
},
57+
},
58+
},
59+
};
60+
61+
const sales_tool = {
62+
functionDeclarations: [get_album_sales],
63+
};
64+
65+
const prompt = `
66+
At Stellar Sounds, a music label, 2024 was a rollercoaster. "Echoes of the Night", a debut synth-pop album,
67+
surprisingly sold 350,000 copies, while veteran rock band "Crimson Tide's" latest, "Reckless Hearts",
68+
lagged at 120,000. Their up-and-coming indie artist, "Luna Bloom's" EP, "Whispers of Dawn",
69+
secured 75,000 sales. The biggest disappointment was the highly-anticipated rap album "Street Symphony"
70+
only reaching 100,000 units. Overall, Stellar Sounds moved over 645,000 units this year, revealing unexpected
71+
trends in music consumption.
72+
`;
73+
74+
const response = await ai.models.generateContent({
75+
model: 'gemini-2.0-flash',
76+
contents: prompt,
77+
config: {
78+
tools: [sales_tool],
79+
temperature: 0,
80+
},
81+
});
82+
83+
console.log(response.functionCalls);
84+
85+
return response.functionCalls;
86+
}
87+
// [END googlegenaisdk_tools_func_desc_with_txt]
88+
89+
module.exports = {
90+
generateContent,
91+
};

0 commit comments

Comments
 (0)