2025-11-05 02:04:34 +08:00
|
|
|
|
from langchain.agents import create_agent
|
2025-03-24 19:07:51 +08:00
|
|
|
|
|
2025-11-05 02:04:34 +08:00
|
|
|
|
from src.agents.common import BaseAgent, load_chat_model
|
|
|
|
|
|
from src.agents.common.mcp import MCP_SERVERS
|
2025-11-08 10:51:30 +08:00
|
|
|
|
from src.agents.common.middlewares import (
|
|
|
|
|
|
DynamicToolMiddleware,
|
|
|
|
|
|
context_aware_prompt,
|
|
|
|
|
|
context_based_model,
|
|
|
|
|
|
inject_attachment_context,
|
|
|
|
|
|
)
|
2025-11-05 02:04:34 +08:00
|
|
|
|
from src.agents.common.subagents import calc_agent_tool
|
2025-09-01 03:38:46 +08:00
|
|
|
|
|
2025-08-31 00:34:26 +08:00
|
|
|
|
from .context import Context
|
|
|
|
|
|
from .tools import get_tools
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-03-24 19:07:51 +08:00
|
|
|
|
class ChatbotAgent(BaseAgent):
|
2025-08-11 21:29:33 +08:00
|
|
|
|
name = "智能体助手"
|
2025-04-05 17:27:52 +08:00
|
|
|
|
description = "基础的对话机器人,可以回答问题,默认不使用任何工具,可在配置中启用需要的工具。"
|
2025-11-08 10:51:30 +08:00
|
|
|
|
capabilities = ["file_upload"] # 支持文件上传功能
|
2025-03-24 23:00:14 +08:00
|
|
|
|
|
2025-03-28 11:40:46 +08:00
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
|
super().__init__(**kwargs)
|
2025-05-15 22:34:32 +08:00
|
|
|
|
self.graph = None
|
2025-10-12 16:04:46 +08:00
|
|
|
|
self.checkpointer = None
|
2025-08-31 00:34:26 +08:00
|
|
|
|
self.context_schema = Context
|
2025-09-01 03:38:46 +08:00
|
|
|
|
|
|
|
|
|
|
def get_tools(self):
|
2025-11-05 02:04:34 +08:00
|
|
|
|
"""返回基本工具"""
|
|
|
|
|
|
base_tools = get_tools()
|
|
|
|
|
|
base_tools.append(calc_agent_tool)
|
|
|
|
|
|
return base_tools
|
2025-03-24 23:00:14 +08:00
|
|
|
|
|
2025-08-31 00:34:26 +08:00
|
|
|
|
async def get_graph(self, **kwargs):
|
2025-03-24 23:00:14 +08:00
|
|
|
|
"""构建图"""
|
2025-10-11 21:10:39 +08:00
|
|
|
|
if self.graph:
|
|
|
|
|
|
return self.graph
|
2025-07-26 03:36:54 +08:00
|
|
|
|
|
2025-11-05 02:04:34 +08:00
|
|
|
|
# 创建动态工具中间件实例,并传入所有可用的 MCP 服务器列表
|
|
|
|
|
|
dynamic_tool_middleware = DynamicToolMiddleware(
|
|
|
|
|
|
base_tools=self.get_tools(), mcp_servers=list(MCP_SERVERS.keys())
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# 预加载所有 MCP 工具并注册到 middleware.tools
|
|
|
|
|
|
await dynamic_tool_middleware.initialize_mcp_tools()
|
|
|
|
|
|
|
|
|
|
|
|
# 使用 create_agent 创建智能体,并传入 middleware
|
|
|
|
|
|
graph = create_agent(
|
|
|
|
|
|
model=load_chat_model("siliconflow/Qwen/Qwen3-235B-A22B-Instruct-2507"), # 默认模型,会被 middleware 覆盖
|
|
|
|
|
|
tools=get_tools(), # 注册基础工具
|
|
|
|
|
|
middleware=[
|
|
|
|
|
|
context_aware_prompt, # 动态系统提示词
|
2025-11-08 10:51:30 +08:00
|
|
|
|
inject_attachment_context, # 附件上下文注入(LangChain 标准中间件)
|
2025-11-05 02:04:34 +08:00
|
|
|
|
context_based_model, # 动态模型选择
|
|
|
|
|
|
dynamic_tool_middleware, # 动态工具选择(支持 MCP 工具注册)
|
|
|
|
|
|
],
|
|
|
|
|
|
checkpointer=await self._get_checkpointer(),
|
2025-03-25 05:40:07 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-10-11 21:10:39 +08:00
|
|
|
|
self.graph = graph
|
|
|
|
|
|
return graph
|
2025-03-24 23:00:14 +08:00
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-03-24 23:00:14 +08:00
|
|
|
|
def main():
|
2025-10-12 16:04:46 +08:00
|
|
|
|
pass
|
2025-03-24 19:07:51 +08:00
|
|
|
|
|
2025-10-12 23:45:17 +08:00
|
|
|
|
|
2025-03-24 23:00:14 +08:00
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
main()
|
|
|
|
|
|
# asyncio.run(main())
|