diff --git a/src/chat/chat_loop/proactive/proactive_thinker.py b/src/chat/chat_loop/proactive/proactive_thinker.py index 4abaccebd..d1716e75e 100644 --- a/src/chat/chat_loop/proactive/proactive_thinker.py +++ b/src/chat/chat_loop/proactive/proactive_thinker.py @@ -159,27 +159,46 @@ class ProactiveThinker: news_block = "暂时没有获取到最新资讯。" if trigger_event.source != "reminder_system": - try: - web_search_tool = tool_api.get_tool_instance("web_search") - if web_search_tool: - try: - search_result_dict = await web_search_tool.execute(function_args={"keyword": topic, "max_results": 10}) - except TypeError: + # 增加搜索前决策 + should_search_prompt = f""" +# 搜索决策 + +## 任务 +判断是否有必要为了话题“{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: search_result_dict = await web_search_tool.execute(function_args={"keyword": topic, "max_results": 10}) - except TypeError: - logger.warning(f"{self.context.log_prefix} 网络搜索工具参数不匹配,跳过搜索") - news_block = "跳过网络搜索。" - search_result_dict = None - - if search_result_dict and not search_result_dict.get("error"): - news_block = search_result_dict.get("content", "未能提取有效资讯。") - elif search_result_dict: - logger.warning(f"{self.context.log_prefix} 网络搜索返回错误: {search_result_dict.get('error')}") - else: - logger.warning(f"{self.context.log_prefix} 未找到 web_search 工具实例。") - except Exception as e: - logger.error(f"{self.context.log_prefix} 主动思考时网络搜索失败: {e}") + if search_result_dict and not search_result_dict.get("error"): + news_block = search_result_dict.get("content", "未能提取有效资讯。") + elif search_result_dict: + logger.warning(f"{self.context.log_prefix} 网络搜索返回错误: {search_result_dict.get('error')}") + except Exception as e: + logger.error(f"{self.context.log_prefix} 网络搜索执行失败: {e}") + else: + 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( chat_id=self.context.stream_id, timestamp=time.time(), @@ -201,15 +220,17 @@ class ProactiveThinker: {chat_context_block} ## 合理判断标准 -请检查以下条件,如果**大部分条件都合理**就可以回复: +请检查以下条件,如果**所有条件都合理**就可以回复: -1. **时间合理性**:当前时间是否在深夜(凌晨2点-6点)这种不适合主动聊天的时段? -2. **内容价值**:这个话题"{topic}"是否有意义,不是完全无关紧要的内容? -3. **重复避免**:你准备说的话题是否与最近2条消息明显重复? -4. **自然性**:在当前上下文中主动提起这个话题是否自然合理? +1. **回应检查**:检查你({bot_name})发送的最后一条消息之后,是否有其他人发言。如果没有,则大概率应该保持沉默。 +2. **话题补充**:只有当你认为准备发起的话题是对上一条无人回应消息的**有价值的补充**时,才可以在上一条消息无人回应的情况下继续发言。 +3. **时间合理性**:当前时间是否在深夜(凌晨2点-6点)这种不适合主动聊天的时段? +4. **内容价值**:这个话题"{topic}"是否有意义,不是完全无关紧要的内容? +5. **重复避免**:你准备说的话题是否与你自己的上一条消息明显重复? +6. **自然性**:在当前上下文中主动提起这个话题是否自然合理? ## 输出要求 -如果判断应该跳过(比如深夜时段、完全无意义话题、明显重复内容),输出:SKIP_PROACTIVE_REPLY +如果判断应该跳过(比如上一条消息无人回应、深夜时段、无意义话题、重复内容),输出:SKIP_PROACTIVE_REPLY 其他情况都应该输出:PROCEED_TO_REPLY 请严格按照上述格式输出,不要添加任何解释。"""