diff --git a/commands/ai_router.py b/commands/ai_router.py index 90d6606..7d9478b 100644 --- a/commands/ai_router.py +++ b/commands/ai_router.py @@ -7,6 +7,10 @@ from .context import MessageContext logger = logging.getLogger(__name__) +ROUTING_HISTORY_LIMIT = 10 +CHAT_HISTORY_MIN = 10 +CHAT_HISTORY_MAX = 300 + @dataclass class AIFunction: """AI可调用的功能定义""" @@ -81,7 +85,8 @@ class AIRouter: 1. 如果用户只是聊天或者不匹配任何功能,返回: { - "action_type": "chat" + "action_type": "chat", + "history_messages": 25 # 你认为闲聊需要的历史条数,介于10-300之间 } 2.如果用户需要使用上述功能之一,返回: @@ -94,13 +99,14 @@ class AIRouter: #### 示例: - 用户输入"提醒我下午3点开会" -> {"action_type": "function", "function_name": "reminder_set", "params": "下午3点开会"} - 用户输入"查看我的提醒" -> {"action_type": "function", "function_name": "reminder_list", "params": ""} - - 用户输入"你好" -> {"action_type": "chat"} + - 用户输入"你好" -> {"action_type": "chat", "history_messages": 15} - 用户输入"查一下Python教程" -> {"action_type": "function", "function_name": "perplexity_search", "params": "Python教程"} #### 格式注意事项: 1. action_type 只能是 "function" 或 "chat" 2. 只返回JSON,无需其他解释 3. function_name 必须完全匹配上述功能列表中的名称 + 4. 当 action_type 是 "chat" 时,必须提供整数 history_messages 字段,范围为10-300 """ return prompt @@ -139,7 +145,8 @@ class AIRouter: ai_response = chat_model.get_answer( user_input, wxid=ctx.get_receiver(), - system_prompt_override=system_prompt + system_prompt_override=system_prompt, + specific_max_history=ROUTING_HISTORY_LIMIT ) self.logger.debug(f"[AI路由器] AI响应: {ai_response}") @@ -165,7 +172,17 @@ class AIRouter: self.logger.warning(f"AI路由器:未知的功能名 - {function_name}") return False, None - self.logger.info(f"AI路由决策: {decision}") + if action_type == "chat": + history_value = decision.get("history_messages") + try: + history_value = int(history_value) + except (TypeError, ValueError): + history_value = CHAT_HISTORY_MIN + history_value = max(CHAT_HISTORY_MIN, min(CHAT_HISTORY_MAX, history_value)) + decision["history_messages"] = history_value + self.logger.info(f"AI路由决策: {decision}") + else: + self.logger.info(f"AI路由决策: {decision}") return True, decision except json.JSONDecodeError as e: @@ -228,7 +245,14 @@ class AIRouter: # 如果是聊天,返回False让后续处理器处理 if action_type == "chat": - self.logger.info("AI路由器:识别为聊天意图,交给聊天处理器") + history_limit = decision.get("history_messages", CHAT_HISTORY_MIN) + try: + history_limit = int(history_limit) + except (TypeError, ValueError): + history_limit = CHAT_HISTORY_MIN + history_limit = max(CHAT_HISTORY_MIN, min(CHAT_HISTORY_MAX, history_limit)) + setattr(ctx, 'specific_max_history', history_limit) + self.logger.info(f"AI路由器:识别为聊天意图,交给聊天处理器,使用历史条数 {history_limit}") return False # 如果是功能调用 diff --git a/commands/handlers.py b/commands/handlers.py index 6d150e8..d367e69 100644 --- a/commands/handlers.py +++ b/commands/handlers.py @@ -27,7 +27,19 @@ def handle_chitchat(ctx: 'MessageContext', match: Optional[Match]) -> bool: return False # 获取特定的历史消息数量限制 - specific_max_history = getattr(ctx, 'specific_max_history', None) + raw_specific_max_history = getattr(ctx, 'specific_max_history', None) + specific_max_history = None + if raw_specific_max_history is not None: + try: + specific_max_history = int(raw_specific_max_history) + except (TypeError, ValueError): + specific_max_history = None + if specific_max_history is not None: + if specific_max_history < 10: + specific_max_history = 10 + elif specific_max_history > 300: + specific_max_history = 300 + setattr(ctx, 'specific_max_history', specific_max_history) if ctx.logger and specific_max_history is not None: ctx.logger.debug(f"为 {ctx.get_receiver()} 使用特定历史限制: {specific_max_history}") @@ -781,4 +793,4 @@ def handle_delete_reminder(ctx: 'MessageContext', match: Optional[Match]) -> boo ctx.send_text(f"❌ 处理删除提醒时发生意外错误。", at_list) if ctx.logger: ctx.logger.error(f"handle_delete_reminder AI 部分顶层错误: {e}", exc_info=True) - return True \ No newline at end of file + return True