Skip to content

Commit cbd4fcf

Browse files
author
Juan Dominguez
committed
feat(genai): add thinking samples
1 parent 7c95aa3 commit cbd4fcf

File tree

6 files changed

+291
-0
lines changed

6 files changed

+291
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using System.Threading.Tasks;
18+
using Xunit;
19+
20+
[Collection(nameof(GenAIFixture))]
21+
public class ThinkingBudgetWithTxtTest
22+
{
23+
private readonly GenAIFixture _fixture;
24+
private readonly ThinkingBudgetWithTxt _sample;
25+
26+
public ThinkingBudgetWithTxtTest(GenAIFixture fixture)
27+
{
28+
_fixture = fixture;
29+
_sample = new ThinkingBudgetWithTxt();
30+
}
31+
32+
[Fact]
33+
public async Task TestThinkingBudgetWithTxt()
34+
{
35+
var response = await _sample.GenerateContent(_fixture.ProjectId);
36+
Assert.NotEmpty(response);
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using System.Threading.Tasks;
18+
using Xunit;
19+
20+
[Collection(nameof(GenAIFixture))]
21+
public class ThinkingIncludeThoughtsWithTxtTest
22+
{
23+
private readonly GenAIFixture _fixture;
24+
private readonly ThinkingIncludeThoughtsWithTxt _sample;
25+
26+
public ThinkingIncludeThoughtsWithTxtTest(GenAIFixture fixture)
27+
{
28+
_fixture = fixture;
29+
_sample = new ThinkingIncludeThoughtsWithTxt();
30+
}
31+
32+
[Fact]
33+
public async Task TestThinkingIncludeThoughtsWithTxt()
34+
{
35+
var response = await _sample.GenerateContent(_fixture.ProjectId);
36+
Assert.NotEmpty(response);
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using System.Threading.Tasks;
18+
using Xunit;
19+
20+
[Collection(nameof(GenAIFixture))]
21+
public class ThinkingWithTxtTest
22+
{
23+
private readonly GenAIFixture _fixture;
24+
private readonly ThinkingWithTxt _sample;
25+
26+
public ThinkingWithTxtTest(GenAIFixture fixture)
27+
{
28+
_fixture = fixture;
29+
_sample = new ThinkingWithTxt();
30+
}
31+
32+
[Fact]
33+
public async Task TestThinkingWithTxt()
34+
{
35+
var response = await _sample.GenerateContent(_fixture.ProjectId);
36+
Assert.NotEmpty(response);
37+
}
38+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START googlegenaisdk_thinking_budget_with_txt]
18+
19+
using Google.GenAI;
20+
using Google.GenAI.Types;
21+
using System;
22+
using System.Threading.Tasks;
23+
24+
public class ThinkingBudgetWithTxt
25+
{
26+
public async Task<string> GenerateContent(
27+
string projectId = "your-project-id",
28+
string location = "global",
29+
string model = "gemini-2.5-flash")
30+
{
31+
var client = new Client(project: projectId, location: location, vertexAI: true);
32+
33+
GenerateContentResponse response = await client.Models.GenerateContentAsync(
34+
model: model,
35+
contents: "solve x^2 + 4x + 4 = 0",
36+
config: new GenerateContentConfig
37+
{
38+
ThinkingConfig = new ThinkingConfig { ThinkingBudget = 1024 }
39+
});
40+
41+
string responseText = response.Candidates[0].Content.Parts[0].Text;
42+
Console.WriteLine(responseText);
43+
// Example reponse:
44+
// To solve the equation $x^2 + 4x + 4 = 0$, we can use several methods:
45+
// **Method 1: Factoring(Recognizing a Perfect Square Trinomial) * *
46+
// Notice that the left side of the equation is a perfect square trinomial.
47+
// It follows the pattern $a ^ 2 + 2ab + b ^ 2 = (a + b) ^ 2$.
48+
// ...
49+
// All methods yield the same solution.
50+
// The solution is $x = -2$.
51+
52+
Console.WriteLine($"Token count for thinking: {response.UsageMetadata.ThoughtsTokenCount}");
53+
// Example response:
54+
// Token count for thinking: 804
55+
56+
Console.WriteLine($"Total token count: {response.UsageMetadata.TotalTokenCount}");
57+
// Example response:
58+
// Total token count: 1450
59+
return responseText;
60+
}
61+
}
62+
// [END googlegenaisdk_thinking_budget_with_txt]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START googlegenaisdk_thinking_includethoughts_with_txt]
18+
19+
using Google.GenAI;
20+
using Google.GenAI.Types;
21+
using System;
22+
using System.Threading.Tasks;
23+
24+
public class ThinkingIncludeThoughtsWithTxt
25+
{
26+
public async Task<string> GenerateContent(
27+
string projectId = "your-project-id",
28+
string location = "global",
29+
string model = "gemini-2.5-pro")
30+
{
31+
var client = new Client(project: projectId, location: location, vertexAI: true);
32+
33+
GenerateContentResponse response = await client.Models.GenerateContentAsync(
34+
model: model,
35+
contents: "solve x^2 + 4x + 4 = 0",
36+
config: new GenerateContentConfig
37+
{
38+
ThinkingConfig = new ThinkingConfig { IncludeThoughts = true }
39+
});
40+
41+
foreach (Part part in response.Candidates[0].Content.Parts)
42+
{
43+
if (part.Thought.GetValueOrDefault(false))
44+
{
45+
Console.WriteLine(part.Text);
46+
}
47+
}
48+
// Example response:
49+
// Alright, let's break down this quadratic equation, `x^2 + 4x + 4 = 0`. First things first, I recognize
50+
// this is a standard quadratic form, perfect for applying my knowledge.
51+
// 1. **Analyze and Prep:**Okay, the user wants the solution.Got it. This is a quadratic in standard
52+
// form, `ax ^ 2 + bx + c = 0`. That means I have several solution strategies at my disposal.
53+
// 2. **Coefficients in Hand: ** `a` is 1, `b` is 4, and `c` is also 4.Easy enough.
54+
// 3. **Method Selection - The Decision Point:**Now, I've got to choose the most efficient path.
55+
// I could *always* use the quadratic formula, but that might be overkill. Factoring is always worth a
56+
// look, especially when dealing with smaller coefficients. Completing the square is a possibility, but
57+
// less elegant here. Graphing... well, that's more for visualization, not direct algebraic solution.
58+
// 4. **Evaluate for *This * Case:**Let's see if factoring holds up.
59+
// ...
60+
// * End with a concise summary of the key takeaway, emphasizing that it's a repeated root.
61+
// Perfect.The plan is set, the path is clear.
62+
return response.Candidates[0].Content.Parts[0].Text;
63+
}
64+
}
65+
// [END googlegenaisdk_thinking_includethoughts_with_txt]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START googlegenaisdk_thinking_with_txt]
18+
19+
using Google.GenAI;
20+
using Google.GenAI.Types;
21+
using System;
22+
using System.Threading.Tasks;
23+
24+
public class ThinkingWithTxt
25+
{
26+
public async Task<string> GenerateContent(
27+
string projectId = "your-project-id",
28+
string location = "global",
29+
string model = "gemini-2.5-pro")
30+
{
31+
var client = new Client(project: projectId, location: location, vertexAI: true);
32+
33+
GenerateContentResponse response = await client.Models.GenerateContentAsync(
34+
model: model,
35+
contents: "solve x^2 + 4x + 4 = 0");
36+
37+
string responseText = response.Candidates[0].Content.Parts[0].Text;
38+
Console.WriteLine(responseText);
39+
// Example reponse:
40+
// Of course! We can solve the equation **x² + 4x + 4 = 0** in a couple of ways.
41+
// ### Method 1: Factoring
42+
// This is the quickest method for this particular problem.
43+
// 1. * *Recognize the pattern:**The expression `x² +4x + 4` is a perfect square trinomial
44+
// ...
45+
// Both methods give the same result.
46+
// **The solution is x = -2.**
47+
return responseText;
48+
}
49+
}
50+
// [END googlegenaisdk_thinking_with_txt]

0 commit comments

Comments
 (0)