From 8b5eed4b40d450236d230e886d151e5a34a4610b Mon Sep 17 00:00:00 2001 From: zihanjian Date: Fri, 18 Jul 2025 14:11:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(ai=5Frouter):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=B0=83=E8=AF=95=E6=97=A5=E5=BF=97=E4=BB=A5=E8=B7=9F=E8=B8=AA?= =?UTF-8?q?AI=E8=B7=AF=E7=94=B1=E5=A4=84=E7=90=86=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/ai_functions.py | 4 ++-- commands/ai_router.py | 16 ++++++++++++++++ main.py | 2 ++ robot.py | 5 +++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/commands/ai_functions.py b/commands/ai_functions.py index b77e837..43f843f 100644 --- a/commands/ai_functions.py +++ b/commands/ai_functions.py @@ -72,7 +72,7 @@ def ai_handle_weather(ctx: MessageContext, params: str) -> bool: # ======== 新闻功能 ======== @ai_router.register( name="news_query", - description="获取当日新闻资讯(很长的流水账,如果用户要精简的新闻则不用)", + description="获取当日新闻资讯", examples=[ "看看今天的新闻", "有什么新闻吗", @@ -188,7 +188,7 @@ def ai_handle_reminder_delete(ctx: MessageContext, params: str) -> bool: # ======== Perplexity搜索功能 ======== @ai_router.register( name="perplexity_search", - description="使用Perplexity AI进行深度搜索和问答", + description="使用Perplexity AI进行深度搜索和问答,用于查询资料的场景", examples=[ "搜索一下Python最新版本的特性", "帮我查查如何学习机器学习", diff --git a/commands/ai_router.py b/commands/ai_router.py index c281d0c..81ad01e 100644 --- a/commands/ai_router.py +++ b/commands/ai_router.py @@ -94,7 +94,10 @@ class AIRouter: 返回: (是否处理成功, AI决策结果) """ + print(f"[AI路由器] route方法被调用") + if not ctx.text: + print("[AI路由器] ctx.text为空,返回False") return False, None # 获取AI模型 @@ -103,21 +106,29 @@ class AIRouter: chat_model = getattr(ctx.robot, 'chat', None) if ctx.robot else None if not chat_model: + print("[AI路由器] 无可用的AI模型") self.logger.error("AI路由器:无可用的AI模型") return False, None + print(f"[AI路由器] 找到AI模型: {type(chat_model)}") + try: # 构建系统提示词 system_prompt = self._build_ai_prompt() + print(f"[AI路由器] 已构建系统提示词,长度: {len(system_prompt)}") # 让AI分析用户意图 user_input = f"用户输入:{ctx.text}" + print(f"[AI路由器] 准备调用AI分析意图: {user_input}") + ai_response = chat_model.get_answer( user_input, wxid=ctx.get_receiver(), system_prompt_override=system_prompt ) + print(f"[AI路由器] AI响应: {ai_response}") + # 解析AI返回的JSON json_match = re.search(r'\{.*\}', ai_response, re.DOTALL) if not json_match: @@ -155,9 +166,14 @@ class AIRouter: 返回: 是否成功处理 """ + print(f"[AI路由器] dispatch被调用,消息内容: {ctx.text}") + # 获取AI路由决策 success, decision = self.route(ctx) + print(f"[AI路由器] route返回 - success: {success}, decision: {decision}") + if not success or not decision: + print("[AI路由器] route失败或无决策,返回False") return False action_type = decision.get("action_type") diff --git a/main.py b/main.py index b2b0c32..dffe2a4 100644 --- a/main.py +++ b/main.py @@ -32,6 +32,8 @@ logging.getLogger("httpx").setLevel(logging.ERROR) # 提高为 ERROR logging.getLogger("Weather").setLevel(logging.WARNING) logging.getLogger("ai_providers").setLevel(logging.WARNING) logging.getLogger("commands").setLevel(logging.WARNING) +# 临时调试:为AI路由器设置更详细的日志级别 +logging.getLogger("commands.ai_router").setLevel(logging.INFO) from function.func_report_reminder import ReportReminder from configuration import Config diff --git a/robot.py b/robot.py index 53e516a..f452b00 100644 --- a/robot.py +++ b/robot.py @@ -219,10 +219,15 @@ class Robot(Job): if not handled: # 只在被@或私聊时才使用AI路由 if (msg.from_group() and msg.is_at(self.wxid)) or not msg.from_group(): + print(f"[AI路由调试] 准备调用AI路由器处理消息: {msg.content}") ai_handled = ai_router.dispatch(ctx) + print(f"[AI路由调试] AI路由器处理结果: {ai_handled}") if ai_handled: self.LOG.info("消息已由AI路由器处理") + print("[AI路由调试] 消息已成功由AI路由器处理") return + else: + print("[AI路由调试] AI路由器未处理该消息") # 7. 如果没有命令处理器处理,则进行特殊逻辑处理 if not handled: