refactor(chat): overhaul response processing for robustness and integrity

This commit redesigns the LLM response handling pipeline to gracefully manage complex, long, and structured outputs, shifting from a restrictive filtering model to a more resilient and content-aware approach.

The previous implementation suffered from brittleness, often discarding valid responses or corrupting formatted content. This refactor introduces two core changes to ensure response integrity:

1.  **Adaptive Message Merging:** Replaces the rigid message count and length limits, which caused the silent dropping of valid long-form answers. The new logic dynamically merges the shortest adjacent sentences if a response is overly fragmented, ensuring the full thought is preserved while adhering to display limits.

2.  **Content-Aware Pre-processing:** Implements a protection mechanism for special text formats. It now identifies and isolates Markdown code blocks, LaTeX-style math equations, and other code-like segments using placeholders before splitting or typo generation. This prevents formatting corruption and ensures technical content is rendered correctly.

Additionally, the LLM prompt for splitting has been refined to emphasize logical coherence over simple fragmentation.

Finally, this commit also hardens the `LLMRequest` class against race conditions by adding an `asyncio.Lock` to serialize generation requests, improving the system's stability under concurrent workloads.
This commit is contained in:
tt-P607
2025-10-17 02:34:39 +08:00
committed by Windpicker-owo
parent a095bc27fb
commit 87b1d5aec4
3 changed files with 146 additions and 59 deletions

View File

@@ -1545,19 +1545,19 @@ class DefaultReplyer:
# --- 动态添加分割指令 ---
if global_config.response_splitter.enable and global_config.response_splitter.split_mode == "llm":
split_instruction = """
## 消息分段指导
你的任务是将回复分割成多段发送,以模仿人类自然、富有节奏感的聊天方式。
## 关于回复分割的一些小建议
**核心指导**:
1. **短句优先**: 请尽量将回复拆分成 20-30 字左右的短句,模拟逐步思考和表达的节奏。
2. **智能分割时机**: 为了让分割更自然,**推荐**在以下时机插入 `[SPLIT]` 标记:
- **标点符号后**: 这是最常见的分割点,如句号、逗号、问号、感叹号之后。
- **逻辑转折处**: 当意思发生转变时,例如使用“但是”、“不过”、“而且”、“所以”等词语后。
- **话题切换时**: 当你准备从一个话题跳到另一个相关话题时。
3. **保持流畅**: 如果一句话本身就很短,或者分割后会显得不自然,就不要强行分割。
这个指令的**唯一目的**是为了**提高可读性**,将一个**单一、完整的回复**拆分成视觉上更易读的短句,**而不是让你生成多个不同的回复**。
**任务**:
请像一个真正在聊天的朋友一样,参考以上建议,自然地决定在哪里插入 `[SPLIT]` 标记,让对话显得更生动。
请在思考好的、连贯的回复中,找到合适的停顿点插入 `[SPLIT]` 标记。
**最重要的原则:**
- **禁止内容重复**:分割后的各个部分必须是**一个连贯思想的不同阶段**,绝不能是相似意思的重复表述。
**一些可以参考的分割时机:**
1. **短句优先**: 整体上,让每个分割后的句子长度在 20-30 字左右会显得很自然。
2. **自然停顿**: 在自然的标点符号(如逗号、问号)后,或者在逻辑转折词(如“而且”、“不过”)后,都是不错的分割点。
3. **保留连贯性**: 请确保所有被 `[SPLIT]` 分隔的句子能无缝拼接成一个逻辑通顺的完整回复。如果一句话很短,或者分割会破坏语感,就不要分割。
"""
# 将分段指令添加到提示词顶部
prompt_text = f"{split_instruction}\n{prompt_text}"