feat(agents): 增加模型重试中间件并调整递归限制

将递归限制从100增加到300以支持更深的调用链
添加ModelRetryMiddleware到聊天机器人中间件链
优化模型加载逻辑,支持openai和deepseek官方初始化
This commit is contained in:
Wenjie Zhang 2025-12-17 22:27:46 +08:00
parent 425c5b127c
commit e62ec33e4b
3 changed files with 12 additions and 3 deletions

View File

@ -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(),
)

View File

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

View File

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