- 调整 Python 文件 import 顺序,遵循标准库→第三方库→项目模块顺序 - 统一字符串引号风格为双引号 - 优化 Vue 组件 console.log 格式 - 压缩 import 语句为单行 - 清理不必要的空行
58 lines
1.9 KiB
Python
58 lines
1.9 KiB
Python
from deepagents.backends import StateBackend
|
||
from deepagents.middleware.filesystem import FilesystemMiddleware
|
||
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.middlewares import (
|
||
RuntimeConfigMiddleware,
|
||
save_attachments_to_fs,
|
||
)
|
||
from src.services.mcp_service import get_tools_from_all_servers
|
||
|
||
|
||
def _create_fs_backend(rt):
|
||
"""创建文件存储后端"""
|
||
return StateBackend(rt)
|
||
|
||
|
||
class ChatbotAgent(BaseAgent):
|
||
name = "智能体助手"
|
||
description = "基础的对话机器人,可以回答问题,可在配置中启用需要的工具。"
|
||
capabilities = ["file_upload"] # 支持文件上传功能
|
||
|
||
def __init__(self, **kwargs):
|
||
super().__init__(**kwargs)
|
||
|
||
async def get_graph(self, **kwargs):
|
||
"""构建图"""
|
||
context = self.context_schema()
|
||
all_mcp_tools = (
|
||
await get_tools_from_all_servers()
|
||
) # 因为异步加载,无法放在 RuntimeConfigMiddleware 的 __init__ 中
|
||
|
||
# 使用 create_agent 创建智能体
|
||
# 注意:tools 参数由 RuntimeConfigMiddleware 在 wrap_model_call 中动态设置
|
||
graph = create_agent(
|
||
model=load_chat_model(context.model),
|
||
system_prompt=context.system_prompt,
|
||
middleware=[
|
||
save_attachments_to_fs, # 附件注入提示词
|
||
FilesystemMiddleware(backend=_create_fs_backend), # 文件系统后端
|
||
RuntimeConfigMiddleware(extra_tools=all_mcp_tools), # 运行时配置应用(模型/工具/知识库/MCP/提示词)
|
||
ModelRetryMiddleware(), # 模型重试中间件
|
||
],
|
||
checkpointer=await self._get_checkpointer(),
|
||
)
|
||
|
||
return graph
|
||
|
||
|
||
def main():
|
||
pass
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|
||
# asyncio.run(main())
|