Skip to content

feat(genai): Adding tools samples #4153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
29 changes: 29 additions & 0 deletions genai/test/tools-code-exec-with-txt-local-img.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../tools/tools-code-exec-with-txt-local-img.js');

describe('tools-code-exec-with-txt-local-img', async () => {
it('should generate a function definition', async function () {
this.timeout(100000);
const output = await sample.generateContent(projectId);
assert(output.length > 0);
});
});
3 changes: 2 additions & 1 deletion genai/test/tools-code-exec-with-txt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../tools/tools-code-exec-with-txt.js');

describe('tools-code-exec-with-txt', async () => {
it('should generate code and execution result', async () => {
it('should generate code and execution result', async function () {
this.timeout(50000);
const output = await sample.generateContent(projectId);
assert(output.length > 0);
});
Expand Down
29 changes: 29 additions & 0 deletions genai/test/tools-func-def-with-txt.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../tools/tools-func-def-with-txt.js');

describe('tools-func-def-with-txt', async () => {
it('should generate a function definition', async function () {
this.timeout(10000);
const output = await sample.generateContent(projectId);
assert(output.length > 0);
});
});
3 changes: 2 additions & 1 deletion genai/test/tools-func-desc-with-txt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../tools/tools-func-desc-with-txt.js');

describe('tools-func-desc-with-txt', async () => {
it('should generate a function call', async () => {
it('should generate a function call', async function () {
this.timeout(10000);
const output = await sample.generateContent(projectId);
assert(output.length > 0);
});
Expand Down
29 changes: 29 additions & 0 deletions genai/test/tools-google-search-with-txt.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../tools/tools-google-search-with-txt.js');

describe('tools-google-search-with-txt', async () => {
it('should generate answer to a question in prompt using google search', async function () {
this.timeout(10000);
const output = await sample.generateContent(projectId);
assert(output.length > 0);
});
});
29 changes: 29 additions & 0 deletions genai/test/tools-vais-with-txt.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');

const projectId = process.env.CAIP_PROJECT_ID;
const sample = require('../tools/tools-vais-with-txt.js');

describe('tools-vais-with-txt', async () => {
it('should generate a function call', async function () {
this.timeout(60000);
const output = await sample.generateContent(projectId);
assert(output.length > 0);
});
});
90 changes: 90 additions & 0 deletions genai/tools/tools-code-exec-with-txt-local-img.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START googlegenaisdk_tools_code_exec_with_txt]
const fs = require('fs').promises;
const path = require('path');

const {GoogleGenAI} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';

async function generateContent(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const ai = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const imagePath = path.join(
__dirname,
'../test-data/640px-Monty_open_door.svg.png'
);
const imageBuffer = await fs.readFile(imagePath);
const imageBase64 = imageBuffer.toString('base64');

const prompt = `
Run a simulation of the Monty Hall Problem with 1,000 trials.
Here's how this works as a reminder. In the Monty Hall Problem, you're on a game
show with three doors. Behind one is a car, and behind the others are goats. You
pick a door. The host, who knows what's behind the doors, opens a different door
to reveal a goat. Should you switch to the remaining unopened door?
The answer has always been a little difficult for me to understand when people
solve it with math - so please run a simulation with Python to show me what the
best strategy is.
Thank you!
`;

const contents = [
{
role: 'user',
parts: [
{
inlineData: {
mimeType: 'image/png',
data: imageBase64,
},
},
{
text: prompt,
},
],
},
];

const response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: contents,
config: {
tools: [{codeExecution: {}}],
temperature: 0,
},
});

console.debug(response.executableCode);
console.debug(response.codeExecutionResult);

return response.codeExecutionResult;
}
// [END googlegenaisdk_tools_code_exec_with_txt]

module.exports = {
generateContent,
};
2 changes: 1 addition & 1 deletion genai/tools/tools-code-exec-with-txt.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function generateContent(
});

const response = await ai.models.generateContent({
model: 'gemini-2.5-flash-preview-05-20',
model: 'gemini-2.5-flash',
contents:
'What is the sum of the first 50 prime numbers? Generate and run code for the calculation, and make sure you get all 50.',
config: {
Expand Down
66 changes: 66 additions & 0 deletions genai/tools/tools-func-def-with-txt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START googlegenaisdk_tools_func_def_with_txt]
const {GoogleGenAI} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
// todo not working
async function generateContent(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const ai = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

function getCurrentWeather({location}) {
const weatherMap = {
'Boston, MA': 'snowing',
'San Francisco, CA': 'foggy',
'Seattle, WA': 'raining',
'Austin, TX': 'hot',
'Chicago, IL': 'windy',
};
return {
result: weatherMap[location] || 'unknown',
};
}

const response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: 'What is the weather like in Boston?',
config: {
tools: [
{
functionDeclarations: [getCurrentWeather],
},
],
temperature: 0,
},
});
console.log(response);

return response.text;
}
// [END googlegenaisdk_tools_func_def_with_txt]

module.exports = {
generateContent,
};
9 changes: 4 additions & 5 deletions genai/tools/tools-func-desc-with-txt.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const {GoogleGenAI, Type} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';

async function generateContent(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
Expand Down Expand Up @@ -72,17 +71,17 @@ async function generateContent(
`;

const response = await ai.models.generateContent({
model: 'gemini-2.0-flash',
model: 'gemini-2.5-flash',
contents: prompt,
config: {
tools: [sales_tool],
temperature: 0,
},
});
const output = JSON.stringify(response.functionCalls, null, 2);
console.log(output);

console.log(response.functionCalls);

return response.functionCalls;
return output;
}
// [END googlegenaisdk_tools_func_desc_with_txt]

Expand Down
53 changes: 53 additions & 0 deletions genai/tools/tools-google-search-with-txt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START googlegenaisdk_tools_google_search_with_txt]
const {GoogleGenAI} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';

async function generateContent(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const ai = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const response = await ai.models.generateContent({
model: 'gemini-2.5-flash',
contents: 'When is the next total solar eclipse in Poland?',
config: {
tools: [
{
googleSearch: {},
},
],
},
});

console.log(response.text);

return response.text;
}
// [END googlegenaisdk_tools_google_search_with_txt]

module.exports = {
generateContent,
};
Loading