mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-29 18:23:59 +08:00
fix:优化对话过程中JSON闭合符号清理的问题
This commit is contained in:
@@ -1020,15 +1020,18 @@ class ConnectionHandler:
|
|||||||
safe_end = max(sent_len, len(da_text) - _DA_STREAM_BUFFER)
|
safe_end = max(sent_len, len(da_text) - _DA_STREAM_BUFFER)
|
||||||
if safe_end > sent_len:
|
if safe_end > sent_len:
|
||||||
new_part = da_text[sent_len:safe_end]
|
new_part = da_text[sent_len:safe_end]
|
||||||
tc["_da_sent"] = safe_end
|
# 清理 delta 中可能泄漏的 JSON 闭合垃圾
|
||||||
self.tts.tts_text_queue.put(
|
new_part = self._clean_response_garbage(new_part)
|
||||||
TTSMessageDTO(
|
if new_part:
|
||||||
sentence_id=current_sentence_id,
|
tc["_da_sent"] = safe_end
|
||||||
sentence_type=SentenceType.MIDDLE,
|
self.tts.tts_text_queue.put(
|
||||||
content_type=ContentType.TEXT,
|
TTSMessageDTO(
|
||||||
content_detail=new_part,
|
sentence_id=current_sentence_id,
|
||||||
|
sentence_type=SentenceType.MIDDLE,
|
||||||
|
content_type=ContentType.TEXT,
|
||||||
|
content_detail=new_part,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
content = response
|
content = response
|
||||||
|
|
||||||
@@ -1116,7 +1119,7 @@ class ConnectionHandler:
|
|||||||
sent_len = tc.get("_da_sent", 0)
|
sent_len = tc.get("_da_sent", 0)
|
||||||
remaining = da_response[sent_len:]
|
remaining = da_response[sent_len:]
|
||||||
if remaining:
|
if remaining:
|
||||||
remaining = self._clean_trailing_json_garbage(remaining)
|
remaining = self._clean_response_garbage(remaining)
|
||||||
if remaining:
|
if remaining:
|
||||||
self.tts.tts_text_queue.put(
|
self.tts.tts_text_queue.put(
|
||||||
TTSMessageDTO(
|
TTSMessageDTO(
|
||||||
@@ -1127,7 +1130,7 @@ class ConnectionHandler:
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
# 写入对话历史
|
# 写入对话历史
|
||||||
da_response = self._clean_trailing_json_garbage(da_response)
|
da_response = self._clean_response_garbage(da_response)
|
||||||
self.tts.store_tts_text(current_sentence_id, da_response)
|
self.tts.store_tts_text(current_sentence_id, da_response)
|
||||||
self.dialogue.put(Message(role="assistant", content=da_response))
|
self.dialogue.put(Message(role="assistant", content=da_response))
|
||||||
|
|
||||||
@@ -1594,18 +1597,26 @@ class ConnectionHandler:
|
|||||||
return raw
|
return raw
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _clean_trailing_json_garbage(text):
|
def _clean_response_garbage(text):
|
||||||
"""清理 response 末尾可能泄漏的 JSON 闭合符号。
|
"""清理 response 中可能泄漏的 JSON 闭合符号。
|
||||||
模型有时会在 response 内容中生成 JSON 闭合字符(如 )"}} 或 '}),
|
模型有时会在 response 内容中生成 JSON 闭合字符(如 )"}} 或 '}),
|
||||||
这些不是故事内容的一部分,需要去除。
|
这些不是故事内容的一部分,需要去除。
|
||||||
"""
|
"""
|
||||||
if not text:
|
if not text:
|
||||||
return text
|
return text
|
||||||
# 匹配末尾的 JSON 闭合垃圾模式:
|
# 清理独立一行的 JSON 闭合垃圾(如 )"}} '}} "}} }} } )
|
||||||
# 可选的中文标点 )'),紧跟 " 和 } 的组合,中间可夹杂空白/换行
|
_garbage_chars = frozenset('")\'})')
|
||||||
# 例如:)"}} '}" }} "}} 等
|
lines = text.split('\n')
|
||||||
cleaned = re.sub(r'[\s\n]*[)\)]?[\s\n]*["\'}\]]{1,6}$', '', text)
|
cleaned = []
|
||||||
return cleaned if cleaned else text
|
for line in lines:
|
||||||
|
stripped = line.strip()
|
||||||
|
if stripped and len(stripped) <= 8 and all(c in _garbage_chars for c in stripped):
|
||||||
|
continue
|
||||||
|
cleaned.append(line)
|
||||||
|
result = '\n'.join(cleaned)
|
||||||
|
# 清理末尾残留的 JSON 闭合符号
|
||||||
|
result = re.sub(r'["\'}\]]+$', '', result.rstrip()).rstrip()
|
||||||
|
return result
|
||||||
|
|
||||||
def _merge_tool_calls(self, tool_calls_list, tools_call):
|
def _merge_tool_calls(self, tool_calls_list, tools_call):
|
||||||
"""合并工具调用列表
|
"""合并工具调用列表
|
||||||
|
|||||||
Reference in New Issue
Block a user