-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(genai): add thinking samples #3275
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-thinking-samples
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 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/Thinking/ThinkingBudgetWithTxtTest.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 ThinkingBudgetWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ThinkingBudgetWithTxt _sample; | ||
|
|
||
| public ThinkingBudgetWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ThinkingBudgetWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestThinkingBudgetWithTxt() | ||
| { | ||
| var response = await _sample.GenerateContent(_fixture.ProjectId); | ||
| Assert.NotEmpty(response); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
genai/api/GenAI.Samples.Tests/Thinking/ThinkingIncludeThoughtsWithTxtTest.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 ThinkingIncludeThoughtsWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ThinkingIncludeThoughtsWithTxt _sample; | ||
|
|
||
| public ThinkingIncludeThoughtsWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ThinkingIncludeThoughtsWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestThinkingIncludeThoughtsWithTxt() | ||
| { | ||
| var response = await _sample.GenerateContent(_fixture.ProjectId); | ||
| Assert.NotEmpty(response); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
genai/api/GenAI.Samples.Tests/Thinking/ThinkingWithTxtTest.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 ThinkingWithTxtTest | ||
| { | ||
| private readonly GenAIFixture _fixture; | ||
| private readonly ThinkingWithTxt _sample; | ||
|
|
||
| public ThinkingWithTxtTest(GenAIFixture fixture) | ||
| { | ||
| _fixture = fixture; | ||
| _sample = new ThinkingWithTxt(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task TestThinkingWithTxt() | ||
| { | ||
| 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,58 @@ | ||
| /* | ||
| * 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_thinking_budget_with_txt] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class ThinkingBudgetWithTxt | ||
| { | ||
| public async Task<string> GenerateContent( | ||
| string projectId = "your-project-id", | ||
| string location = "global", | ||
| string model = "gemini-2.5-flash") | ||
| { | ||
| var client = new Client(project: projectId, location: location, vertexAI: true); | ||
|
|
||
| GenerateContentResponse response = await client.Models.GenerateContentAsync( | ||
| model: model, | ||
| contents: "solve x^2 + 4x + 4 = 0", | ||
| config: new GenerateContentConfig | ||
| { | ||
| ThinkingConfig = new ThinkingConfig { ThinkingBudget = 1024 } | ||
| }); | ||
|
|
||
| string responseText = response.Candidates[0].Content.Parts[0].Text; | ||
| Console.WriteLine(responseText); | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Example reponse: | ||
| // To solve the equation $x^2 + 4x + 4 = 0$, we can use several methods: | ||
| // **Method 1: Factoring(Recognizing a Perfect Square Trinomial) * * | ||
| // Notice that the left side of the equation is a perfect square trinomial. | ||
| // It follows the pattern $a ^ 2 + 2ab + b ^ 2 = (a + b) ^ 2$. | ||
| // ... | ||
| // All methods yield the same solution. | ||
| // The solution is $x = -2$. | ||
|
|
||
| Console.WriteLine($"Token count for thinking: {response.UsageMetadata.ThoughtsTokenCount}"); | ||
| // Example response: | ||
| // Token count for thinking: 804 | ||
| return responseText; | ||
| } | ||
| } | ||
| // [END googlegenaisdk_thinking_budget_with_txt] | ||
65 changes: 65 additions & 0 deletions
65
genai/api/GenAI.Samples/Thinking/ThinkingIncludeThoughtsWithTxt.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_thinking_includethoughts_with_txt] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class ThinkingIncludeThoughtsWithTxt | ||
| { | ||
| public async Task<string> GenerateContent( | ||
| string projectId = "your-project-id", | ||
| string location = "global", | ||
| string model = "gemini-2.5-pro") | ||
| { | ||
| var client = new Client(project: projectId, location: location, vertexAI: true); | ||
|
|
||
| GenerateContentResponse response = await client.Models.GenerateContentAsync( | ||
| model: model, | ||
| contents: "solve x^2 + 4x + 4 = 0", | ||
| config: new GenerateContentConfig | ||
| { | ||
| ThinkingConfig = new ThinkingConfig { IncludeThoughts = true } | ||
| }); | ||
|
|
||
| foreach (Part part in response.Candidates[0].Content.Parts) | ||
| { | ||
| if (part.Thought.GetValueOrDefault(false)) | ||
| { | ||
| Console.WriteLine(part.Text); | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| // Example response: | ||
| // Alright, let's break down this quadratic equation, `x^2 + 4x + 4 = 0`. First things first, I recognize | ||
| // this is a standard quadratic form, perfect for applying my knowledge. | ||
| // 1. **Analyze and Prep:**Okay, the user wants the solution.Got it. This is a quadratic in standard | ||
| // form, `ax ^ 2 + bx + c = 0`. That means I have several solution strategies at my disposal. | ||
| // 2. **Coefficients in Hand: ** `a` is 1, `b` is 4, and `c` is also 4.Easy enough. | ||
| // 3. **Method Selection - The Decision Point:**Now, I've got to choose the most efficient path. | ||
| // I could *always* use the quadratic formula, but that might be overkill. Factoring is always worth a | ||
| // look, especially when dealing with smaller coefficients. Completing the square is a possibility, but | ||
| // less elegant here. Graphing... well, that's more for visualization, not direct algebraic solution. | ||
| // 4. **Evaluate for *This * Case:**Let's see if factoring holds up. | ||
| // ... | ||
| // * End with a concise summary of the key takeaway, emphasizing that it's a repeated root. | ||
| // Perfect.The plan is set, the path is clear. | ||
| return response.Candidates[0].Content.Parts[0].Text; | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| // [END googlegenaisdk_thinking_includethoughts_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,50 @@ | ||
| /* | ||
| * 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_thinking_with_txt] | ||
|
|
||
| using Google.GenAI; | ||
| using Google.GenAI.Types; | ||
| using System; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class ThinkingWithTxt | ||
| { | ||
| public async Task<string> GenerateContent( | ||
| string projectId = "your-project-id", | ||
| string location = "global", | ||
| string model = "gemini-2.5-pro") | ||
| { | ||
| var client = new Client(project: projectId, location: location, vertexAI: true); | ||
|
|
||
| GenerateContentResponse response = await client.Models.GenerateContentAsync( | ||
| model: model, | ||
| contents: "solve x^2 + 4x + 4 = 0"); | ||
|
|
||
| string responseText = response.Candidates[0].Content.Parts[0].Text; | ||
| Console.WriteLine(responseText); | ||
jdomingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Example reponse: | ||
| // Of course! We can solve the equation **x² + 4x + 4 = 0** in a couple of ways. | ||
| // ### Method 1: Factoring | ||
| // This is the quickest method for this particular problem. | ||
| // 1. * *Recognize the pattern:**The expression `x² +4x + 4` is a perfect square trinomial | ||
| // ... | ||
| // Both methods give the same result. | ||
| // **The solution is x = -2.** | ||
| return responseText; | ||
| } | ||
| } | ||
| // [END googlegenaisdk_thinking_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.