-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Guiners
wants to merge
12
commits into
GoogleCloudPlatform:main
Choose a base branch
from
Guiners:sample/tools
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+303
−10
Open
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
974533b
fixing tools-code-exec-with-txt.js
aea4c94
fixing code from code review and adding linters
6993543
fixing code from code review and adding linters
b3977ff
linting
debd161
Update genai/test/tools-google-search-with-txt.test.js
Guiners cfc452b
adding samples, test, lints
764b444
Merge remote-tracking branch 'origin/sample/tools' into sample/tools
6197a83
Merge branch 'main' into sample/tools
Guiners 92729b1
adding samples, test, lints
59fd49b
adding samples, test, lints
2ed95d2
adding samples, test, lints
b9dc1cc
Merge branch 'main' into sample/tools
Guiners File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () => { | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
it('should generate a function definition', async function () { | ||
this.timeout(10000); | ||
const output = await sample.generateContent(projectId); | ||
assert(output.length > 0); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () => { | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () => { | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
it('should generate a function call', async function () { | ||
this.timeout(60000); | ||
const output = await sample.generateContent(projectId); | ||
assert(output.length > 0); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
Guiners marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.