2025-11-05 02:04:34 +08:00
|
|
|
|
from langchain.agents import create_agent
|
2025-12-17 22:27:46 +08:00
|
|
|
|
from langchain.agents.middleware import ModelRetryMiddleware
|
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
|
2025-11-08 10:51:30 +08:00
|
|
|
|
from src.agents.common.middlewares import (
|
|
|
|
|
|
inject_attachment_context,
|
|
|
|
|
|
)
|
2025-12-30 19:01:45 +08:00
|
|
|
|
from src.agents.common.tools import get_kb_based_tools
|
2026-01-14 17:32:19 +08:00
|
|
|
|
from src.services.mcp_service import get_enabled_mcp_tools
|
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-12-30 19:19:49 +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-08-31 00:34:26 +08:00
|
|
|
|
self.context_schema = Context
|
2025-09-01 03:38:46 +08:00
|
|
|
|
|
2025-12-30 19:01:45 +08:00
|
|
|
|
async def get_tools(self, tools: list[str] = None, mcps=None, knowledges=None):
|
|
|
|
|
|
# 1. 基础工具 (从 context.tools 中筛选)
|
|
|
|
|
|
all_basic_tools = get_tools()
|
|
|
|
|
|
selected_tools = []
|
|
|
|
|
|
|
|
|
|
|
|
if tools:
|
|
|
|
|
|
# 创建工具映射表
|
|
|
|
|
|
tools_map = {t.name: t for t in all_basic_tools}
|
|
|
|
|
|
for tool_name in tools:
|
|
|
|
|
|
if tool_name in tools_map:
|
|
|
|
|
|
selected_tools.append(tools_map[tool_name])
|
|
|
|
|
|
|
|
|
|
|
|
# 2. 知识库工具
|
|
|
|
|
|
if knowledges:
|
|
|
|
|
|
kb_tools = get_kb_based_tools(db_names=knowledges)
|
|
|
|
|
|
selected_tools.extend(kb_tools)
|
|
|
|
|
|
|
2026-01-14 17:32:19 +08:00
|
|
|
|
# 3. MCP 工具(使用统一入口,自动过滤 disabled_tools)
|
2025-12-30 19:01:45 +08:00
|
|
|
|
if mcps:
|
|
|
|
|
|
for server_name in mcps:
|
2026-01-14 17:32:19 +08:00
|
|
|
|
mcp_tools = await get_enabled_mcp_tools(server_name)
|
2025-12-30 19:01:45 +08:00
|
|
|
|
selected_tools.extend(mcp_tools)
|
|
|
|
|
|
|
|
|
|
|
|
return selected_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-12-30 19:01:45 +08:00
|
|
|
|
# 获取上下文配置
|
|
|
|
|
|
context = self.context_schema.from_file(module_name=self.module_name)
|
2025-11-05 02:04:34 +08:00
|
|
|
|
|
2025-12-30 19:01:45 +08:00
|
|
|
|
# 使用 create_agent 创建智能体
|
2025-11-05 02:04:34 +08:00
|
|
|
|
graph = create_agent(
|
2025-12-30 19:01:45 +08:00
|
|
|
|
model=load_chat_model(context.model), # 使用 context 中的模型配置
|
|
|
|
|
|
tools=await self.get_tools(context.tools, context.mcps, context.knowledges),
|
2025-12-30 19:19:49 +08:00
|
|
|
|
system_prompt=context.system_prompt,
|
2025-11-05 02:04:34 +08:00
|
|
|
|
middleware=[
|
2025-12-30 19:01:45 +08:00
|
|
|
|
inject_attachment_context, # 附件上下文注入
|
2025-12-17 22:27:46 +08:00
|
|
|
|
ModelRetryMiddleware(), # 模型重试中间件
|
2025-11-05 02:04:34 +08:00
|
|
|
|
],
|
|
|
|
|
|
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()
|
2025-12-30 19:19:49 +08:00
|
|
|
|
# asyncio.run(main())
|