feat(chat): 实现由 LLM 控制的自然回复分割
之前基于标点符号的自动分割逻辑较为僵硬,有时会破坏回复的连贯性,导致对话体验不佳。 本次更新引入了一种由 LLM 主导的回复分割机制: 1. 在 Prompt 中增加了明确的分割指令,引导 LLM 在需要模拟人类对话停顿或转折时,使用 `[SPLIT]` 标记。 2. 后端回复处理逻辑相应更新,优先根据 `[SPLIT]` 标记分割消息。 3. 若 LLM 未提供 `[SPLIT]` 标记,则将整段回复作为单条消息发送,避免了不必要的拆分。 此项改动旨在让消息的发送节奏更贴近真实人类的聊天习惯,从而提升交互的自然感和流畅度。
This commit is contained in:
@@ -136,7 +136,7 @@ def init_prompt():
|
||||
4. 不要浮夸,不要夸张修辞,不要输出多余内容(包括前后缀,冒号和引号,括号(),表情包,at或 @等 )。
|
||||
最终请输出一条简短、完整且口语化的回复。
|
||||
|
||||
--------------------------------
|
||||
--------------------------------
|
||||
{time_block}
|
||||
|
||||
{reply_target_block}
|
||||
@@ -1024,6 +1024,21 @@ class DefaultReplyer:
|
||||
prompt = Prompt(template=template_prompt.template, parameters=prompt_parameters)
|
||||
prompt_text = await prompt.build()
|
||||
|
||||
# --- 动态添加分割指令 ---
|
||||
if global_config.response_splitter.enable:
|
||||
split_instruction = """
|
||||
## 分割指令
|
||||
你正在通过一个即时聊天软件发送消息。请模仿一个真实人类的打字和发送习惯:
|
||||
- **简洁明了**: 如果一句话能说清楚,就一次性发出去,不要添加任何标记。
|
||||
- **自然断句**: 当你想表达一个转折、一个停顿,或者想补充说明时,就像正常人会先发一部分再发另一部分一样,请在断句处插入 `[SPLIT]` 标记。
|
||||
- **动机**: 使用 `[SPLIT]` 的目的是为了让对话节奏更自然,更有层次感,而不是为了分割而分割。
|
||||
示例: "我刚刚看到一个超好笑的视频![SPLIT]等我找找发给你~"
|
||||
"""
|
||||
# 在 "现在,你说:" 之前插入
|
||||
parts = prompt_text.rsplit("现在,你说:", 1)
|
||||
if len(parts) == 2:
|
||||
prompt_text = f"{parts[0]}{split_instruction}\n现在,你说:{parts[1]}"
|
||||
|
||||
return prompt_text
|
||||
|
||||
async def build_prompt_rewrite_context(
|
||||
|
||||
@@ -331,8 +331,17 @@ def process_llm_response(text: str, enable_splitter: bool = True, enable_chinese
|
||||
)
|
||||
|
||||
if global_config.response_splitter.enable and enable_splitter:
|
||||
split_sentences = split_into_sentences_w_remove_punctuation(cleaned_text)
|
||||
logger.info("回复分割器已启用。")
|
||||
if "[SPLIT]" in cleaned_text:
|
||||
split_sentences_raw = cleaned_text.split("[SPLIT]")
|
||||
# 清理每个句子首尾可能由LLM添加的空格或换行符,并移除空句子
|
||||
split_sentences = [s.strip() for s in split_sentences_raw if s.strip()]
|
||||
logger.debug(f"LLM 自定义分割结果: {split_sentences}")
|
||||
else:
|
||||
# 如果没有 [SPLIT] 标记,则不进行任何分割
|
||||
split_sentences = [cleaned_text]
|
||||
else:
|
||||
logger.debug("回复分割器已禁用。")
|
||||
split_sentences = [cleaned_text]
|
||||
|
||||
sentences = []
|
||||
|
||||
Reference in New Issue
Block a user