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.
This commit is contained in:
@@ -105,6 +105,10 @@ func (c *OllamaChat) Chat(ctx context.Context, messages []Message, opts *ChatOpt
|
|||||||
// 使用 Ollama 客户端发送请求
|
// 使用 Ollama 客户端发送请求
|
||||||
err := c.ollamaService.Chat(ctx, chatReq, func(resp ollamaapi.ChatResponse) error {
|
err := c.ollamaService.Chat(ctx, chatReq, func(resp ollamaapi.ChatResponse) error {
|
||||||
responseContent = resp.Message.Content
|
responseContent = resp.Message.Content
|
||||||
|
// 当 Content 为空但 Thinking 有内容时(如推理模型未正确配置 thinking 参数),使用 Thinking 作为兜底
|
||||||
|
if responseContent == "" && resp.Message.Thinking != "" {
|
||||||
|
responseContent = resp.Message.Thinking
|
||||||
|
}
|
||||||
toolCalls = c.toolCallTo(resp.Message.ToolCalls)
|
toolCalls = c.toolCallTo(resp.Message.ToolCalls)
|
||||||
|
|
||||||
// 获取token计数
|
// 获取token计数
|
||||||
@@ -159,8 +163,27 @@ func (c *OllamaChat) ChatStream(
|
|||||||
go func() {
|
go func() {
|
||||||
defer close(streamChan)
|
defer close(streamChan)
|
||||||
|
|
||||||
|
hasThinking := false
|
||||||
err := c.ollamaService.Chat(ctx, chatReq, func(resp ollamaapi.ChatResponse) error {
|
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 resp.Message.Content != "" {
|
||||||
|
// 思考阶段结束后,发送思考完成事件
|
||||||
|
if hasThinking {
|
||||||
|
streamChan <- types.StreamResponse{
|
||||||
|
ResponseType: types.ResponseTypeThinking,
|
||||||
|
Done: true,
|
||||||
|
}
|
||||||
|
hasThinking = false
|
||||||
|
}
|
||||||
streamChan <- types.StreamResponse{
|
streamChan <- types.StreamResponse{
|
||||||
ResponseType: types.ResponseTypeAnswer,
|
ResponseType: types.ResponseTypeAnswer,
|
||||||
Content: resp.Message.Content,
|
Content: resp.Message.Content,
|
||||||
|
|||||||
Reference in New Issue
Block a user