feat(proactive): 优化主动聊天逻辑,增加搜索前判断与回复前检查
为了提升主动聊天的智能化和自然度,本次更新引入了两个核心优化: 1. **搜索前决策**: 在为主动话题进行网络搜索前,新增一个LLM决策步骤,判断该话题是否有搜索的必要性。这避免了对日常寒暄或无需外部信息的话题进行不必要的搜索,提高了效率。 2. **回复前检查强化**: 严格化了主动回复的触发条件。现在,系统会检查自己的上一条消息是否得到回应,如果无人回应则倾向于保持沉默,以避免连续发言造成打扰。同时,所有判断标准需全部满足才能触发回复,使行为更符合社交直觉。
This commit is contained in:
@@ -159,27 +159,46 @@ class ProactiveThinker:
|
|||||||
|
|
||||||
news_block = "暂时没有获取到最新资讯。"
|
news_block = "暂时没有获取到最新资讯。"
|
||||||
if trigger_event.source != "reminder_system":
|
if trigger_event.source != "reminder_system":
|
||||||
try:
|
# 增加搜索前决策
|
||||||
web_search_tool = tool_api.get_tool_instance("web_search")
|
should_search_prompt = f"""
|
||||||
if web_search_tool:
|
# 搜索决策
|
||||||
try:
|
|
||||||
search_result_dict = await web_search_tool.execute(function_args={"keyword": topic, "max_results": 10})
|
## 任务
|
||||||
except TypeError:
|
判断是否有必要为了话题“{topic}”进行网络搜索。
|
||||||
|
|
||||||
|
## 判断标准
|
||||||
|
- **需要搜索**:时事新闻、知识查询、具体事件等需要外部信息的话题。
|
||||||
|
- **无需搜索**:日常关心、个人感受、延续已有对话等不需要外部信息的话题。
|
||||||
|
|
||||||
|
## 你的决策
|
||||||
|
输出`SEARCH`或`SKIP`。
|
||||||
|
"""
|
||||||
|
from src.llm_models.utils_model import LLMRequest
|
||||||
|
from src.config.config import model_config
|
||||||
|
|
||||||
|
decision_llm = LLMRequest(
|
||||||
|
model_set=model_config.model_task_config.planner,
|
||||||
|
request_type="planner"
|
||||||
|
)
|
||||||
|
|
||||||
|
decision, _ = await decision_llm.generate_response_async(prompt=should_search_prompt)
|
||||||
|
|
||||||
|
if "SEARCH" in decision:
|
||||||
|
try:
|
||||||
|
web_search_tool = tool_api.get_tool_instance("web_search")
|
||||||
|
if web_search_tool and topic:
|
||||||
try:
|
try:
|
||||||
search_result_dict = await web_search_tool.execute(function_args={"keyword": topic, "max_results": 10})
|
search_result_dict = await web_search_tool.execute(function_args={"keyword": topic, "max_results": 10})
|
||||||
except TypeError:
|
if search_result_dict and not search_result_dict.get("error"):
|
||||||
logger.warning(f"{self.context.log_prefix} 网络搜索工具参数不匹配,跳过搜索")
|
news_block = search_result_dict.get("content", "未能提取有效资讯。")
|
||||||
news_block = "跳过网络搜索。"
|
elif search_result_dict:
|
||||||
search_result_dict = None
|
logger.warning(f"{self.context.log_prefix} 网络搜索返回错误: {search_result_dict.get('error')}")
|
||||||
|
except Exception as e:
|
||||||
if search_result_dict and not search_result_dict.get("error"):
|
logger.error(f"{self.context.log_prefix} 网络搜索执行失败: {e}")
|
||||||
news_block = search_result_dict.get("content", "未能提取有效资讯。")
|
else:
|
||||||
elif search_result_dict:
|
logger.warning(f"{self.context.log_prefix} 未找到 web_search 工具实例或主题为空。")
|
||||||
logger.warning(f"{self.context.log_prefix} 网络搜索返回错误: {search_result_dict.get('error')}")
|
except Exception as e:
|
||||||
else:
|
logger.error(f"{self.context.log_prefix} 主动思考时网络搜索失败: {e}")
|
||||||
logger.warning(f"{self.context.log_prefix} 未找到 web_search 工具实例。")
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"{self.context.log_prefix} 主动思考时网络搜索失败: {e}")
|
|
||||||
message_list = get_raw_msg_before_timestamp_with_chat(
|
message_list = get_raw_msg_before_timestamp_with_chat(
|
||||||
chat_id=self.context.stream_id,
|
chat_id=self.context.stream_id,
|
||||||
timestamp=time.time(),
|
timestamp=time.time(),
|
||||||
@@ -201,15 +220,17 @@ class ProactiveThinker:
|
|||||||
{chat_context_block}
|
{chat_context_block}
|
||||||
|
|
||||||
## 合理判断标准
|
## 合理判断标准
|
||||||
请检查以下条件,如果**大部分条件都合理**就可以回复:
|
请检查以下条件,如果**所有条件都合理**就可以回复:
|
||||||
|
|
||||||
1. **时间合理性**:当前时间是否在深夜(凌晨2点-6点)这种不适合主动聊天的时段?
|
1. **回应检查**:检查你({bot_name})发送的最后一条消息之后,是否有其他人发言。如果没有,则大概率应该保持沉默。
|
||||||
2. **内容价值**:这个话题"{topic}"是否有意义,不是完全无关紧要的内容?
|
2. **话题补充**:只有当你认为准备发起的话题是对上一条无人回应消息的**有价值的补充**时,才可以在上一条消息无人回应的情况下继续发言。
|
||||||
3. **重复避免**:你准备说的话题是否与最近2条消息明显重复?
|
3. **时间合理性**:当前时间是否在深夜(凌晨2点-6点)这种不适合主动聊天的时段?
|
||||||
4. **自然性**:在当前上下文中主动提起这个话题是否自然合理?
|
4. **内容价值**:这个话题"{topic}"是否有意义,不是完全无关紧要的内容?
|
||||||
|
5. **重复避免**:你准备说的话题是否与你自己的上一条消息明显重复?
|
||||||
|
6. **自然性**:在当前上下文中主动提起这个话题是否自然合理?
|
||||||
|
|
||||||
## 输出要求
|
## 输出要求
|
||||||
如果判断应该跳过(比如深夜时段、完全无意义话题、明显重复内容),输出:SKIP_PROACTIVE_REPLY
|
如果判断应该跳过(比如上一条消息无人回应、深夜时段、无意义话题、重复内容),输出:SKIP_PROACTIVE_REPLY
|
||||||
其他情况都应该输出:PROCEED_TO_REPLY
|
其他情况都应该输出:PROCEED_TO_REPLY
|
||||||
|
|
||||||
请严格按照上述格式输出,不要添加任何解释。"""
|
请严格按照上述格式输出,不要添加任何解释。"""
|
||||||
|
|||||||
Reference in New Issue
Block a user