From 25af6e6f77e7720bc5aeae71a7965dc90b9358bc Mon Sep 17 00:00:00 2001 From: somnifex <98788152+somnifex@users.noreply.github.com> Date: Fri, 17 Oct 2025 18:35:08 +0800 Subject: [PATCH] fix: handle JSON parsing for thinking content in ollama stream and chat handlers --- relay/channel/ollama/stream.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/relay/channel/ollama/stream.go b/relay/channel/ollama/stream.go index c73f2d50f..2a264b27e 100644 --- a/relay/channel/ollama/stream.go +++ b/relay/channel/ollama/stream.go @@ -121,7 +121,14 @@ func ollamaStreamHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http if chunk.Message != nil && len(chunk.Message.Thinking) > 0 { raw := strings.TrimSpace(string(chunk.Message.Thinking)) if raw != "" && raw != "null" { - delta.Choices[0].Delta.SetReasoningContent(raw) + // Unmarshal the JSON string to get the actual content without quotes + var thinkingContent string + if err := json.Unmarshal(chunk.Message.Thinking, &thinkingContent); err == nil { + delta.Choices[0].Delta.SetReasoningContent(thinkingContent) + } else { + // Fallback to raw string if it's not a JSON string + delta.Choices[0].Delta.SetReasoningContent(raw) + } } } // tool calls @@ -209,7 +216,14 @@ func ollamaChatHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.R if ck.Message != nil && len(ck.Message.Thinking) > 0 { raw := strings.TrimSpace(string(ck.Message.Thinking)) if raw != "" && raw != "null" { - reasoningBuilder.WriteString(raw) + // Unmarshal the JSON string to get the actual content without quotes + var thinkingContent string + if err := json.Unmarshal(ck.Message.Thinking, &thinkingContent); err == nil { + reasoningBuilder.WriteString(thinkingContent) + } else { + // Fallback to raw string if it's not a JSON string + reasoningBuilder.WriteString(raw) + } } } if ck.Message != nil && ck.Message.Content != "" { @@ -229,7 +243,14 @@ func ollamaChatHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.R if len(single.Message.Thinking) > 0 { raw := strings.TrimSpace(string(single.Message.Thinking)) if raw != "" && raw != "null" { - reasoningBuilder.WriteString(raw) + // Unmarshal the JSON string to get the actual content without quotes + var thinkingContent string + if err := json.Unmarshal(single.Message.Thinking, &thinkingContent); err == nil { + reasoningBuilder.WriteString(thinkingContent) + } else { + // Fallback to raw string if it's not a JSON string + reasoningBuilder.WriteString(raw) + } } } aggContent.WriteString(single.Message.Content)