diff --git a/genai/test/test-data/640px-Monty_open_door.svg.png b/genai/test/test-data/640px-Monty_open_door.svg.png new file mode 100644 index 0000000000..90f83375e3 Binary files /dev/null and b/genai/test/test-data/640px-Monty_open_door.svg.png differ diff --git a/genai/test/tools-code-exec-with-txt-local-img.test.js b/genai/test/tools-code-exec-with-txt-local-img.test.js new file mode 100644 index 0000000000..d6d6538d88 --- /dev/null +++ b/genai/test/tools-code-exec-with-txt-local-img.test.js @@ -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); + }); +}); diff --git a/genai/test/tools-code-exec-with-txt.test.js b/genai/test/tools-code-exec-with-txt.test.js index 8ce7402e4d..da23b11b68 100644 --- a/genai/test/tools-code-exec-with-txt.test.js +++ b/genai/test/tools-code-exec-with-txt.test.js @@ -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); }); }); diff --git a/genai/test/tools-func-desc-with-txt.test.js b/genai/test/tools-func-desc-with-txt.test.js index 1ff31c89a7..6bb8852003 100644 --- a/genai/test/tools-func-desc-with-txt.test.js +++ b/genai/test/tools-func-desc-with-txt.test.js @@ -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); }); }); diff --git a/genai/test/tools-google-search-with-txt.test.js b/genai/test/tools-google-search-with-txt.test.js new file mode 100644 index 0000000000..7d3dd2fd40 --- /dev/null +++ b/genai/test/tools-google-search-with-txt.test.js @@ -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); + }); +}); diff --git a/genai/tools/tools-code-exec-with-txt-local-img.js b/genai/tools/tools-code-exec-with-txt-local-img.js new file mode 100644 index 0000000000..22a8cf0a03 --- /dev/null +++ b/genai/tools/tools-code-exec-with-txt-local-img.js @@ -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_exec_with_txt_local_img] +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_exec_with_txt_local_img] + +module.exports = { + generateAndExecuteMultimodalCode, +}; diff --git a/genai/tools/tools-code-exec-with-txt.js b/genai/tools/tools-code-exec-with-txt.js index 0d0b883ac8..8a6a137941 100644 --- a/genai/tools/tools-code-exec-with-txt.js +++ b/genai/tools/tools-code-exec-with-txt.js @@ -20,20 +20,20 @@ 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.', + 'Calculate 20th fibonacci number. Then find the nearest palindrome to it.', config: { tools: [{codeExecution: {}}], temperature: 0, @@ -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, }; diff --git a/genai/tools/tools-func-desc-with-txt.js b/genai/tools/tools-func-desc-with-txt.js index b416016ef6..f9b3c55036 100644 --- a/genai/tools/tools-func-desc-with-txt.js +++ b/genai/tools/tools-func-desc-with-txt.js @@ -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, @@ -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, }; diff --git a/genai/tools/tools-google-search-with-txt.js b/genai/tools/tools-google-search-with-txt.js new file mode 100644 index 0000000000..2b123944cd --- /dev/null +++ b/genai/tools/tools-google-search-with-txt.js @@ -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 the United States?', + config: { + tools: [ + { + googleSearch: {}, + }, + ], + }, + }); + + console.log(response.text); + + // Example response: + // 'The next total solar eclipse in United States will occur on ...' + + return response.text; +} + +// [END googlegenaisdk_tools_google_search_with_txt] + +module.exports = { + generateGoogleSearch, +};