xiaozhi-esp32-server/main/xiaozhi-server/tests
Allen b1f6b09311 fix(llm): 思考块和正文在同一个 chunk 里时不再吞掉整段回复
现象:接 DeepSeek / Qwen3 这类推理模型时,设备一句话都不说,或者说到
一半就断掉。日志里也看不到任何异常。

原因:response() 里处理 <think> 标签的顺序反了 —— 先按 <think> 把
content 截断,截断后的 content 里已经没有 </think> 了,第二个 if 永远
进不去,「正在思考」这个状态也就永远退不出去。

    if "<think>" in content:
        is_active = False
        content = content.split("<think>")[0]   # 截断后匹配不到 </think>
    if "</think>" in content:                   # 永远进不来
        is_active = True

服务端把整段思考块放进同一个 chunk 时(<think>推理</think>正文),
实测输出是空的,而且 is_active 停在 False,后续 chunk 也继续被丢。
另一头,<think> 之前的正文同样被丢掉。

改法:把标签处理抽成模块级生成器 iter_answer_text(),按标签出现的
顺序扫一遍,跨 chunk 的状态用 in_think 记住:
- <think> 之前的文本是正文,照常输出
- <think> 和 </think> 之间是推理内容,丢掉
- </think> 之后的文本又是正文,照常输出

response() 改成把 delta.content 依次喂给它,仍然是边收边吐,不影响
流式。

测试:
- 新增 tests/core/providers/llm/test_think_tag_filter.py:七种输入形态
  的输出断言(同一 chunk 整段 / 跨 chunk 拆开 / 前缀 / 只有闭标签 /
  思考块之后的 chunk / 空 chunk / 纯正文)
- 全量 pytest tests/ -q 无新增失败

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-11-07 22:36:35 +08:00
..
config 加配置合并函数测试,7 例 2026-09-02 22:24:35 +08:00
core fix(llm): 思考块和正文在同一个 chunk 里时不再吞掉整段回复 2026-11-07 22:36:35 +08:00
plugins_func fix: 修复ha读取异常 2026-11-05 15:36:04 +08:00
__init__.py 加 Python 冒烟测试,验证 pytest 流水线 2026-09-02 22:24:35 +08:00
conftest.py 让 CI 真正跑起三类测试并修掉挡路的测试缺陷 (#3475) 2026-11-03 17:44:29 +08:00
test_llm.py 让 CI 真正跑起三类测试并修掉挡路的测试缺陷 (#3475) 2026-11-03 17:44:29 +08:00
test_manage_api_client_lifecycle.py 迁移测试用例,优化相关注释 2026-10-13 14:31:21 +08:00
test_memory.py chore(deps): 批量升级 4 个允许的依赖 (#3374) 2026-09-17 15:57:43 +08:00
test_rag.py chore(deps): 批量升级 4 个允许的依赖 (#3374) 2026-09-17 15:57:43 +08:00
test_smoke.py 加 Python 冒烟测试,验证 pytest 流水线 2026-09-02 22:24:35 +08:00
test_stt.py chore(deps): 批量升级 4 个允许的依赖 (#3374) 2026-09-17 15:57:43 +08:00
test_tts.py chore(deps): 批量升级 4 个允许的依赖 (#3374) 2026-09-17 15:57:43 +08:00