feat(metadata): add stripThinkingBlocks function and related tests
This commit is contained in:
@@ -189,6 +189,7 @@ func (c *Client) extractMetadataWithModel(ctx context.Context, input, model stri
|
||||
}
|
||||
|
||||
metadataText := strings.TrimSpace(resp.Choices[0].Message.Content)
|
||||
metadataText = stripThinkingBlocks(metadataText)
|
||||
metadataText = stripCodeFence(metadataText)
|
||||
metadataText = extractJSONObject(metadataText)
|
||||
if metadataText == "" {
|
||||
@@ -320,6 +321,30 @@ func extractJSONObject(s string) string {
|
||||
return s[start : end+1]
|
||||
}
|
||||
|
||||
// stripThinkingBlocks removes <think>...</think> and <thinking>...</thinking>
|
||||
// blocks produced by reasoning models (DeepSeek R1, QwQ, etc.) so that the
|
||||
// remaining text can be parsed as JSON without interference from thinking content
|
||||
// that may itself contain braces.
|
||||
func stripThinkingBlocks(s string) string {
|
||||
for _, tag := range []string{"think", "thinking"} {
|
||||
open := "<" + tag + ">"
|
||||
close := "</" + tag + ">"
|
||||
for {
|
||||
start := strings.Index(s, open)
|
||||
if start == -1 {
|
||||
break
|
||||
}
|
||||
end := strings.Index(s[start:], close)
|
||||
if end == -1 {
|
||||
s = s[:start]
|
||||
break
|
||||
}
|
||||
s = s[:start] + s[start+end+len(close):]
|
||||
}
|
||||
}
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
|
||||
func stripCodeFence(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if !strings.HasPrefix(value, "```") {
|
||||
|
||||
Reference in New Issue
Block a user