From b536e86cd0bd64607449b5bc08a9294e218f7781 Mon Sep 17 00:00:00 2001 From: xiaoen <2768753269@qq.com> Date: Tue, 24 Feb 2026 22:24:04 +0800 Subject: [PATCH] fix: handle thinking content in Ollama chat responses Ollama API returns reasoning model output in Message.Thinking field, but OllamaChat only reads Message.Content. Add thinking content handling for both streaming and non-streaming, consistent with RemoteAPIChat behavior. --- internal/models/chat/ollama.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/internal/models/chat/ollama.go b/internal/models/chat/ollama.go index e6791b1b..17abde0d 100644 --- a/internal/models/chat/ollama.go +++ b/internal/models/chat/ollama.go @@ -105,6 +105,10 @@ func (c *OllamaChat) Chat(ctx context.Context, messages []Message, opts *ChatOpt // 使用 Ollama 客户端发送请求 err := c.ollamaService.Chat(ctx, chatReq, func(resp ollamaapi.ChatResponse) error { responseContent = resp.Message.Content + // 当 Content 为空但 Thinking 有内容时(如推理模型未正确配置 thinking 参数),使用 Thinking 作为兜底 + if responseContent == "" && resp.Message.Thinking != "" { + responseContent = resp.Message.Thinking + } toolCalls = c.toolCallTo(resp.Message.ToolCalls) // 获取token计数 @@ -159,8 +163,27 @@ func (c *OllamaChat) ChatStream( go func() { defer close(streamChan) + hasThinking := false err := c.ollamaService.Chat(ctx, chatReq, func(resp ollamaapi.ChatResponse) error { + // 发送思考内容(支持 Qwen3、DeepSeek 等推理模型) + if resp.Message.Thinking != "" { + hasThinking = true + streamChan <- types.StreamResponse{ + ResponseType: types.ResponseTypeThinking, + Content: resp.Message.Thinking, + Done: false, + } + } + if resp.Message.Content != "" { + // 思考阶段结束后,发送思考完成事件 + if hasThinking { + streamChan <- types.StreamResponse{ + ResponseType: types.ResponseTypeThinking, + Done: true, + } + hasThinking = false + } streamChan <- types.StreamResponse{ ResponseType: types.ResponseTypeAnswer, Content: resp.Message.Content,