-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(genai): add new tool samples #10191
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
jdomingr
wants to merge
4
commits into
GoogleCloudPlatform:main
Choose a base branch
from
jdomingr:genai-sdk-tool-samples-p2
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.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
76 changes: 76 additions & 0 deletions
76
genai/snippets/src/main/java/genai/tools/ToolsGoogleMapsCoordinatesWithTxt.java
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,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_google_maps_coordinates_with_txt] | ||
|
|
||
| import com.google.genai.Client; | ||
| import com.google.genai.types.GenerateContentConfig; | ||
| import com.google.genai.types.GenerateContentResponse; | ||
| import com.google.genai.types.GoogleMaps; | ||
| import com.google.genai.types.HttpOptions; | ||
| import com.google.genai.types.LatLng; | ||
| import com.google.genai.types.RetrievalConfig; | ||
| import com.google.genai.types.Tool; | ||
| import com.google.genai.types.ToolConfig; | ||
|
|
||
| public class ToolsGoogleMapsCoordinatesWithTxt { | ||
|
|
||
| public static void main(String[] args) { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| String modelId = "gemini-2.5-flash"; | ||
| generateContent(modelId); | ||
| } | ||
|
|
||
| // Generates content with Google Maps Tool. | ||
| public static String generateContent(String modelId) { | ||
| // Client Initialization. Once created, it can be reused for multiple requests. | ||
| try (Client client = | ||
| Client.builder() | ||
| .location("global") | ||
| .vertexAI(true) | ||
| .httpOptions(HttpOptions.builder().apiVersion("v1").build()) | ||
| .build()) { | ||
|
|
||
| // Set the Google Maps Tool. | ||
| Tool tool = Tool.builder().googleMaps(GoogleMaps.builder().build()).build(); | ||
|
|
||
| ToolConfig toolConfig = | ||
| ToolConfig.builder() | ||
| .retrievalConfig( | ||
| RetrievalConfig.builder() | ||
| // Pass coordinates for location-aware grounding | ||
| .latLng(LatLng.builder().latitude(40.7128).longitude(-74.006).build()) | ||
| // Localize Maps results | ||
| .languageCode("en_US") | ||
| .build()) | ||
| .build(); | ||
|
|
||
| GenerateContentResponse response = | ||
| client.models.generateContent( | ||
| modelId, | ||
| "Where can I get the best espresso near me?", | ||
| GenerateContentConfig.builder().tools(tool).toolConfig(toolConfig).build()); | ||
|
|
||
| System.out.println(response.text()); | ||
| // Example response: | ||
| // Here are some of the top-rated coffee shops near you that serve excellent espresso... | ||
| return response.text(); | ||
| } | ||
| } | ||
| } | ||
| // [END googlegenaisdk_tools_google_maps_coordinates_with_txt] |
84 changes: 84 additions & 0 deletions
84
genai/snippets/src/main/java/genai/tools/ToolsGoogleSearchAndUrlContextWithTxt.java
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,84 @@ | ||
| /* | ||
| * 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_and_urlcontext_with_txt] | ||
|
|
||
| import com.google.genai.Client; | ||
| import com.google.genai.types.Candidate; | ||
| import com.google.genai.types.Content; | ||
| 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; | ||
| import com.google.genai.types.UrlContext; | ||
| import java.util.List; | ||
|
|
||
| public class ToolsGoogleSearchAndUrlContextWithTxt { | ||
|
|
||
| public static void main(String[] args) { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| String modelId = "gemini-2.5-flash"; | ||
| String url = "https://www.google.com/search?q=events+in+New+York"; | ||
| generateContent(modelId, url); | ||
| } | ||
|
|
||
| // Generates content with the Url Context and Google Search Tools. | ||
| public static String generateContent(String modelId, String url) { | ||
| // Client Initialization. Once created, it can be reused for multiple requests. | ||
| try (Client client = | ||
| Client.builder() | ||
| .location("global") | ||
| .vertexAI(true) | ||
| .httpOptions(HttpOptions.builder().apiVersion("v1beta1").build()) | ||
| .build()) { | ||
|
|
||
| // Set the Url Context and Google Search tools. | ||
| List<Tool> tools = | ||
| List.of( | ||
| Tool.builder().urlContext(UrlContext.builder().build()).build(), | ||
| Tool.builder().googleSearch(GoogleSearch.builder().build()).build()); | ||
|
|
||
| String prompt = | ||
| String.format( | ||
| "Give me three day events schedule based on %s. Also let me know what" | ||
| + " needs to be taken care of considering weather and commute.", | ||
| url); | ||
|
|
||
| GenerateContentResponse response = | ||
| client.models.generateContent( | ||
| modelId, | ||
| prompt, | ||
| GenerateContentConfig.builder().tools(tools).responseModalities("TEXT").build()); | ||
|
|
||
| // Get response candidate and print parts of the response. | ||
| response | ||
| .candidates() | ||
| .flatMap(candidates -> candidates.stream().findFirst()) | ||
| .flatMap(Candidate::content) | ||
| .flatMap(Content::parts) | ||
| .ifPresent(parts -> parts.forEach(part -> part.text().ifPresent(System.out::println))); | ||
| // Example response: | ||
| // Three-Day Event Schedule in... | ||
| // **Day 1: Friday, October 24, 2025** | ||
| // * **Evening Event:** Attend the **2025... | ||
| return response.text(); | ||
| } | ||
| } | ||
| } | ||
| // [END googlegenaisdk_tools_google_search_and_urlcontext_with_txt] | ||
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
90 changes: 90 additions & 0 deletions
90
genai/snippets/src/main/java/genai/tools/ToolsUrlContextWithTxt.java
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 | ||
| * | ||
| * 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_urlcontext_with_txt] | ||
|
|
||
| import com.google.genai.Client; | ||
| import com.google.genai.types.Candidate; | ||
| 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.Tool; | ||
| import com.google.genai.types.UrlContext; | ||
|
|
||
| public class ToolsUrlContextWithTxt { | ||
|
|
||
| public static void main(String[] args) { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| String modelId = "gemini-2.5-flash"; | ||
| String url1 = "https://cloud.google.com/vertex-ai/generative-ai/docs"; | ||
| String url2 = "https://cloud.google.com/docs/overview"; | ||
| generateContent(modelId, url1, url2); | ||
| } | ||
|
|
||
| // Generates text with the Url Context Tool. | ||
| public static String generateContent(String modelId, String url1, String url2) { | ||
| // Client Initialization. Once created, it can be reused for multiple requests. | ||
| try (Client client = | ||
| Client.builder() | ||
| .location("global") | ||
| .vertexAI(true) | ||
| .httpOptions(HttpOptions.builder().apiVersion("v1").build()) | ||
| .build()) { | ||
|
|
||
| String prompt = | ||
| String.format("Compare the content, purpose, and audiences of %s and %s", url1, url2); | ||
|
|
||
| // Set the Url Context Tool. | ||
| Tool tool = Tool.builder().urlContext(UrlContext.builder().build()).build(); | ||
|
|
||
| GenerateContentResponse response = | ||
| client.models.generateContent( | ||
| modelId, | ||
| prompt, | ||
| GenerateContentConfig.builder().tools(tool).responseModalities("TEXT").build()); | ||
|
|
||
| // Get response candidate. | ||
| Candidate candidate = | ||
| response | ||
| .candidates() | ||
| .flatMap(candidates -> candidates.stream().findFirst()) | ||
| .orElseThrow( | ||
| () -> new IllegalStateException("No response candidate generated by the model.")); | ||
|
|
||
| // Print parts of the response. | ||
| candidate | ||
| .content() | ||
| .flatMap(Content::parts) | ||
| .ifPresent(parts -> parts.forEach(part -> part.text().ifPresent(System.out::println))); | ||
| // Example response: | ||
| // The two Google Cloud documentation pages serve distinct purposes and cater to different | ||
| // audiences within the broader Google Cloud ecosystem | ||
|
|
||
| candidate.urlContextMetadata().ifPresent(System.out::println); | ||
| // Example response: | ||
| // UrlContextMetadata{urlMetadata=Optional[[UrlMetadata{ | ||
| // retrievedUrl=Optional[https://cloud.google.com/vertex-ai/generative-ai/docs], | ||
| // urlRetrievalStatus=Optional[URL_RETRIEVAL_STATUS_SUCCESS]}, | ||
| // UrlMetadata{retrievedUrl=Optional[https://cloud.google.com/docs/overview], | ||
| // urlRetrievalStatus=Optional[URL_RETRIEVAL_STATUS_SUCCESS]}]]} | ||
| return response.text(); | ||
| } | ||
| } | ||
| } | ||
| // [END googlegenaisdk_tools_urlcontext_with_txt] |
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
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.