-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(genai): add tool samples (3) #3283
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
2
commits into
GoogleCloudPlatform:main
Choose a base branch
from
jdomingr:genai-sdk-tool-samples-p3
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.
+299
−0
Open
Changes from all commits
Commits
Show all changes
2 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
38 changes: 38 additions & 0 deletions
38
genai/api/GenAI.Samples.Tests/Tools/ToolsEnterpriseWebSearchWithTxtTest.cs
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,38 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(GenAIFixture))] | ||
| public class ToolsEnterpriseWebSearchWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ToolsEnterpriseWebSearchWithTxt _sample; | ||
|
|
||
| public ToolsEnterpriseWebSearchWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ToolsEnterpriseWebSearchWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestToolsEnterpriseWebSearchWithTxt() | ||
| { | ||
| var response = await _sample.GenerateContent(_fixture.ProjectId); | ||
| Assert.NotEmpty(response); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
genai/api/GenAI.Samples.Tests/Tools/ToolsGoogleMapsCoordinatesWithTxtTest.cs
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,38 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(GenAIFixture))] | ||
| public class ToolsGoogleMapsCoordinatesWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ToolsGoogleMapsCoordinatesWithTxt _sample; | ||
|
|
||
| public ToolsGoogleMapsCoordinatesWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ToolsGoogleMapsCoordinatesWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestToolsGoogleMapsCoordinatesWithTxt() | ||
| { | ||
| var response = await _sample.GenerateContent(_fixture.ProjectId); | ||
| Assert.NotEmpty(response); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
genai/api/GenAI.Samples.Tests/Tools/ToolsGoogleSearchAndUrlContextWithTxtTest.cs
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,38 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(GenAIFixture))] | ||
| public class ToolsGoogleSearchAndUrlContextWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ToolsGoogleSearchAndUrlContextWithTxt _sample; | ||
|
|
||
| public ToolsGoogleSearchAndUrlContextWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ToolsGoogleSearchAndUrlContextWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestToolsGoogleSearchAndUrlContextWithTxt() | ||
| { | ||
| var response = await _sample.GenerateContent(_fixture.ProjectId); | ||
| Assert.NotEmpty(response); | ||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
56
genai/api/GenAI.Samples/Tools/ToolsEnterpriseWebSearchWithTxt.cs
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,56 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| // [START googlegenaisdk_tools_enterprise_web_search_with_txt] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class ToolsEnterpriseWebSearchWithTxt | ||
| { | ||
| public async Task<string> GenerateContent( | ||
| string projectId = "your-project-id", | ||
| string location = "global", | ||
| string model = "gemini-2.5-flash") | ||
| { | ||
| await using var client = new Client( | ||
| project: projectId, | ||
| location: location, | ||
| vertexAI: true, | ||
| httpOptions: new HttpOptions { ApiVersion = "v1" }); | ||
|
|
||
| GenerateContentResponse response = await client.Models.GenerateContentAsync( | ||
| model: model, | ||
| contents: "When is the next total solar eclipse in the United States", | ||
| config: new GenerateContentConfig | ||
| { | ||
| Tools = new List<Tool> | ||
| { | ||
| new Tool { EnterpriseWebSearch = new EnterpriseWebSearch() } | ||
| } | ||
| }); | ||
|
|
||
| string responseText = response.Candidates[0].Content.Parts[0].Text; | ||
| Console.WriteLine(responseText); | ||
| // Example response: | ||
| // The next total solar eclipse in the United States will occur on... | ||
| return responseText; | ||
| } | ||
| } | ||
| // [END googlegenaisdk_tools_enterprise_web_search_with_txt] | ||
65 changes: 65 additions & 0 deletions
65
genai/api/GenAI.Samples/Tools/ToolsGoogleMapsCoordinatesWithTxt.cs
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,65 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| // [START googlegenaisdk_tools_google_maps_coordinates_with_txt] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class ToolsGoogleMapsCoordinatesWithTxt | ||
| { | ||
| public async Task<string> GenerateContent( | ||
| string projectId = "your-project-id", | ||
| string location = "global", | ||
| string model = "gemini-2.5-flash") | ||
| { | ||
| await using var client = new Client( | ||
| project: projectId, | ||
| location: location, | ||
| vertexAI: true, | ||
| httpOptions: new HttpOptions { ApiVersion = "v1" }); | ||
|
|
||
| GenerateContentResponse response = await client.Models.GenerateContentAsync( | ||
| model: model, | ||
| contents: "Where can I get the best espresso near me?", | ||
| config: new GenerateContentConfig | ||
| { | ||
| Tools = new List<Tool> | ||
| { | ||
| new Tool { GoogleMaps = new GoogleMaps() } | ||
| }, | ||
| ToolConfig = new ToolConfig | ||
| { | ||
| RetrievalConfig = new RetrievalConfig | ||
| { | ||
| LatLng = new LatLng { Latitude = 40.7128, Longitude = -74.006 }, | ||
| LanguageCode = "en_US" | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| string responseText = response.Candidates[0].Content.Parts[0].Text; | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Console.WriteLine(responseText); | ||
| // Example response: | ||
| // Here are some of the top-rated coffee shops near you that are | ||
| // open now and known for their espresso... | ||
| return responseText; | ||
| } | ||
| } | ||
| // [END googlegenaisdk_tools_google_maps_coordinates_with_txt] | ||
64 changes: 64 additions & 0 deletions
64
genai/api/GenAI.Samples/Tools/ToolsGoogleSearchAndUrlContextWithTxt.cs
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,64 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| // [START googlegenaisdk_tools_google_search_and_urlcontext_with_txt] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class ToolsGoogleSearchAndUrlContextWithTxt | ||
| { | ||
| public async Task<string> GenerateContent( | ||
| string projectId = "your-project-id", | ||
| string location = "global", | ||
| string model = "gemini-2.5-flash") | ||
| { | ||
| await using var client = new Client( | ||
| project: projectId, | ||
| location: location, | ||
| vertexAI: true, | ||
| httpOptions: new HttpOptions { ApiVersion = "v1beta1" }); | ||
|
|
||
| string url = "https://www.google.com/search?q=events+in+New+York"; | ||
|
|
||
| GenerateContentResponse response = await client.Models.GenerateContentAsync( | ||
| model: model, | ||
| contents: $"Give me three day events schedule based on {url}." + | ||
| " Also let me know what needs to be taken care of considering weather and commute", | ||
| config: new GenerateContentConfig | ||
| { | ||
| Tools = new List<Tool> | ||
| { | ||
| new Tool { UrlContext = new UrlContext() }, | ||
| new Tool { GoogleSearch = new GoogleSearch() }, | ||
| }, | ||
| ResponseModalities = new List<string> { "TEXT" } | ||
| }); | ||
|
|
||
| string responseText = response.Candidates[0].Content.Parts[0].Text; | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Console.WriteLine(responseText); | ||
| // Example response: | ||
| // Here is a three-day event schedule for New York City in mid-December, | ||
| // along with important considerations for weather and commute: | ||
| // **Three - Day Event Schedule** | ||
| // **Day 1... | ||
| return responseText; | ||
| } | ||
| } | ||
| // [END googlegenaisdk_tools_google_search_and_urlcontext_with_txt] | ||
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.