Skip to content
Open
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
974533b
fixing tools-code-exec-with-txt.js
Aug 4, 2025
aea4c94
fixing code from code review and adding linters
Aug 5, 2025
6993543
fixing code from code review and adding linters
Aug 5, 2025
b3977ff
linting
Aug 7, 2025
debd161
Update genai/test/tools-google-search-with-txt.test.js
Guiners Aug 7, 2025
cfc452b
adding samples, test, lints
Aug 7, 2025
764b444
Merge remote-tracking branch 'origin/sample/tools' into sample/tools
Aug 7, 2025
6197a83
Merge branch 'main' into sample/tools
Guiners Aug 7, 2025
92729b1
adding samples, test, lints
Aug 7, 2025
59fd49b
adding samples, test, lints
Aug 8, 2025
2ed95d2
adding samples, test, lints
Aug 11, 2025
b9dc1cc
Merge branch 'main' into sample/tools
Guiners Aug 12, 2025
e42ad00
adding samples, test, lints
Aug 13, 2025
171d5ac
Merge branch 'main' into sample/tools
Guiners Aug 18, 2025
652322e
Merge branch 'main' into sample/tools
Guiners Aug 19, 2025
6cf15e0
adding samples, test, lints
Aug 20, 2025
24fadf1
adding samples, test, lints
Aug 20, 2025
78c193a
adding samples, test, lints
Aug 20, 2025
9181c57
adding samples, test, lints
Aug 20, 2025
faa6d22
adding samples, test, lints
Aug 22, 2025
7aa567c
Merge branch 'main' into sample/tools
Guiners Aug 25, 2025
87e4e16
Merge branch 'main' into sample/tools
Guiners Sep 2, 2025
40e54a0
adding samples, test, lints
Sep 4, 2025
44c2426
Merge branch 'main' into sample/tools
msampathkumar Sep 4, 2025
53191cb
adding samples, test, lints
Sep 5, 2025
fe799d1
Merge branch 'main' into sample/tools
Guiners Sep 10, 2025
22b85c3
fixing functions names
Sep 10, 2025
4cb2338
Merge branch 'main' into sample/tools
Guiners Sep 12, 2025
7da4573
Merge branch 'main' into sample/tools
msampathkumar Sep 17, 2025
6a5834b
code review fixes
Sep 22, 2025
fdc97e4
sample update
Oct 2, 2025
21b4678
Merge branch 'main' into sample/tools
Guiners Oct 2, 2025
763a095
Merge branch 'main' into sample/tools
Guiners Oct 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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', () => {
it('should generate a function definition', async function () {
this.timeout(100000);
const output = await sample.generateAndExecuteMultimodalCode(projectId);
assert(output.length > 0);
});
});
2 changes: 1 addition & 1 deletion genai/test/tools-code-exec-with-txt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('tools-code-exec-with-txt', async () => {
this.timeout(180000);
this.retries(4);
await delay(this.test);
const output = await sample.generateContent(projectId);
const output = await sample.generateAndExecuteCode(projectId);
assert(output.length > 0);
});
});
2 changes: 1 addition & 1 deletion genai/test/tools-func-desc-with-txt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ describe('tools-func-desc-with-txt', async () => {
this.timeout(180000);
this.retries(4);
await delay(this.test);
await sample.generateContent(projectId);
await sample.generateFunctionDesc(projectId);
});
});
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', () => {
it('should generate answer to a question in prompt using google search', async function () {
this.timeout(10000);
const output = await sample.generateGoogleSearch(projectId);
assert(output.length > 0);
});
});
95 changes: 95 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,95 @@
// 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 generateAndExecuteMultimodalCode(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const imagePath = path.join(
__dirname,
'../test/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 client.models.generateContent({
model: 'gemini-2.5-flash',
contents: contents,
config: {
tools: [{codeExecution: {}}],
temperature: 0,
},
});

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

// Example response:
// Win percentage when switching: 65.50%
// Win percentage when not switching: 34.50%

return response.codeExecutionResult;
}

// [END googlegenaisdk_tools_code_exec_with_txt]

module.exports = {
generateAndExecuteMultimodalCode,
};
32 changes: 28 additions & 4 deletions genai/tools/tools-code-exec-with-txt.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ 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(
async function generateAndExecuteCode(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const ai = new GoogleGenAI({
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

const response = await ai.models.generateContent({
const response = await client.models.generateContent({
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.',
Expand All @@ -41,12 +41,36 @@ async function generateContent(
});

console.debug(response.executableCode);

// Example response:
// Code:
// function fibonacci(n) {
// if (n <= 0) {
// return 0;
// } else if (n === 1) {
// return 1;
// } else {
// let a = 0, b = 1;
// for (let i = 2; i <= n; i++) {
// [a, b] = [b, a + b];
// }
// return b;
// }
// }
//
// const fib20 = fibonacci(20);
// console.log(`fib20=${fib20}`);

console.debug(response.codeExecutionResult);

// Outcome:
// fib20=6765

return response.codeExecutionResult;
}

// [END googlegenaisdk_tools_code_exec_with_txt]

module.exports = {
generateContent,
generateAndExecuteCode,
};
32 changes: 22 additions & 10 deletions genai/tools/tools-func-desc-with-txt.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ 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(
async function generateFunctionDesc(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const ai = new GoogleGenAI({
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
Expand Down Expand Up @@ -71,23 +70,36 @@ async function generateContent(
trends in music consumption.
`;

const MODEL_NAME = 'gemini-2.5-flash';

const response = await ai.models.generateContent({
model: MODEL_NAME,
const response = await client.models.generateContent({
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);
// Example response:
// [FunctionCall(
// id=None,
// name="get_album_sales",
// args={
// "albums": [
// {"album_name": "Echoes of the Night", "copies_sold": 350000},
// {"copies_sold": 120000, "album_name": "Reckless Hearts"},
// {"copies_sold": 75000, "album_name": "Whispers of Dawn"},
// {"copies_sold": 100000, "album_name": "Street Symphony"},
// ]
// },
// )]

return response.functionCalls;
return output;
}

// [END googlegenaisdk_tools_func_desc_with_txt]

module.exports = {
generateContent,
generateFunctionDesc,
};
57 changes: 57 additions & 0 deletions genai/tools/tools-google-search-with-txt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 generateGoogleSearch(
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
});

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

console.log(response.text);

// Example response:
// 'The next total solar eclipse in Poland will occur on ...'

return response.text;
}

// [END googlegenaisdk_tools_google_search_with_txt]

module.exports = {
generateGoogleSearch,
};
Loading