diff --git a/src/agents/chatbot/graph.py b/src/agents/chatbot/graph.py index fa9296ca..080e81d7 100644 --- a/src/agents/chatbot/graph.py +++ b/src/agents/chatbot/graph.py @@ -1,4 +1,5 @@ from langchain.agents import create_agent +from langchain.agents.middleware import ModelRetryMiddleware from src.agents.common import BaseAgent, load_chat_model from src.agents.common.mcp import MCP_SERVERS @@ -53,6 +54,7 @@ class ChatbotAgent(BaseAgent): inject_attachment_context, # 附件上下文注入(LangChain 标准中间件) context_based_model, # 动态模型选择 dynamic_tool_middleware, # 动态工具选择(支持 MCP 工具注册) + ModelRetryMiddleware(), # 模型重试中间件 ], checkpointer=await self._get_checkpointer(), ) diff --git a/src/agents/common/base.py b/src/agents/common/base.py index d5f5796a..d67fe8c2 100644 --- a/src/agents/common/base.py +++ b/src/agents/common/base.py @@ -74,7 +74,7 @@ class BaseAgent: # 从 input_context 中提取 attachments(如果有) attachments = (input_context or {}).get("attachments", []) - input_config = {"configurable": input_context, "recursion_limit": 100} + input_config = {"configurable": input_context, "recursion_limit": 300} async for msg, metadata in graph.astream( {"messages": messages, "attachments": attachments}, diff --git a/src/agents/common/models.py b/src/agents/common/models.py index a860e34a..b40b9946 100644 --- a/src/agents/common/models.py +++ b/src/agents/common/models.py @@ -1,11 +1,12 @@ import os import traceback -from langchain.chat_models import BaseChatModel +from langchain.chat_models import BaseChatModel, init_chat_model from pydantic import SecretStr from src import config from src.utils import get_docker_safe_url +from src.utils.logging_config import logger def load_chat_model(fully_specified_name: str, **kwargs) -> BaseChatModel: @@ -26,7 +27,13 @@ def load_chat_model(fully_specified_name: str, **kwargs) -> BaseChatModel: base_url = get_docker_safe_url(model_info.base_url) - if provider in ["deepseek", "dashscope"]: + if provider in ["openai", "deepseek"]: + model_spec = f"{provider}:{model}" + logger.debug(f"[offical] Loading model {model_spec} with kwargs {kwargs}") + return init_chat_model(model_spec, **kwargs) + + + elif provider in ["dashscope"]: from langchain_deepseek import ChatDeepSeek return ChatDeepSeek(