feat(ai_router): 添加调试日志以跟踪AI路由处理流程

This commit is contained in:
zihanjian
2025-07-18 14:11:36 +08:00
parent f86e2deee3
commit 8b5eed4b40
4 changed files with 25 additions and 2 deletions

View File

@@ -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最新版本的特性",
"帮我查查如何学习机器学习",

View File

@@ -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")

View File

@@ -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

View File

@@ -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: