-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(genai): add tool samples (1) #3277
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
3
commits into
GoogleCloudPlatform:main
Choose a base branch
from
jdomingr:genai-sdk-tool-samples-p1
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 1 commit
Commits
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/ToolsCodeExecWithTxtTest.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 ToolsCodeExecWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ToolsCodeExecWithTxt _sample; | ||
|
|
||
| public ToolsCodeExecWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ToolsCodeExecWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestToolsCodeExecWithTxt() | ||
| { | ||
| var response = await _sample.GenerateContent(_fixture.ProjectId); | ||
| Assert.NotEmpty(response); | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
genai/api/GenAI.Samples.Tests/Tools/ToolsFuncDescWithTxtTest.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,41 @@ | ||
| /* | ||
| * 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 Google.GenAI.Types; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(GenAIFixture))] | ||
| public class ToolsFuncDescWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ToolsFuncDescWithTxt _sample; | ||
|
|
||
| public ToolsFuncDescWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ToolsFuncDescWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestToolsFuncDescWithTxt() | ||
| { | ||
| FunctionCall response = await _sample.GenerateContent(_fixture.ProjectId); | ||
| Assert.NotNull(response); | ||
| Assert.NotEmpty(response.Name); | ||
| Assert.NotEmpty(response.Args); | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
genai/api/GenAI.Samples.Tests/Tools/ToolsGoogleSearchWithTxtTest.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,40 @@ | ||
| /* | ||
| * 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 Google.GenAI.Types; | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(GenAIFixture))] | ||
| public class ToolsGoogleSearchWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ToolsGoogleSearchWithTxt _sample; | ||
|
|
||
| public ToolsGoogleSearchWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ToolsGoogleSearchWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestToolsGoogleSearchWithTxt() | ||
| { | ||
| var response = await _sample.GenerateContent(_fixture.ProjectId); | ||
| Assert.NotEmpty(response); | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
genai/api/GenAI.Samples.Tests/Tools/ToolsUrlContextWithTxtTest.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,40 @@ | ||
| /* | ||
| * 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 Google.GenAI.Types; | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| [Collection(nameof(GenAIFixture))] | ||
| public class ToolsUrlContextWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ToolsUrlContextWithTxt _sample; | ||
|
|
||
| public ToolsUrlContextWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ToolsUrlContextWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestToolsUrlContextWithTxt() | ||
| { | ||
| var response = await _sample.GenerateContent(_fixture.ProjectId); | ||
| Assert.NotEmpty(response); | ||
| } | ||
| } |
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,96 @@ | ||
| /* | ||
| * 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_code_exec_with_txt] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class ToolsCodeExecWithTxt | ||
| { | ||
| 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: "Calculate 20th fibonacci number. Then find the nearest palindrome to it.", | ||
| config: new GenerateContentConfig | ||
| { | ||
| Tools = new List<Tool> { new Tool { CodeExecution = new ToolCodeExecution() } }, | ||
| Temperature = 0 | ||
| }); | ||
|
|
||
| List<Part> parts = response.Candidates?[0]?.Content?.Parts ?? new List<Part>(); | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| StringBuilder responseText = new StringBuilder(); | ||
|
|
||
| foreach (Part part in parts) | ||
| { | ||
| if (part.Text != null) | ||
| { | ||
| Console.WriteLine($"Text: \n{part.Text}"); | ||
| responseText.AppendLine(part.Text); | ||
| } | ||
|
|
||
| if (part.ExecutableCode != null) | ||
| { | ||
| Console.WriteLine($"Code: \n{part.ExecutableCode}"); | ||
| } | ||
|
|
||
| if (part.CodeExecutionResult != null) | ||
| { | ||
| Console.WriteLine($"Outcome: \n{part.CodeExecutionResult}"); | ||
| } | ||
| } | ||
|
|
||
| // Example response: | ||
| // Text: | ||
| // o calculate the 20th Fibonacci number and find its nearest palindrome, I will perform the following steps: | ||
| // 1. * *Calculate the 20th Fibonacci number: **I will use a Python function to compute the Fibonacci sequence. | ||
| // ... | ||
| // Code: | ||
| // ExecutableCode { | ||
| // Code = def fibonacci(n): | ||
| // a, b = 0, 1 | ||
| // for _ in range(n): | ||
| // a, b = b, a + b | ||
| // return a | ||
| // fib_20 = fibonacci(20) | ||
| // print(f'{fib_20=}') | ||
| // , Language = PYTHON } | ||
| // | ||
| // Outcome: | ||
| // CodeExecutionResult { Outcome = OUTCOME_OK, Output = fib_20 = 6765 } | ||
| // | ||
| // Code: | ||
| // ExecutableCode { | ||
| // Code = def is_palindrome(n): | ||
| // ... | ||
| return responseText.ToString(); | ||
| } | ||
| } | ||
| // [END googlegenaisdk_tools_code_exec_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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| * 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_func_desc_with_txt] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class ToolsFuncDescWithTxt | ||
| { | ||
| public async Task<FunctionCall> GenerateContent( | ||
| string projectId = "your-project-id", | ||
| string location = "global", | ||
| string model = "gemini-2.5-flash") | ||
| { | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| await using var client = new Client( | ||
| project: projectId, | ||
| location: location, | ||
| vertexAI: true, | ||
| httpOptions: new HttpOptions { ApiVersion = "v1" }); | ||
|
|
||
| List<FunctionDeclaration> getAlbumSales = new List<FunctionDeclaration> | ||
| { | ||
| new FunctionDeclaration | ||
| { | ||
| Name = "get_album_sales", | ||
| Description = "Gets the number of albums sold", | ||
| Parameters = new Schema | ||
jdomingr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| Type = Google.GenAI.Types.Type.OBJECT, | ||
| Properties = new Dictionary<string, Schema> | ||
| { | ||
| { | ||
| "albums", | ||
| new Schema | ||
| { | ||
| Type = Google.GenAI.Types.Type.ARRAY, | ||
| Description = "List of albums", | ||
| Items = new Schema | ||
| { | ||
| Type = Google.GenAI.Types.Type.OBJECT, | ||
| Description = "Album and its sales", | ||
| Properties = new Dictionary<string, Schema> | ||
| { | ||
| { | ||
| "album_name", | ||
| new Schema | ||
| { | ||
| Type = Google.GenAI.Types.Type.STRING, | ||
| Description = "Name of the music album" | ||
| } | ||
| }, | ||
| { | ||
| "copies_sold", | ||
| new Schema | ||
| { | ||
| Type = Google.GenAI.Types.Type.INTEGER, | ||
| Description = "Number of copies sold" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| string contents = "At Stellar Sounds, a music label, 2024 was a rollercoaster." + | ||
| " \"Echoes of the Night,\" a debut synth-pop album, surprisingly sold 350,000 copies," + | ||
| " while veteran rock band \"Crimson Tide\\'s\" latest, \"Reckless Hearts,\" lagged at 120,000." + | ||
| " Their up-and-coming indie artist, \"Luna Bloom\\'s\" EP, \"Whispers of Dawn,\" secured 75,000 sales." + | ||
| " The biggest disappointment was the highly-anticipated rap album \"Street Symphony\" only reaching 100,000" + | ||
| " units. Overall, Stellar Sounds moved over 645,000 units this year, revealing unexpected trends in music consumption."; | ||
|
|
||
| GenerateContentResponse response = await client.Models.GenerateContentAsync( | ||
| model: model, | ||
| contents: contents, | ||
| config: new GenerateContentConfig | ||
| { | ||
| Temperature = 0, | ||
| Tools = new List<Tool> | ||
| { | ||
| new Tool { FunctionDeclarations = getAlbumSales } | ||
| } | ||
| }); | ||
|
|
||
| FunctionCall functionCall = response.Candidates[0].Content.Parts[0].FunctionCall; | ||
|
|
||
| Console.WriteLine($"Name: {functionCall.Name}"); | ||
|
|
||
| foreach (var parameter in functionCall.Args) | ||
| { | ||
| Console.WriteLine(parameter); | ||
| } | ||
| // Example output: | ||
| // Name: get_album_sales | ||
| // | ||
| // [albums, [{"album_name": "Echoes of the Night", "copies_sold": 350000}, | ||
| // {"album_name": "Reckless Hearts", "copies_sold": 120000}, | ||
| // {"copies_sold": 75000, "album_name": "Whispers of Dawn"}, | ||
| // {"album_name": "Street Symphony","copies_sold": 100000}]] | ||
| return functionCall; | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| // [END googlegenaisdk_tools_func_desc_with_txt] | ||
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.