2025-10-25 22:49:20 +08:00
|
|
|
from langchain.agents import create_agent
|
|
|
|
|
|
2026-01-14 17:32:19 +08:00
|
|
|
from src.agents.common import BaseAgent, load_chat_model
|
2025-10-25 22:49:20 +08:00
|
|
|
from src.agents.common.toolkits.mysql import get_mysql_tools
|
2026-01-14 17:32:19 +08:00
|
|
|
from src.services.mcp_service import get_mcp_tools
|
2025-10-25 22:49:20 +08:00
|
|
|
from src.utils import logger
|
|
|
|
|
|
2025-11-01 21:34:16 +08:00
|
|
|
|
2025-10-25 22:49:20 +08:00
|
|
|
class SqlReporterAgent(BaseAgent):
|
2025-11-02 01:15:36 +08:00
|
|
|
name = "数据库报表助手"
|
2025-10-25 22:49:20 +08:00
|
|
|
description = "一个能够生成 SQL 查询报告的智能体助手。同时调用 Charts MCP 生成图表。"
|
|
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
|
|
async def get_tools(self):
|
|
|
|
|
mysql_tools = get_mysql_tools()
|
2026-01-14 17:32:19 +08:00
|
|
|
chart_tools = await get_mcp_tools("mcp-server-chart")
|
|
|
|
|
return mysql_tools + chart_tools
|
2025-10-25 22:49:20 +08:00
|
|
|
|
|
|
|
|
async def get_graph(self, **kwargs):
|
|
|
|
|
if self.graph:
|
|
|
|
|
return self.graph
|
|
|
|
|
|
2025-12-30 19:19:49 +08:00
|
|
|
context = self.context_schema.from_file(module_name=self.module_name)
|
|
|
|
|
|
2025-10-25 22:49:20 +08:00
|
|
|
# 创建 SqlReporterAgent
|
|
|
|
|
graph = create_agent(
|
2026-01-02 19:56:55 +08:00
|
|
|
model=load_chat_model(context.model), # 使用 context 中的模型配置
|
2025-12-30 19:19:49 +08:00
|
|
|
system_prompt=context.system_prompt,
|
2025-10-25 22:49:20 +08:00
|
|
|
tools=await self.get_tools(),
|
|
|
|
|
checkpointer=await self._get_checkpointer(),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.graph = graph
|
|
|
|
|
logger.info("SqlReporterAgent 构建成功")
|
2025-10-31 14:16:07 +08:00
|
|
|
return graph
|