From 8b7599053664d6abd3e87f6946cc993784bbc6cf Mon Sep 17 00:00:00 2001 From: Juan Dominguez Date: Wed, 13 Aug 2025 17:12:40 -0400 Subject: [PATCH 1/8] feat: add new GenAI tools samples --- .../resources/640px-Monty_open_door.svg.png | Bin 0 -> 24719 bytes .../genai/tools/ToolsCodeExecWithText.java | 87 +++++++++++++++ .../ToolsCodeExecWithTextLocalImage.java | 101 ++++++++++++++++++ .../tools/ToolsGoogleSearchWithText.java | 63 +++++++++++ .../java/genai/tools/ToolsVaisWithText.java | 76 +++++++++++++ .../src/test/java/genai/tools/ToolsIT.java | 36 +++++++ 6 files changed, 363 insertions(+) create mode 100644 genai/snippets/resources/640px-Monty_open_door.svg.png create mode 100644 genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithText.java create mode 100644 genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithTextLocalImage.java create mode 100644 genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchWithText.java create mode 100644 genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java diff --git a/genai/snippets/resources/640px-Monty_open_door.svg.png b/genai/snippets/resources/640px-Monty_open_door.svg.png new file mode 100644 index 0000000000000000000000000000000000000000..90f83375e36651f988e99720e3274fc4c2b0f878 GIT binary patch literal 24719 zcmagFcRbbq`#*l{WRty;m5hU9MM#o8ict36n{4OEjEs=ILS&R z^KcyBr`P-Q`}=q6c2viCKA+d)y6)GyU)OnKpr=lClkFx51ftT^c>D|mB7}iJ1ok&b zfp_2s3HN}%h@Dh)RY0J+6bhUT3Gn~hb{fxgL7)%;5C|3x0-XbI!L~u50C5oLk2MG+ zpA7;rd*^?9`T+O^sjas9W6;(0pW=>+bl@E_e@$IAvTagYN;>kvn8RWa$SPg)v5Ha9 z++IOo5Xa12|sL55o_FzVIvHye0bachQJY1 z2xQS(o|M^r-Sfr%y{|Qv%_FD%HB)V_ZPnj40yon-hbPG zmSQ{8i8F5eZhd?9w3*qP?13k#F%e0ZW0#SS8jU6aw1S}2h_8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olsCodeExecWithText.java new file mode 100644 index 00000000000..e1b9f147147 --- /dev/null +++ b/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithText.java @@ -0,0 +1,87 @@ +/* + * 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 + * + * http://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. + */ + + +package genai.tools; + +// [START googlegenaisdk_tools_code_exec_with_txt] + +import com.google.genai.Client; +import com.google.genai.types.GenerateContentConfig; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.HttpOptions; +import com.google.genai.types.Tool; +import com.google.genai.types.ToolCodeExecution; + +public class ToolsCodeExecWithText { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + String modelId = "gemini-2.5-flash"; + generateContent(modelId); + } + + // Generates text using the Code Execution tool + public static String generateContent(String modelId) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + try (Client client = + Client.builder() + .location("global") + .vertexAI(true) + .httpOptions(HttpOptions.builder().apiVersion("v1").build()) + .build()) { + + // Create a config and set codeExecution tool + GenerateContentConfig contentConfig = + GenerateContentConfig.builder() + .tools(Tool.builder().codeExecution(ToolCodeExecution.builder().build()).build()) + .temperature(0.0F) + .build(); + + GenerateContentResponse response = + client.models.generateContent( + modelId, + "Calculate 20th fibonacci number. Then find the nearest palindrome to it.", + contentConfig); + + System.out.println("Code: "); + System.out.print(response.executableCode()); + System.out.println("Outcome: "); + System.out.print(response.codeExecutionResult()); + // Example response + // Code: + // def fibonacci(n): + // if n <= 0: + // return 0 + // elif n == 1: + // return 1 + // else: + // a, b = 1, 1 + // for _ in range(2, n): + // a, b = b, a + b + // return b + // + // fib_20 = fibonacci(20) + // print(f'{fib_20=}') + // + // Outcome: + // fib_20=6765 + return response.executableCode(); + } + } +} +// [END googlegenaisdk_tools_code_exec_with_txt] \ No newline at end of file diff --git a/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithTextLocalImage.java b/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithTextLocalImage.java new file mode 100644 index 00000000000..bf81977d9ed --- /dev/null +++ b/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithTextLocalImage.java @@ -0,0 +1,101 @@ +/* + * 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 + * + * http://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. + */ + +package genai.tools; + +// [START googlegenaisdk_tools_code_exec_with_txt_local_img] + +import com.google.genai.Client; +import com.google.genai.types.Content; +import com.google.genai.types.GenerateContentConfig; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.HttpOptions; +import com.google.genai.types.Part; +import com.google.genai.types.Tool; +import com.google.genai.types.ToolCodeExecution; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +public class ToolsCodeExecWithTextLocalImage { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String modelId = "gemini-2.5-flash"; + String localImagePath = "your-local-image.png"; + generateContent(modelId, localImagePath); + } + + // Generates text using the Code Execution tool with text and image input + public static String generateContent(String modelId, String localImagePath) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + try (Client client = + Client.builder() + .location("global") + .vertexAI(true) + .httpOptions(HttpOptions.builder().apiVersion("v1").build()) + .build()) { + + String prompt = + "Run a simulation of the Monty Hall Problem with 1,000 trials.\n" + + "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?\n" + + " 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.\n" + + " Thank you!"; + + // Read content from a local image + byte[] imageData = Files.readAllBytes(Paths.get(localImagePath)); + + // Create a config and set codeExecution tool + GenerateContentConfig contentConfig = + GenerateContentConfig.builder() + .tools(Tool.builder().codeExecution(ToolCodeExecution.builder().build()).build()) + .temperature(0.0F) + .build(); + + GenerateContentResponse response = + client.models.generateContent( + modelId, + Content.fromParts(Part.fromText(prompt), Part.fromBytes(imageData, "image/png")), + contentConfig); + + System.out.println("Code: \n" + response.executableCode()); + System.out.println("Outcome: \n" + response.codeExecutionResult()); + // Example response + // Code: + // import random + // + // def run_monty_hall_trial(): + // doors = [0, 1, 2] # Represent doors as indices 0, 1, 2 + // + // # 1. Randomly place the car behind one door + // car_door = random.choice(doors) + // ... + // + // Outcome: + // Number of trials: 1000 + // Stick strategy wins: 327 (32.70%) + // Switch strategy wins: 673 (67.30%) + return response.executableCode(); + } + } +} +// [END googlegenaisdk_tools_code_exec_with_txt_local_img] \ No newline at end of file diff --git a/genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchWithText.java b/genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchWithText.java new file mode 100644 index 00000000000..89e15cc750d --- /dev/null +++ b/genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchWithText.java @@ -0,0 +1,63 @@ +/* + * 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 + * + * http://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. + */ + +package genai.tools; + +// [START googlegenaisdk_tools_google_search_with_txt] + +import com.google.genai.Client; +import com.google.genai.types.GenerateContentConfig; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.GoogleSearch; +import com.google.genai.types.HttpOptions; +import com.google.genai.types.Tool; + +public class ToolsGoogleSearchWithText { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + String modelId = "gemini-2.5-flash"; + generateContent(modelId); + } + + // Generates text with Google Search tool + public static String generateContent(String modelId) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + try (Client client = + Client.builder() + .location("global") + .vertexAI(true) + .httpOptions(HttpOptions.builder().apiVersion("v1").build()) + .build()) { + + GenerateContentResponse response = + client.models.generateContent( + modelId, + "When is the next total solar eclipse in the United States?", + GenerateContentConfig.builder() + // set Google Search tool + .tools(Tool.builder().googleSearch(GoogleSearch.builder().build()).build()) + .build()); + + System.out.println(response.text()); + // Example response: + // The next total solar eclipse in the United States will occur on... + return response.text(); + } + } +} +// [END googlegenaisdk_tools_google_search_with_txt] \ No newline at end of file diff --git a/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java b/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java new file mode 100644 index 00000000000..a20973291ba --- /dev/null +++ b/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java @@ -0,0 +1,76 @@ +/* + * 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 + * + * http://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. + */ + +package genai.tools; + +// [START googlegenaisdk_tools_vais_with_txt] + +import com.google.genai.Client; +import com.google.genai.types.GenerateContentConfig; +import com.google.genai.types.GenerateContentResponse; +import com.google.genai.types.HttpOptions; +import com.google.genai.types.Retrieval; +import com.google.genai.types.Tool; +import com.google.genai.types.VertexAISearch; + +public class ToolsVaisWithText { + + public static void main(String[] args) { + // TODO(developer): Replace these variables before running the sample. + String modelId = "gemini-2.5-flash"; + // Load Data Store ID from Vertex AI Search + // E.g datastore = + // "projects/11111111/locations/global/collections/default_collection/dataStores/data-store-id" + String datastoreId = "your-datastore"; + generateContent(modelId, datastoreId); + } + + // Generates text with Vertex AI Search tool + public static String generateContent(String modelId, String datastoreId) { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + try (Client client = + Client.builder() + .location("global") + .vertexAI(true) + .httpOptions(HttpOptions.builder().apiVersion("v1").build()) + .build()) { + + // Set the VertexAI Search tool and the datastore that the model can use to retrieve data from + Tool vaisSearchTool = + Tool.builder() + .retrieval( + Retrieval.builder() + .vertexAiSearch(VertexAISearch.builder().datastore(datastoreId).build()) + .build()) + .build(); + + GenerateContentConfig config = + GenerateContentConfig.builder().tools(vaisSearchTool).build(); + + GenerateContentResponse response = + client.models.generateContent( + modelId, "How do I make an appointment to renew my driver's license?", config); + + System.out.println(response.text()); + // Example response: + // The process for making an appointment to renew your driver's license varies depending + // on your location. To provide you with the most accurate instructions... + return response.text(); + } + } +} +// [END googlegenaisdk_tools_vais_with_txt] \ No newline at end of file diff --git a/genai/snippets/src/test/java/genai/tools/ToolsIT.java b/genai/snippets/src/test/java/genai/tools/ToolsIT.java index b18fb4a9bc1..fc22ca70666 100644 --- a/genai/snippets/src/test/java/genai/tools/ToolsIT.java +++ b/genai/snippets/src/test/java/genai/tools/ToolsIT.java @@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertWithMessage; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; import org.junit.After; import org.junit.Before; @@ -32,6 +33,7 @@ public class ToolsIT { private static final String GEMINI_FLASH = "gemini-2.5-flash"; + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private ByteArrayOutputStream bout; private PrintStream out; @@ -54,9 +56,11 @@ public void setUp() { System.setOut(out); } + @After public void tearDown() { System.setOut(null); + bout.reset(); } @Test @@ -79,4 +83,36 @@ public void testGenerateContentWithFunctionDescription() { assertThat(response).contains("copies_sold=350000"); assertThat(response).contains("album_name=Echoes of the Night"); } + + @Test + public void testToolsCodeExecWithText() { + String response = ToolsCodeExecWithText.generateContent(GEMINI_FLASH); + assertThat(response).isNotEmpty(); + assertThat(bout.toString()).contains("Code:"); + assertThat(bout.toString()).contains("Outcome:"); + } + + @Test + public void testToolsCodeExecWithTextLocalImage() throws IOException { + String localImagePath = "resources/640px-Monty_open_door.svg.png"; + String response = ToolsCodeExecWithTextLocalImage.generateContent(GEMINI_FLASH, localImagePath); + assertThat(response).isNotEmpty(); + assertThat(bout.toString()).contains("Code:"); + assertThat(bout.toString()).contains("Outcome:"); + } + + @Test + public void testToolsGoogleSearchWithText() { + String response = ToolsGoogleSearchWithText.generateContent(GEMINI_FLASH); + assertThat(response).isNotEmpty(); + } + + @Test + public void testToolsVaisWithText() { + String datastore = "projects/" + PROJECT_ID + "/locations/global/" + + "collections/default_collection/dataStores/grounding-test-datastore"; + String response = ToolsVaisWithText.generateContent(GEMINI_FLASH, datastore); + assertThat(response).isNotEmpty(); + } + } From 57d0cce166f97507956cc81c12b0f6ec72253a52 Mon Sep 17 00:00:00 2001 From: Juan Dominguez Date: Thu, 14 Aug 2025 09:57:31 -0400 Subject: [PATCH 2/8] refactor: change some print statement in samples --- .../src/main/java/genai/tools/ToolsCodeExecWithText.java | 6 ++---- .../main/java/genai/tools/ToolsGoogleSearchWithText.java | 2 +- .../src/main/java/genai/tools/ToolsVaisWithText.java | 2 +- genai/snippets/src/test/java/genai/tools/ToolsIT.java | 1 - 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithText.java b/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithText.java index e1b9f147147..8de1df26436 100644 --- a/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithText.java +++ b/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithText.java @@ -58,10 +58,8 @@ public static String generateContent(String modelId) { "Calculate 20th fibonacci number. Then find the nearest palindrome to it.", contentConfig); - System.out.println("Code: "); - System.out.print(response.executableCode()); - System.out.println("Outcome: "); - System.out.print(response.codeExecutionResult()); + System.out.println("Code: \n" + response.executableCode()); + System.out.println("Outcome: \n" + response.codeExecutionResult()); // Example response // Code: // def fibonacci(n): diff --git a/genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchWithText.java b/genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchWithText.java index 89e15cc750d..bebe9368a30 100644 --- a/genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchWithText.java +++ b/genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchWithText.java @@ -53,7 +53,7 @@ public static String generateContent(String modelId) { .tools(Tool.builder().googleSearch(GoogleSearch.builder().build()).build()) .build()); - System.out.println(response.text()); + System.out.print(response.text()); // Example response: // The next total solar eclipse in the United States will occur on... return response.text(); diff --git a/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java b/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java index a20973291ba..0dfbb6b1b82 100644 --- a/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java +++ b/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java @@ -65,7 +65,7 @@ public static String generateContent(String modelId, String datastoreId) { client.models.generateContent( modelId, "How do I make an appointment to renew my driver's license?", config); - System.out.println(response.text()); + System.out.print(response.text()); // Example response: // The process for making an appointment to renew your driver's license varies depending // on your location. To provide you with the most accurate instructions... diff --git a/genai/snippets/src/test/java/genai/tools/ToolsIT.java b/genai/snippets/src/test/java/genai/tools/ToolsIT.java index fc22ca70666..9e08c8a297f 100644 --- a/genai/snippets/src/test/java/genai/tools/ToolsIT.java +++ b/genai/snippets/src/test/java/genai/tools/ToolsIT.java @@ -56,7 +56,6 @@ public void setUp() { System.setOut(out); } - @After public void tearDown() { System.setOut(null); From 65d28ef610f60f28fe6529eeec309762010b971f Mon Sep 17 00:00:00 2001 From: Juan Dominguez Date: Thu, 14 Aug 2025 10:32:06 -0400 Subject: [PATCH 3/8] refactor: introduce minor changes to improve consistency --- .../java/genai/tools/ToolsGoogleSearchWithText.java | 13 +++++++------ .../main/java/genai/tools/ToolsVaisWithText.java | 6 +++--- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchWithText.java b/genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchWithText.java index bebe9368a30..a15fdde1007 100644 --- a/genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchWithText.java +++ b/genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchWithText.java @@ -44,14 +44,15 @@ public static String generateContent(String modelId) { .httpOptions(HttpOptions.builder().apiVersion("v1").build()) .build()) { + // Create a config and set Google Search tool + GenerateContentConfig contentConfig = + GenerateContentConfig.builder() + .tools(Tool.builder().googleSearch(GoogleSearch.builder().build()).build()) + .build(); + GenerateContentResponse response = client.models.generateContent( - modelId, - "When is the next total solar eclipse in the United States?", - GenerateContentConfig.builder() - // set Google Search tool - .tools(Tool.builder().googleSearch(GoogleSearch.builder().build()).build()) - .build()); + modelId, "When is the next total solar eclipse in the United States?", contentConfig); System.out.print(response.text()); // Example response: diff --git a/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java b/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java index 0dfbb6b1b82..9bf4e328f18 100644 --- a/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java +++ b/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java @@ -58,12 +58,12 @@ public static String generateContent(String modelId, String datastoreId) { .build()) .build(); - GenerateContentConfig config = - GenerateContentConfig.builder().tools(vaisSearchTool).build(); + GenerateContentConfig contentConfig = + GenerateContentConfig.builder().tools(vaisSearchTool).build(); GenerateContentResponse response = client.models.generateContent( - modelId, "How do I make an appointment to renew my driver's license?", config); + modelId, "How do I make an appointment to renew my driver's license?", contentConfig); System.out.print(response.text()); // Example response: From dba90410200422ca5eb867317b83d068d8bf8dd1 Mon Sep 17 00:00:00 2001 From: Juan Dominguez Date: Mon, 18 Aug 2025 15:47:52 -0400 Subject: [PATCH 4/8] Improve comments in toolVais samples --- .../snippets/src/main/java/genai/tools/ToolsVaisWithText.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java b/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java index 9bf4e328f18..6f71ae4e0f9 100644 --- a/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java +++ b/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java @@ -32,8 +32,8 @@ public static void main(String[] args) { // TODO(developer): Replace these variables before running the sample. String modelId = "gemini-2.5-flash"; // Load Data Store ID from Vertex AI Search - // E.g datastore = - // "projects/11111111/locations/global/collections/default_collection/dataStores/data-store-id" + // E.g datastoreId = + // "projects/project-id/locations/global/collections/default_collection/dataStores/datastore-id" String datastoreId = "your-datastore"; generateContent(modelId, datastoreId); } From 42a61753c5f6ce6f38c86452ae39525eea232f48 Mon Sep 17 00:00:00 2001 From: Juan Dominguez Date: Mon, 18 Aug 2025 15:54:04 -0400 Subject: [PATCH 5/8] delete unnecessary line break --- .../src/main/java/genai/tools/ToolsCodeExecWithText.java | 1 - 1 file changed, 1 deletion(-) diff --git a/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithText.java b/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithText.java index 8de1df26436..97a73475c68 100644 --- a/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithText.java +++ b/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithText.java @@ -14,7 +14,6 @@ * limitations under the License. */ - package genai.tools; // [START googlegenaisdk_tools_code_exec_with_txt] From 638f19ad17822f5a8e7735bb64d886583a30ccd9 Mon Sep 17 00:00:00 2001 From: Juan Dominguez Date: Tue, 19 Aug 2025 15:33:06 -0400 Subject: [PATCH 6/8] refactor: change inputs order and improve comments in some samples --- .../src/main/java/genai/tools/ToolsCodeExecWithText.java | 2 +- .../java/genai/tools/ToolsCodeExecWithTextLocalImage.java | 6 +++--- .../main/java/genai/tools/ToolsGoogleSearchWithText.java | 2 +- .../src/main/java/genai/tools/ToolsVaisWithText.java | 1 + 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithText.java b/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithText.java index 97a73475c68..ce6d106c13c 100644 --- a/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithText.java +++ b/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithText.java @@ -44,7 +44,7 @@ public static String generateContent(String modelId) { .httpOptions(HttpOptions.builder().apiVersion("v1").build()) .build()) { - // Create a config and set codeExecution tool + // Create a GenerateContentConfig and set codeExecution tool GenerateContentConfig contentConfig = GenerateContentConfig.builder() .tools(Tool.builder().codeExecution(ToolCodeExecution.builder().build()).build()) diff --git a/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithTextLocalImage.java b/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithTextLocalImage.java index bf81977d9ed..08858eca013 100644 --- a/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithTextLocalImage.java +++ b/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithTextLocalImage.java @@ -61,10 +61,10 @@ public static String generateContent(String modelId, String localImagePath) thro + " best strategy is.\n" + " Thank you!"; - // Read content from a local image + // Read content from the local image byte[] imageData = Files.readAllBytes(Paths.get(localImagePath)); - // Create a config and set codeExecution tool + // Create a GenerateContentConfig and set codeExecution tool GenerateContentConfig contentConfig = GenerateContentConfig.builder() .tools(Tool.builder().codeExecution(ToolCodeExecution.builder().build()).build()) @@ -74,7 +74,7 @@ public static String generateContent(String modelId, String localImagePath) thro GenerateContentResponse response = client.models.generateContent( modelId, - Content.fromParts(Part.fromText(prompt), Part.fromBytes(imageData, "image/png")), + Content.fromParts(Part.fromBytes(imageData, "image/png"), Part.fromText(prompt)), contentConfig); System.out.println("Code: \n" + response.executableCode()); diff --git a/genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchWithText.java b/genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchWithText.java index a15fdde1007..7c053dbd1e7 100644 --- a/genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchWithText.java +++ b/genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchWithText.java @@ -44,7 +44,7 @@ public static String generateContent(String modelId) { .httpOptions(HttpOptions.builder().apiVersion("v1").build()) .build()) { - // Create a config and set Google Search tool + // Create a GenerateContentConfig and set Google Search tool GenerateContentConfig contentConfig = GenerateContentConfig.builder() .tools(Tool.builder().googleSearch(GoogleSearch.builder().build()).build()) diff --git a/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java b/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java index 6f71ae4e0f9..b3eb0c4e285 100644 --- a/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java +++ b/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java @@ -58,6 +58,7 @@ public static String generateContent(String modelId, String datastoreId) { .build()) .build(); + // Create a GenerateContentConfig and set the Vertex AI Search tool GenerateContentConfig contentConfig = GenerateContentConfig.builder().tools(vaisSearchTool).build(); From a5270d6ab9e9c8832f5092e1014f700989f870a9 Mon Sep 17 00:00:00 2001 From: Juan Dominguez Date: Thu, 21 Aug 2025 10:48:14 -0400 Subject: [PATCH 7/8] delete Vertex AI tool sample to take it to a new PR --- .../java/genai/tools/ToolsVaisWithText.java | 77 ------------------- .../src/test/java/genai/tools/ToolsIT.java | 8 -- 2 files changed, 85 deletions(-) delete mode 100644 genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java diff --git a/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java b/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java deleted file mode 100644 index b3eb0c4e285..00000000000 --- a/genai/snippets/src/main/java/genai/tools/ToolsVaisWithText.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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 - * - * http://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. - */ - -package genai.tools; - -// [START googlegenaisdk_tools_vais_with_txt] - -import com.google.genai.Client; -import com.google.genai.types.GenerateContentConfig; -import com.google.genai.types.GenerateContentResponse; -import com.google.genai.types.HttpOptions; -import com.google.genai.types.Retrieval; -import com.google.genai.types.Tool; -import com.google.genai.types.VertexAISearch; - -public class ToolsVaisWithText { - - public static void main(String[] args) { - // TODO(developer): Replace these variables before running the sample. - String modelId = "gemini-2.5-flash"; - // Load Data Store ID from Vertex AI Search - // E.g datastoreId = - // "projects/project-id/locations/global/collections/default_collection/dataStores/datastore-id" - String datastoreId = "your-datastore"; - generateContent(modelId, datastoreId); - } - - // Generates text with Vertex AI Search tool - public static String generateContent(String modelId, String datastoreId) { - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. - try (Client client = - Client.builder() - .location("global") - .vertexAI(true) - .httpOptions(HttpOptions.builder().apiVersion("v1").build()) - .build()) { - - // Set the VertexAI Search tool and the datastore that the model can use to retrieve data from - Tool vaisSearchTool = - Tool.builder() - .retrieval( - Retrieval.builder() - .vertexAiSearch(VertexAISearch.builder().datastore(datastoreId).build()) - .build()) - .build(); - - // Create a GenerateContentConfig and set the Vertex AI Search tool - GenerateContentConfig contentConfig = - GenerateContentConfig.builder().tools(vaisSearchTool).build(); - - GenerateContentResponse response = - client.models.generateContent( - modelId, "How do I make an appointment to renew my driver's license?", contentConfig); - - System.out.print(response.text()); - // Example response: - // The process for making an appointment to renew your driver's license varies depending - // on your location. To provide you with the most accurate instructions... - return response.text(); - } - } -} -// [END googlegenaisdk_tools_vais_with_txt] \ No newline at end of file diff --git a/genai/snippets/src/test/java/genai/tools/ToolsIT.java b/genai/snippets/src/test/java/genai/tools/ToolsIT.java index 9e08c8a297f..c1127aae103 100644 --- a/genai/snippets/src/test/java/genai/tools/ToolsIT.java +++ b/genai/snippets/src/test/java/genai/tools/ToolsIT.java @@ -106,12 +106,4 @@ public void testToolsGoogleSearchWithText() { assertThat(response).isNotEmpty(); } - @Test - public void testToolsVaisWithText() { - String datastore = "projects/" + PROJECT_ID + "/locations/global/" - + "collections/default_collection/dataStores/grounding-test-datastore"; - String response = ToolsVaisWithText.generateContent(GEMINI_FLASH, datastore); - assertThat(response).isNotEmpty(); - } - } From 96081ee64bd05fc300245d91776caf2472b618de Mon Sep 17 00:00:00 2001 From: Juan Dominguez Date: Tue, 2 Sep 2025 12:53:49 -0400 Subject: [PATCH 8/8] refactor(genai): use image directly in CodeExecWithTextLocalImages sample and add image source --- .../java/genai/tools/ToolsCodeExecWithTextLocalImage.java | 8 ++++---- genai/snippets/src/test/java/genai/tools/ToolsIT.java | 5 ++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithTextLocalImage.java b/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithTextLocalImage.java index 08858eca013..6647889888a 100644 --- a/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithTextLocalImage.java +++ b/genai/snippets/src/main/java/genai/tools/ToolsCodeExecWithTextLocalImage.java @@ -35,12 +35,11 @@ public class ToolsCodeExecWithTextLocalImage { public static void main(String[] args) throws IOException { // TODO(developer): Replace these variables before running the sample. String modelId = "gemini-2.5-flash"; - String localImagePath = "your-local-image.png"; - generateContent(modelId, localImagePath); + generateContent(modelId); } // Generates text using the Code Execution tool with text and image input - public static String generateContent(String modelId, String localImagePath) throws IOException { + public static String generateContent(String modelId) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. try (Client client = @@ -62,7 +61,8 @@ public static String generateContent(String modelId, String localImagePath) thro + " Thank you!"; // Read content from the local image - byte[] imageData = Files.readAllBytes(Paths.get(localImagePath)); + // Image source: https://upload.wikimedia.org/wikipedia/commons/thumb/3/3f/Monty_open_door.svg/640px-Monty_open_door.svg.png + byte[] imageData = Files.readAllBytes(Paths.get("resources/640px-Monty_open_door.svg.png")); // Create a GenerateContentConfig and set codeExecution tool GenerateContentConfig contentConfig = diff --git a/genai/snippets/src/test/java/genai/tools/ToolsIT.java b/genai/snippets/src/test/java/genai/tools/ToolsIT.java index c1127aae103..dbe421694f1 100644 --- a/genai/snippets/src/test/java/genai/tools/ToolsIT.java +++ b/genai/snippets/src/test/java/genai/tools/ToolsIT.java @@ -93,8 +93,7 @@ public void testToolsCodeExecWithText() { @Test public void testToolsCodeExecWithTextLocalImage() throws IOException { - String localImagePath = "resources/640px-Monty_open_door.svg.png"; - String response = ToolsCodeExecWithTextLocalImage.generateContent(GEMINI_FLASH, localImagePath); + String response = ToolsCodeExecWithTextLocalImage.generateContent(GEMINI_FLASH); assertThat(response).isNotEmpty(); assertThat(bout.toString()).contains("Code:"); assertThat(bout.toString()).contains("Outcome:"); @@ -106,4 +105,4 @@ public void testToolsGoogleSearchWithText() { assertThat(response).isNotEmpty(); } -} +} \ No newline at end of file