mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-23 03:19:41 +00:00
Merge b1f6b09311 into d7751f468e
This commit is contained in:
commit
7a7a1e8478
3 changed files with 99 additions and 10 deletions
|
|
@ -25,6 +25,46 @@ THINKING_DISABLED_MODEL_EXCLUSIONS = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def iter_answer_text(contents):
|
||||||
|
"""从流式响应里挑出正文,丢掉 ``<think>...</think>`` 中的推理内容。
|
||||||
|
|
||||||
|
服务端有时把整段思考块放进同一个 chunk(``<think>推理</think>正文``),
|
||||||
|
有时又拆成多个 chunk。原来的写法先按 ``<think>`` 截断,截断后的
|
||||||
|
content 里已经没有 ``</think>`` 了,于是「正在思考」这个状态永远
|
||||||
|
退不出去 —— 整段回复连同后面的 chunk 全被丢掉,设备一句话都不说。
|
||||||
|
|
||||||
|
这里按标签出现的顺序扫一遍,跨 chunk 的状态用 in_think 记住:
|
||||||
|
- ``<think>`` 之前的文本是正文,照常输出
|
||||||
|
- ``<think>`` 和 ``</think>`` 之间是推理内容,丢掉
|
||||||
|
- ``</think>`` 之后的文本又是正文,照常输出
|
||||||
|
"""
|
||||||
|
in_think = False
|
||||||
|
for content in contents:
|
||||||
|
if not content:
|
||||||
|
continue
|
||||||
|
while content:
|
||||||
|
if in_think:
|
||||||
|
# 还在思考块里,等 </think> 出现;这之前的内容都是推理过程
|
||||||
|
if "</think>" not in content:
|
||||||
|
content = ""
|
||||||
|
break
|
||||||
|
_, _, content = content.partition("</think>")
|
||||||
|
in_think = False
|
||||||
|
continue
|
||||||
|
if "<think>" in content:
|
||||||
|
before, _, content = content.partition("<think>")
|
||||||
|
if before:
|
||||||
|
yield before
|
||||||
|
in_think = True
|
||||||
|
continue
|
||||||
|
if "</think>" in content:
|
||||||
|
# 没有开标签只有闭标签:从闭标签之后开始算正文
|
||||||
|
_, _, content = content.partition("</think>")
|
||||||
|
continue
|
||||||
|
yield content
|
||||||
|
content = ""
|
||||||
|
|
||||||
|
|
||||||
class LLMProvider(LLMProviderBase):
|
class LLMProvider(LLMProviderBase):
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.model_name = config.get("model_name")
|
self.model_name = config.get("model_name")
|
||||||
|
|
@ -143,8 +183,8 @@ class LLMProvider(LLMProviderBase):
|
||||||
|
|
||||||
responses = self.client.chat.completions.create(**request_params)
|
responses = self.client.chat.completions.create(**request_params)
|
||||||
|
|
||||||
is_active = True
|
def _contents():
|
||||||
try:
|
"""把流式 chunk 里的 delta.content 依次吐出来。"""
|
||||||
for chunk in responses:
|
for chunk in responses:
|
||||||
try:
|
try:
|
||||||
delta = chunk.choices[0].delta if getattr(chunk, "choices", None) else None
|
delta = chunk.choices[0].delta if getattr(chunk, "choices", None) else None
|
||||||
|
|
@ -152,14 +192,11 @@ class LLMProvider(LLMProviderBase):
|
||||||
except IndexError:
|
except IndexError:
|
||||||
content = ""
|
content = ""
|
||||||
if content:
|
if content:
|
||||||
if "<think>" in content:
|
yield content
|
||||||
is_active = False
|
|
||||||
content = content.split("<think>")[0]
|
try:
|
||||||
if "</think>" in content:
|
for text in iter_answer_text(_contents()):
|
||||||
is_active = True
|
yield text
|
||||||
content = content.split("</think>")[-1]
|
|
||||||
if is_active:
|
|
||||||
yield content
|
|
||||||
finally:
|
finally:
|
||||||
responses.close()
|
responses.close()
|
||||||
|
|
||||||
|
|
|
||||||
0
main/xiaozhi-server/tests/core/providers/llm/__init__.py
Normal file
0
main/xiaozhi-server/tests/core/providers/llm/__init__.py
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
"""iter_answer_text:把推理模型的思考内容剥掉,只留正文。
|
||||||
|
|
||||||
|
背景:DeepSeek / Qwen3 这类推理模型会在正文前面塞一段
|
||||||
|
<think>...</think> 的推理过程。原来的实现先按 <think> 截断 content,
|
||||||
|
同一个 chunk 里带着 </think> 时,截断后就再也匹配不到闭标签,
|
||||||
|
「正在思考」这个状态永远退不出去 —— 整段回复连同后面的 chunk 全被
|
||||||
|
丢掉,设备一句话都不说。
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def _answer_text(contents):
|
||||||
|
"""跑一遍 iter_answer_text,返回它吐出来的文本列表。
|
||||||
|
|
||||||
|
导入放在函数里:core.providers.llm.openai.openai 在 import 期就要
|
||||||
|
config/settings.py 里的 data/.config.yaml,缺了会直接抛
|
||||||
|
FileNotFoundError,把整个收集阶段带崩。
|
||||||
|
"""
|
||||||
|
from core.providers.llm.openai.openai import iter_answer_text
|
||||||
|
|
||||||
|
return list(iter_answer_text(contents))
|
||||||
|
|
||||||
|
|
||||||
|
def test_plain_text_passes_through():
|
||||||
|
assert _answer_text(["你好", ",", "在吗"]) == ["你好", ",", "在吗"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_think_block_in_one_chunk():
|
||||||
|
"""服务端一次性吐出整段思考块时,正文必须还在。"""
|
||||||
|
assert _answer_text(["<think>推理过程</think>你好"]) == ["你好"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_think_block_split_across_chunks():
|
||||||
|
"""思考块被拆成多个 chunk 时,只有推理内容被丢掉。"""
|
||||||
|
assert _answer_text(["<think>", "推理", "过程", "</think>", "你好"]) == ["你好"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_text_before_think_is_kept():
|
||||||
|
assert _answer_text(["前缀<think>推理</think>正文"]) == ["前缀", "正文"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_closing_tag_without_opening_tag():
|
||||||
|
"""只有闭标签时,从闭标签之后开始算正文。"""
|
||||||
|
assert _answer_text(["正文</think>尾巴"]) == ["尾巴"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_chunks_after_think_are_kept():
|
||||||
|
"""思考块结束后,后续 chunk 必须继续输出,不能整段丢。"""
|
||||||
|
assert _answer_text(["<think>abc</think>", "第一句", "第二句"]) == ["第一句", "第二句"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_empty_chunks_are_ignored():
|
||||||
|
assert _answer_text(["", "你好", ""]) == ["你好"]
|
||||||
Loading…
Reference in a new issue