Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
30 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
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
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