Skip to content

Commit b536e86

Browse files
is-Xiaoenlyingbug
authored andcommitted
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.
1 parent 8378454 commit b536e86

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

internal/models/chat/ollama.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ func (c *OllamaChat) Chat(ctx context.Context, messages []Message, opts *ChatOpt
105105
// 使用 Ollama 客户端发送请求
106106
err := c.ollamaService.Chat(ctx, chatReq, func(resp ollamaapi.ChatResponse) error {
107107
responseContent = resp.Message.Content
108+
// 当 Content 为空但 Thinking 有内容时(如推理模型未正确配置 thinking 参数),使用 Thinking 作为兜底
109+
if responseContent == "" && resp.Message.Thinking != "" {
110+
responseContent = resp.Message.Thinking
111+
}
108112
toolCalls = c.toolCallTo(resp.Message.ToolCalls)
109113

110114
// 获取token计数
@@ -159,8 +163,27 @@ func (c *OllamaChat) ChatStream(
159163
go func() {
160164
defer close(streamChan)
161165

166+
hasThinking := false
162167
err := c.ollamaService.Chat(ctx, chatReq, func(resp ollamaapi.ChatResponse) error {
168+
// 发送思考内容(支持 Qwen3、DeepSeek 等推理模型)
169+
if resp.Message.Thinking != "" {
170+
hasThinking = true
171+
streamChan <- types.StreamResponse{
172+
ResponseType: types.ResponseTypeThinking,
173+
Content: resp.Message.Thinking,
174+
Done: false,
175+
}
176+
}
177+
163178
if resp.Message.Content != "" {
179+
// 思考阶段结束后,发送思考完成事件
180+
if hasThinking {
181+
streamChan <- types.StreamResponse{
182+
ResponseType: types.ResponseTypeThinking,
183+
Done: true,
184+
}
185+
hasThinking = false
186+
}
164187
streamChan <- types.StreamResponse{
165188
ResponseType: types.ResponseTypeAnswer,
166189
Content: resp.Message.Content,

0 commit comments

Comments
 (0)