Skip to content

Commit ec4b03e

Browse files
authored
feat: add internal thinking field to capture model's reasoning output (#498)
* feat: add multimodal message support with image data URI handling * feat: enhance ensureDataURIPrefix to detect MIME types and trim whitespace * feat: add internal thinking field to capture model's reasoning output
1 parent 07c231f commit ec4b03e

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

pkg/ollama/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type Message struct {
6666
Images []string `json:"images,omitempty"` // For multimodal support
6767
ToolCalls []ToolCall `json:"tool_calls,omitempty"` // For function calling
6868
ToolCallID string `json:"tool_call_id,omitempty"` // For tool results
69+
Thinking string `json:"thinking,omitempty"` // Internal field for model's thinking output
6970
}
7071

7172
// ToolCall represents a function call made by the model

pkg/ollama/http_handler.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,11 +1056,13 @@ func (s *streamingChatResponseWriter) Write(data []byte) (int, error) {
10561056
continue
10571057
}
10581058

1059-
// Extract content and tool calls from structured response
1059+
// Extract content, tool calls, and thinking from structured response
10601060
var content string
1061+
var thinking string
10611062
var toolCalls []ToolCall
10621063
if len(chunk.Choices) > 0 {
10631064
content = chunk.Choices[0].Delta.Content
1065+
thinking = chunk.Choices[0].Delta.ReasoningContent
10641066
if len(chunk.Choices[0].Delta.ToolCalls) > 0 {
10651067
// Convert tool calls to Ollama format
10661068
toolCalls = convertToolCallsToOllamaFormat(chunk.Choices[0].Delta.ToolCalls)
@@ -1075,6 +1077,9 @@ func (s *streamingChatResponseWriter) Write(data []byte) (int, error) {
10751077
if len(toolCalls) > 0 {
10761078
message.ToolCalls = toolCalls
10771079
}
1080+
if thinking != "" {
1081+
message.Thinking = thinking
1082+
}
10781083

10791084
ollamaChunk := ChatResponse{
10801085
Model: s.modelName,
@@ -1242,6 +1247,10 @@ func (h *HTTPHandler) convertChatResponse(w http.ResponseWriter, respRecorder *r
12421247
if len(openAIResp.Choices[0].Message.ToolCalls) > 0 {
12431248
message.ToolCalls = convertToolCallsToOllamaFormat(openAIResp.Choices[0].Message.ToolCalls)
12441249
}
1250+
// Include thinking content if present
1251+
if openAIResp.Choices[0].Message.ReasoningContent != "" {
1252+
message.Thinking = openAIResp.Choices[0].Message.ReasoningContent
1253+
}
12451254
}
12461255

12471256
// Build Ollama response

0 commit comments

Comments
 (0)