mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-04-19 09:08:38 +00:00
fix: handle JSON parsing for thinking content in ollama stream and chat handlers
This commit is contained in:
@@ -121,7 +121,14 @@ 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" {
|
||||||
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
|
// 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 {
|
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" {
|
||||||
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 != "" {
|
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 {
|
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" {
|
||||||
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)
|
aggContent.WriteString(single.Message.Content)
|
||||||
|
|||||||
Reference in New Issue
Block a user