.Net: feat(ollama): add Think property to OllamaPromptExecutionSettings#14122
Open
arashzjahangiri wants to merge 2 commits into
Open
.Net: feat(ollama): add Think property to OllamaPromptExecutionSettings#14122arashzjahangiri wants to merge 2 commits into
arashzjahangiri wants to merge 2 commits into
Conversation
Adds a Think property (bool?) to OllamaPromptExecutionSettings that controls thinking behavior for Ollama reasoning models (deepseek-r1, qwen3, phi4-reasoning). When a reasoning model has thinking enabled by default, its output lands in a separate thinking stream rather than the standard response field, causing GetTextContentsAsync to return empty content. Setting Think = false suppresses thinking so all output appears in the standard response field. Changes: - OllamaPromptExecutionSettings: adds Think property with backing field, JSON serialization (think), ThrowIfFrozen guard, and Clone support - OllamaTextGenerationService: maps Think to GenerateRequest.Think in CreateRequest() - Bumps OllamaSharp from 5.4.12 to 5.4.25 (adds GenerateRequest.Think) - Tests: serialization round-trip, Clone, Freeze, and request payload verification for both GetTextContentsAsync and GetStreamingTextContentsAsync Fixes microsoft#14078
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extends the .NET Ollama connector to support controlling “thinking” behavior for reasoning-capable models by introducing a Think setting on OllamaPromptExecutionSettings and wiring it through to the Ollama request payload (enabled by the OllamaSharp dependency update).
Changes:
- Added nullable
Think(bool?) toOllamaPromptExecutionSettings, including freeze-guarding and clone support. - Mapped
OllamaPromptExecutionSettings.Thinkinto the generated OllamaGenerateRequest. - Updated OllamaSharp dependency to a version that exposes
GenerateRequest.Think, and added unit tests covering serialization/clone/freeze and request payload behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| dotnet/src/Connectors/Connectors.Ollama/Settings/OllamaPromptExecutionSettings.cs | Adds Think execution setting with JSON serialization behavior and clone support. |
| dotnet/src/Connectors/Connectors.Ollama/Services/OllamaTextGenerationService.cs | Includes Think in the generated request sent to Ollama. |
| dotnet/src/Connectors/Connectors.Ollama.UnitTests/Settings/OllamaPromptExecutionSettingsTests.cs | Adds tests for Think serialization, cloning, and freeze behavior. |
| dotnet/src/Connectors/Connectors.Ollama.UnitTests/Services/OllamaTextGenerationTests.cs | Verifies think is included/omitted in request payloads for streaming and non-streaming paths. |
| dotnet/Directory.Packages.props | Bumps OllamaSharp to a version that includes GenerateRequest.Think. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Author
|
@microsoft-github-policy-service agree |
- Simplify ThinkValue mapping: use settings.Think.Value (non-null bool) instead of passing nullable bool to constructor - Use ToLowerInvariant() instead of ToLower() in test JSON builder to avoid culture-sensitive casing
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Think(bool?) property toOllamaPromptExecutionSettingsto control thinking for Ollama reasoning models (deepseek-r1, qwen3, phi4-reasoning)ThinktoGenerateRequest.ThinkinOllamaTextGenerationService.CreateRequest()GenerateRequest.ThinkMotivation
Fixes #14078. When a reasoning model has thinking enabled by default (e.g. qwen3, phi4-reasoning), the model output lands in a separate thinking stream rather than the standard response field. This causes
GetTextContentsAsyncto return empty content. The only way to get a usable response is to passthink=falseto suppress thinking, but there was no way to set that fromOllamaPromptExecutionSettings.Changes
OllamaPromptExecutionSettings.csThinkproperty withbool?type, JSON namethink,WhenWritingNullignore condition,ThrowIfFrozen()guard, andClone()supportOllamaTextGenerationService.csCreateRequest()mapssettings.ThinktoGenerateRequest.ThinkusingOllamaSharp.Models.Chat.ThinkValueDirectory.Packages.propsGenerateRequest.ThinkandThinkValue)Tests
OllamaPromptExecutionSettingsTests: serialization round-trip, Clone, Freeze guard forThinkOllamaTextGenerationTests: verifiesThinkis present/absent in the serialized request payload for bothGetTextContentsAsyncandGetStreamingTextContentsAsyncTest plan
ThinkPropertyRoundTripsViaSerialization-true/falseround-trips via JSONThinkPropertyIsPreservedByClone- Clone copies the valueThinkPropertyThrowsWhenFrozen- setter throws after FreezeGetTextContentsShouldSendThinkSettingAsync- request payload contains correctthinkfieldGetTextContentsShouldNotSendThinkWhenNotSetAsync- request payload omitsthinkwhen nullGetStreamingTextContentsShouldSendThinkSettingAsync- streaming path also sendsthink