feat(ai): clean reasoning content from replies

This commit is contained in:
liuwei
2026-04-07 09:23:48 +08:00
parent 496463c442
commit 51fe971cda
4 changed files with 73 additions and 10 deletions

View File

@@ -22,3 +22,36 @@ def remove_grok_render_tags(text: str) -> str:
cleaned = re.sub(r'<\s*grok:[^>]+?>[\s\S]*?<\s*/\s*grok:[^>]+?>', '', text, flags=re.IGNORECASE)
cleaned = re.sub(r'<\s*grok:[^>]+?/?>', '', cleaned, flags=re.IGNORECASE)
return cleaned
def remove_reasoning_content(text: str) -> str:
"""移除模型返回中的思考/推理内容,仅保留最终可展示结果。"""
if not text:
return text
cleaned = text
# 常见的思考标签
cleaned = re.sub(r'<\s*think\s*>[\s\S]*?<\s*/\s*think\s*>', '', cleaned, flags=re.IGNORECASE)
cleaned = re.sub(r'<\s*thinking\s*>[\s\S]*?<\s*/\s*thinking\s*>', '', cleaned, flags=re.IGNORECASE)
cleaned = re.sub(r'<\s*reasoning\s*>[\s\S]*?<\s*/\s*reasoning\s*>', '', cleaned, flags=re.IGNORECASE)
# 常见 markdown 思考段落
cleaned = re.sub(
r'^\s*(#{1,6}\s*)?(思考过程|推理过程|分析过程|reasoning|thinking)\s*[:]?\s*$[\s\S]*?(?=^\s*(#{1,6}\s*)?\S|\Z)',
'',
cleaned,
flags=re.IGNORECASE | re.MULTILINE
)
# 某些模型会输出“思考内容:...最终答案:...”
cleaned = re.sub(
r'^\s*(思考内容|思维链|推理过程|分析过程)\s*[:][\s\S]*?(?=(最终答案|总结|摘要|#|\Z))',
'',
cleaned,
flags=re.IGNORECASE
)
cleaned = remove_grok_render_tags(cleaned)
cleaned = re.sub(r'\n{3,}', '\n\n', cleaned).strip()
return cleaned