Skip to content

.Net: Introduce Gemini Thinking Budget Configuration. #11647

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

Merged
merged 20 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
5a9e3fe
Introduce Gemini Thinking Budget Configuration.
Apr 18, 2025
884214d
Merge branch 'main' into shethaadit/FixBug11645
shethaadit Apr 18, 2025
1ad68d6
Merge branch 'main' into shethaadit/FixBug11645
shethaadit Apr 21, 2025
7d36f9b
Merge branch 'main' into shethaadit/FixBug11645
shethaadit Apr 21, 2025
bb19ba4
Merge branch 'main' into shethaadit/FixBug11645
shethaadit Apr 21, 2025
66e0ca6
Update dotnet/src/Connectors/Connectors.Google/GeminiThinkingConfig .cs
shethaadit Apr 22, 2025
38b4221
Update dotnet/src/Connectors/Connectors.Google/GeminiPromptExecutionS…
shethaadit Apr 22, 2025
760bd94
Update dotnet/src/Connectors/Connectors.Google/GeminiPromptExecutionS…
shethaadit Apr 22, 2025
dfca49f
Comments Fixes.
Apr 22, 2025
493445e
Merge branch 'main' into shethaadit/FixBug11645
shethaadit Apr 22, 2025
69ade20
Merge branch 'main' into shethaadit/FixBug11645
shethaadit Apr 23, 2025
617735f
Fixed build error.
Apr 23, 2025
aff242b
Merge branch 'main' into shethaadit/FixBug11645
shethaadit Apr 23, 2025
9431d96
Conclude the PR, added tests and checks
RogerBarreto Apr 24, 2025
b0ed002
Fix warnings
RogerBarreto Apr 24, 2025
6f83d52
Add missing concept example + fix request bug
RogerBarreto Apr 24, 2025
3940c92
Added missing UT + removed vertex examples/tests
RogerBarreto Apr 24, 2025
3e3c4fa
Merge branch 'main' into shethaadit/FixBug11645
RogerBarreto Apr 24, 2025
8b514c4
Merge branch 'main' into shethaadit/FixBug11645
shethaadit Apr 24, 2025
8f6362b
Merge branch 'main' into shethaadit/FixBug11645
RogerBarreto Apr 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Microsoft. All rights reserved.

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.Google;

namespace ChatCompletion;

/// <summary>
/// These examples demonstrate different ways of using chat completion with Google AI API.
/// <para>
/// Currently thinking budget is only supported in Google AI Gemini 2.5+ models
/// See: https://developers.googleblog.com/en/start-building-with-gemini-25-flash/#:~:text=thinking%20budgets
/// </para>
/// </summary>
public sealed class Google_GeminiChatCompletionWithThinkingBudget(ITestOutputHelper output) : BaseTest(output)
{
[Fact]
public async Task GoogleAIChatCompletionUsingThinkingBudget()
{
Console.WriteLine("============= Google AI - Gemini 2.5 Chat Completion using Thinking Budget =============");

Assert.NotNull(TestConfiguration.GoogleAI.ApiKey);
string geminiModelId = "gemini-2.5-pro-exp-03-25";

Kernel kernel = Kernel.CreateBuilder()
.AddGoogleAIGeminiChatCompletion(
modelId: geminiModelId,
apiKey: TestConfiguration.GoogleAI.ApiKey)
.Build();

var chatHistory = new ChatHistory("You are an expert in the tool shop.");
var chat = kernel.GetRequiredService<IChatCompletionService>();
var executionSettings = new GeminiPromptExecutionSettings
{
// This parameter gives the model how much tokens it can use during the thinking process.
ThinkingConfig = new() { ThinkingBudget = 2000 }
};

// First user message
chatHistory.AddUserMessage("Hi, I'm looking for new power tools, any suggestion?");
await MessageOutputAsync(chatHistory);

// First assistant message
var reply = await chat.GetChatMessageContentAsync(chatHistory, executionSettings);
chatHistory.Add(reply);
await MessageOutputAsync(chatHistory);
}

/// <summary>
/// Outputs the last message of the chat history
/// </summary>
private Task MessageOutputAsync(ChatHistory chatHistory)
{
var message = chatHistory.Last();

Console.WriteLine($"{message.Role}: {message.Content}");
Console.WriteLine("------------------------");

return Task.CompletedTask;
}
}
1 change: 1 addition & 0 deletions dotnet/samples/Concepts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dotnet test -l "console;verbosity=detailed" --filter "FullyQualifiedName=ChatCom
- [Connectors_WithMultipleLLMs](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Connectors_WithMultipleLLMs.cs)
- [Google_GeminiChatCompletion](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Google_GeminiChatCompletion.cs)
- [Google_GeminiChatCompletionStreaming](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Google_GeminiChatCompletionStreaming.cs)
- [Google_GeminiChatCompletionWithThinkingBudget](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Google_GeminiChatCompletionWithThinkingBudget.cs)
- [Google_GeminiGetModelResult](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Google_GeminiGetModelResult.cs)
- [Google_GeminiStructuredOutputs](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Google_GeminiStructuredOutputs.cs)
- [Google_GeminiVision](https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/ChatCompletion/Google_GeminiVision.cs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,24 @@ public void ResponseSchemaAddsTypeToEnumProperties()
Assert.Equal(2, roleProperty.GetProperty("enum").GetArrayLength());
}

[Fact]
public void FromPromptAndExecutionSettingsWithThinkingConfigReturnsInGenerationConfig()
{
// Arrange
var prompt = "prompt-example";
var executionSettings = new GeminiPromptExecutionSettings
{
ModelId = "gemini-2.5-flash-preview-04-17",
ThinkingConfig = new GeminiThinkingConfig { ThinkingBudget = 1024 }
};

// Act
var request = GeminiRequest.FromPromptAndExecutionSettings(prompt, executionSettings);

// Assert
Assert.Equal(executionSettings.ThinkingConfig.ThinkingBudget, request.Configuration?.ThinkingConfig?.ThinkingBudget);
}

private sealed class DummyContent(object? innerContent, string? modelId = null, IReadOnlyDictionary<string, object?>? metadata = null) :
KernelContent(innerContent, modelId, metadata);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ public void ItCreatesGeminiExecutionSettingsFromJsonSnakeCase()
"category": "{{category.Label}}",
"threshold": "{{threshold.Label}}"
}
]
],
"thinking_config": {
"thinking_budget": 1000
}
}
""";
var actualSettings = JsonSerializer.Deserialize<PromptExecutionSettings>(json);
Expand All @@ -129,6 +132,8 @@ public void ItCreatesGeminiExecutionSettingsFromJsonSnakeCase()
Assert.Single(executionSettings.SafetySettings!, settings =>
settings.Category.Equals(category) &&
settings.Threshold.Equals(threshold));

Assert.Equal(1000, executionSettings.ThinkingConfig?.ThinkingBudget);
}

[Fact]
Expand All @@ -152,7 +157,10 @@ public void PromptExecutionSettingsCloneWorksAsExpected()
"category": "{{category.Label}}",
"threshold": "{{threshold.Label}}"
}
]
],
"thinking_config": {
"thinking_budget": 1000
}
}
""";
var executionSettings = JsonSerializer.Deserialize<GeminiPromptExecutionSettings>(json);
Expand All @@ -168,6 +176,7 @@ public void PromptExecutionSettingsCloneWorksAsExpected()
Assert.Equivalent(executionSettings.StopSequences, clone.StopSequences);
Assert.Equivalent(executionSettings.SafetySettings, clone.SafetySettings);
Assert.Equal(executionSettings.AudioTimestamp, clone.AudioTimestamp);
Assert.Equivalent(executionSettings.ThinkingConfig, clone.ThinkingConfig);
}

[Fact]
Expand All @@ -191,7 +200,10 @@ public void PromptExecutionSettingsFreezeWorksAsExpected()
"category": "{{category.Label}}",
"threshold": "{{threshold.Label}}"
}
]
],
"thinking_config": {
"thinking_budget": 1000
}
}
""";
var executionSettings = JsonSerializer.Deserialize<GeminiPromptExecutionSettings>(json);
Expand All @@ -206,5 +218,7 @@ public void PromptExecutionSettingsFreezeWorksAsExpected()
Assert.Throws<InvalidOperationException>(() => executionSettings.Temperature = 0.5);
Assert.Throws<InvalidOperationException>(() => executionSettings.AudioTimestamp = false);
Assert.Throws<NotSupportedException>(() => executionSettings.StopSequences!.Add("baz"));
Assert.Throws<NotSupportedException>(() => executionSettings.SafetySettings!.Add(new GeminiSafetySetting(GeminiSafetyCategory.Toxicity, GeminiSafetyThreshold.Unspecified)));
Assert.Throws<InvalidOperationException>(() => executionSettings.ThinkingConfig = new GeminiThinkingConfig { ThinkingBudget = 1 });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,44 @@ public async Task RequestCachedContentWorksCorrectlyAsync(string? cachedContent)
}
}

[Theory]
[InlineData(null, false)]
[InlineData(0, true)]
[InlineData(500, true)]
[InlineData(2048, true)]
public async Task RequestBodyIncludesThinkingConfigWhenSetAsync(int? thinkingBudget, bool shouldContain)
{
// Arrange
string model = "gemini-2.5-pro";
var sut = new GoogleAIGeminiChatCompletionService(model, "key", httpClient: this._httpClient);

var executionSettings = new GeminiPromptExecutionSettings
{
ThinkingConfig = thinkingBudget.HasValue
? new GeminiThinkingConfig { ThinkingBudget = thinkingBudget.Value }
: null
};

// Act
var result = await sut.GetChatMessageContentAsync("my prompt", executionSettings);

// Assert
Assert.NotNull(result);
Assert.NotNull(this._messageHandlerStub.RequestContent);

var requestBody = UTF8Encoding.UTF8.GetString(this._messageHandlerStub.RequestContent);

if (shouldContain)
{
Assert.Contains("thinkingConfig", requestBody);
Assert.Contains($"\"thinkingBudget\":{thinkingBudget}", requestBody);
}
else
{
Assert.DoesNotContain("thinkingConfig", requestBody);
}
}

public void Dispose()
{
this._httpClient.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,44 @@ public async Task RequestCachedContentWorksCorrectlyAsync(string? cachedContent)
}
}

[Theory]
[InlineData(null, false)]
[InlineData(0, true)]
[InlineData(500, true)]
[InlineData(2048, true)]
public async Task RequestBodyIncludesThinkingConfigWhenSetAsync(int? thinkingBudget, bool shouldContain)
{
// Arrange
string model = "gemini-2.5-pro";
var sut = new VertexAIGeminiChatCompletionService(model, () => new ValueTask<string>("key"), "location", "project", httpClient: this._httpClient);

var executionSettings = new GeminiPromptExecutionSettings
{
ThinkingConfig = thinkingBudget.HasValue
? new GeminiThinkingConfig { ThinkingBudget = thinkingBudget.Value }
: null
};

// Act
var result = await sut.GetChatMessageContentAsync("my prompt", executionSettings);

// Assert
Assert.NotNull(result);
Assert.NotNull(this._messageHandlerStub.RequestContent);

var requestBody = UTF8Encoding.UTF8.GetString(this._messageHandlerStub.RequestContent);

if (shouldContain)
{
Assert.Contains("thinkingConfig", requestBody);
Assert.Contains($"\"thinkingBudget\":{thinkingBudget}", requestBody);
}
else
{
Assert.DoesNotContain("thinkingConfig", requestBody);
}
}

public void Dispose()
{
this._httpClient.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ private static void AddConfiguration(GeminiPromptExecutionSettings executionSett
CandidateCount = executionSettings.CandidateCount,
AudioTimestamp = executionSettings.AudioTimestamp,
ResponseMimeType = executionSettings.ResponseMimeType,
ResponseSchema = GetResponseSchemaConfig(executionSettings.ResponseSchema)
ResponseSchema = GetResponseSchemaConfig(executionSettings.ResponseSchema),
};
}

Expand Down Expand Up @@ -430,6 +430,11 @@ private static void AddSafetySettings(GeminiPromptExecutionSettings executionSet
private static void AddAdditionalBodyFields(GeminiPromptExecutionSettings executionSettings, GeminiRequest request)
{
request.CachedContent = executionSettings.CachedContent;
if (executionSettings.ThinkingConfig is not null)
{
request.Configuration ??= new ConfigurationElement();
request.Configuration.ThinkingConfig = new GeminiRequestThinkingConfig { ThinkingBudget = executionSettings.ThinkingConfig.ThinkingBudget };
}
}

internal sealed class ConfigurationElement
Expand Down Expand Up @@ -469,5 +474,16 @@ internal sealed class ConfigurationElement
[JsonPropertyName("responseSchema")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public JsonElement? ResponseSchema { get; set; }

[JsonPropertyName("thinkingConfig")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public GeminiRequestThinkingConfig? ThinkingConfig { get; set; }
}

internal sealed class GeminiRequestThinkingConfig
{
[JsonPropertyName("thinkingBudget")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? ThinkingBudget { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public sealed class GeminiPromptExecutionSettings : PromptExecutionSettings
private string? _cachedContent;
private IList<GeminiSafetySetting>? _safetySettings;
private GeminiToolCallBehavior? _toolCallBehavior;
private GeminiThinkingConfig? _thinkingConfig;

/// <summary>
/// Default max tokens for a text generation.
Expand Down Expand Up @@ -262,6 +263,24 @@ public string? CachedContent
}
}

/// <summary>
/// Configuration for the thinking budget in Gemini 2.5.
/// </summary>
/// <remarks>
/// This property is specific to Gemini 2.5 and similar experimental models.
/// </remarks>
[JsonPropertyName("thinking_config")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public GeminiThinkingConfig? ThinkingConfig
{
get => this._thinkingConfig;
set
{
this.ThrowIfFrozen();
this._thinkingConfig = value;
}
}

/// <inheritdoc />
public override void Freeze()
{
Expand Down Expand Up @@ -301,6 +320,7 @@ public override PromptExecutionSettings Clone()
AudioTimestamp = this.AudioTimestamp,
ResponseMimeType = this.ResponseMimeType,
ResponseSchema = this.ResponseSchema,
ThinkingConfig = this.ThinkingConfig?.Clone()
};
}

Expand Down
33 changes: 33 additions & 0 deletions dotnet/src/Connectors/Connectors.Google/GeminiThinkingConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Text.Json.Serialization;

namespace Microsoft.SemanticKernel.Connectors.Google;

/// <summary>
/// GeminiThinkingConfig class
/// </summary>
public class GeminiThinkingConfig
{
/// <summary>The thinking budget parameter gives the model guidance on how many thinking tokens it can use for its thinking process.</summary>
/// <remarks>
/// <para>A greater number of tokens is typically associated with more detailed thinking, which is needed for solving more complex tasks.
/// thinkingBudget must be an integer in the range 0 to 24576. Setting the thinking budget to 0 disables thinking.
/// Budgets from 1 to 1024 tokens will be set to 1024.
/// </para>
/// This parameter is specific to Gemini 2.5 and similar experimental models.
/// If no ThinkingBudget is explicitly set, the API default (likely 0) will be used
/// </remarks>
[JsonPropertyName("thinking_budget")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public int? ThinkingBudget { get; set; }

/// <summary>
/// Clones this instance.
/// </summary>
/// <returns></returns>
public GeminiThinkingConfig Clone()
{
return (GeminiThinkingConfig)this.MemberwiseClone();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -578,4 +578,26 @@ public async Task ChatStreamingReturnsResponseSafetyRatingsAsync(ServiceType ser
this.Output.WriteLine($"ResponseSafetyRatings: {JsonSerializer.Serialize(geminiMetadata.ResponseSafetyRatings)}");
Assert.NotNull(geminiMetadata.ResponseSafetyRatings);
}

[RetryFact(Skip = "This test is for manual verification.")]
public async Task GoogleAIChatReturnsResponseWorksWithThinkingBudgetAsync()
{
// Arrange
var modelId = "gemini-2.5-pro-exp-03-25";
var chatHistory = new ChatHistory();
chatHistory.AddUserMessage("Hello, I'm Brandon, how are you?");
chatHistory.AddAssistantMessage("I'm doing well, thanks for asking.");
chatHistory.AddUserMessage("Call me by my name and expand this abbreviation: LLM");

var sut = this.GetChatService(ServiceType.GoogleAI, isBeta: true, overrideModelId: modelId);
var settings = new GeminiPromptExecutionSettings { ThinkingConfig = new() { ThinkingBudget = 2000 } };

// Act
var streamResponses = await sut.GetStreamingChatMessageContentsAsync(chatHistory, settings).ToListAsync();
var responses = await sut.GetChatMessageContentsAsync(chatHistory, settings);

// Assert
Assert.NotNull(streamResponses[0].Content);
Assert.NotNull(responses[0].Content);
}
}
Loading
Loading