Commit e4a2904c by somnifex

fix: handle JSON parsing for thinking content in ollama stream and chat handlers

parent 2df75d14
...@@ -121,9 +121,16 @@ func ollamaStreamHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http ...@@ -121,9 +121,16 @@ func ollamaStreamHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http
if chunk.Message != nil && len(chunk.Message.Thinking) > 0 { if chunk.Message != nil && len(chunk.Message.Thinking) > 0 {
raw := strings.TrimSpace(string(chunk.Message.Thinking)) raw := strings.TrimSpace(string(chunk.Message.Thinking))
if raw != "" && raw != "null" { if raw != "" && raw != "null" {
// 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) delta.Choices[0].Delta.SetReasoningContent(raw)
} }
} }
}
// tool calls // tool calls
if chunk.Message != nil && len(chunk.Message.ToolCalls) > 0 { if chunk.Message != nil && len(chunk.Message.ToolCalls) > 0 {
delta.Choices[0].Delta.ToolCalls = make([]dto.ToolCallResponse, 0, len(chunk.Message.ToolCalls)) delta.Choices[0].Delta.ToolCalls = make([]dto.ToolCallResponse, 0, len(chunk.Message.ToolCalls))
...@@ -209,9 +216,16 @@ func ollamaChatHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.R ...@@ -209,9 +216,16 @@ func ollamaChatHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.R
if ck.Message != nil && len(ck.Message.Thinking) > 0 { if ck.Message != nil && len(ck.Message.Thinking) > 0 {
raw := strings.TrimSpace(string(ck.Message.Thinking)) raw := strings.TrimSpace(string(ck.Message.Thinking))
if raw != "" && raw != "null" { if raw != "" && raw != "null" {
// 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) reasoningBuilder.WriteString(raw)
} }
} }
}
if ck.Message != nil && ck.Message.Content != "" { if ck.Message != nil && ck.Message.Content != "" {
aggContent.WriteString(ck.Message.Content) aggContent.WriteString(ck.Message.Content)
} else if ck.Response != "" { } else if ck.Response != "" {
...@@ -229,9 +243,16 @@ func ollamaChatHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.R ...@@ -229,9 +243,16 @@ func ollamaChatHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.R
if len(single.Message.Thinking) > 0 { if len(single.Message.Thinking) > 0 {
raw := strings.TrimSpace(string(single.Message.Thinking)) raw := strings.TrimSpace(string(single.Message.Thinking))
if raw != "" && raw != "null" { if raw != "" && raw != "null" {
// 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) reasoningBuilder.WriteString(raw)
} }
} }
}
aggContent.WriteString(single.Message.Content) aggContent.WriteString(single.Message.Content)
} else { } else {
aggContent.WriteString(single.Response) aggContent.WriteString(single.Response)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment