refactor(agents): 移除reload_graph能力并简化agent初始化逻辑
统一agent初始化流程,直接从context获取system_prompt 清理不再使用的middleware导入和配置
This commit is contained in:
parent
8087ef4b00
commit
22fc138ab5
@ -865,7 +865,7 @@ async def resume_agent_chat(
|
||||
async def save_agent_config(
|
||||
agent_id: str,
|
||||
config: dict = Body(...),
|
||||
reload_graph: bool = Query(False),
|
||||
reload_graph: bool = Query(True),
|
||||
current_user: User = Depends(get_required_user),
|
||||
):
|
||||
"""保存智能体配置到YAML文件(需要登录)"""
|
||||
|
||||
@ -36,6 +36,7 @@ class Context(BaseContext):
|
||||
"options": lambda: list(MCP_SERVERS.keys()),
|
||||
"description": (
|
||||
"MCP服务器列表,建议使用支持 SSE 的 MCP 服务器,"
|
||||
"如果需要使用 uvx 或 npx 运行的服务器,也请在项目外部启动 MCP 服务器,并在项目中配置 MCP 服务器。"),
|
||||
"如果需要使用 uvx 或 npx 运行的服务器,也请在项目外部启动 MCP 服务器,并在项目中配置 MCP 服务器。"
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
@ -4,8 +4,6 @@ from langchain.agents.middleware import ModelRetryMiddleware
|
||||
from src.agents.common import BaseAgent, load_chat_model
|
||||
from src.agents.common.mcp import get_mcp_tools
|
||||
from src.agents.common.middlewares import (
|
||||
context_aware_prompt,
|
||||
context_based_model,
|
||||
inject_attachment_context,
|
||||
)
|
||||
from src.agents.common.tools import get_kb_based_tools
|
||||
@ -17,7 +15,7 @@ from .tools import get_tools
|
||||
class ChatbotAgent(BaseAgent):
|
||||
name = "智能体助手"
|
||||
description = "基础的对话机器人,可以回答问题,默认不使用任何工具,可在配置中启用需要的工具。"
|
||||
capabilities = ["file_upload", "reload_graph"] # 支持文件上传功能和重载图
|
||||
capabilities = ["file_upload"] # 支持文件上传功能
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
@ -26,7 +24,6 @@ class ChatbotAgent(BaseAgent):
|
||||
self.context_schema = Context
|
||||
|
||||
async def get_tools(self, tools: list[str] = None, mcps=None, knowledges=None):
|
||||
|
||||
# 1. 基础工具 (从 context.tools 中筛选)
|
||||
all_basic_tools = get_tools()
|
||||
selected_tools = []
|
||||
@ -63,10 +60,9 @@ class ChatbotAgent(BaseAgent):
|
||||
graph = create_agent(
|
||||
model=load_chat_model(context.model), # 使用 context 中的模型配置
|
||||
tools=await self.get_tools(context.tools, context.mcps, context.knowledges),
|
||||
system_prompt=context.system_prompt,
|
||||
middleware=[
|
||||
context_aware_prompt, # 动态系统提示词
|
||||
inject_attachment_context, # 附件上下文注入
|
||||
context_based_model, # 动态模型选择
|
||||
ModelRetryMiddleware(), # 模型重试中间件
|
||||
],
|
||||
checkpointer=await self._get_checkpointer(),
|
||||
@ -82,4 +78,4 @@ def main():
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
# asyncio.run(main())
|
||||
# asyncio.run(main())
|
||||
|
||||
@ -49,7 +49,7 @@ class BaseContext:
|
||||
metadata={
|
||||
"name": "智能体模型",
|
||||
"options": [],
|
||||
"description": "智能体的驱动模型,建议选择 Agent 能力较强的模型,不建议使用小参数模型。"
|
||||
"description": "智能体的驱动模型,建议选择 Agent 能力较强的模型,不建议使用小参数模型。",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@ from langchain.agents import create_agent
|
||||
from langchain.agents.middleware import ModelRequest, SummarizationMiddleware, TodoListMiddleware, dynamic_prompt
|
||||
|
||||
from src.agents.common import BaseAgent, load_chat_model
|
||||
from src.agents.common.middlewares import context_based_model, inject_attachment_context
|
||||
from src.agents.common.middlewares import inject_attachment_context
|
||||
from src.agents.common.tools import search
|
||||
|
||||
from .context import DeepContext
|
||||
@ -64,7 +64,6 @@ class DeepAgent(BaseAgent):
|
||||
"file_upload",
|
||||
"todo",
|
||||
"files",
|
||||
"reload_graph",
|
||||
]
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
@ -93,8 +92,8 @@ class DeepAgent(BaseAgent):
|
||||
graph = create_agent(
|
||||
model=model,
|
||||
tools=tools,
|
||||
system_prompt=context.system_prompt,
|
||||
middleware=[
|
||||
context_based_model, # 动态模型选择
|
||||
context_aware_prompt, # 动态系统提示词
|
||||
inject_attachment_context, # 附件上下文注入
|
||||
TodoListMiddleware(),
|
||||
@ -104,8 +103,8 @@ class DeepAgent(BaseAgent):
|
||||
default_tools=tools,
|
||||
subagents=[critique_sub_agent, research_sub_agent],
|
||||
default_middleware=[
|
||||
TodoListMiddleware(),
|
||||
FilesystemMiddleware(),
|
||||
TodoListMiddleware(), # 子智能体也有 todo 列表
|
||||
FilesystemMiddleware(), # 当前的两个文件系统是隔离的
|
||||
SummarizationMiddleware(
|
||||
model=sub_model,
|
||||
trigger=("tokens", 110000),
|
||||
|
||||
@ -2,7 +2,6 @@ from langchain.agents import create_agent
|
||||
|
||||
from src import config
|
||||
from src.agents.common import BaseAgent, get_buildin_tools, load_chat_model
|
||||
from src.agents.common.middlewares import context_aware_prompt, context_based_model
|
||||
|
||||
|
||||
class MiniAgent(BaseAgent):
|
||||
@ -19,11 +18,13 @@ class MiniAgent(BaseAgent):
|
||||
if self.graph:
|
||||
return self.graph
|
||||
|
||||
context = self.context_schema.from_file(module_name=self.module_name)
|
||||
|
||||
# 创建 MiniAgent
|
||||
graph = create_agent(
|
||||
model=load_chat_model(config.default_model), # 实际会被覆盖
|
||||
model=load_chat_model(config.default_model),
|
||||
system_prompt=context.system_prompt,
|
||||
tools=self.get_tools(),
|
||||
middleware=[context_aware_prompt, context_based_model],
|
||||
checkpointer=await self._get_checkpointer(),
|
||||
)
|
||||
|
||||
|
||||
@ -2,7 +2,6 @@ from langchain.agents import create_agent
|
||||
|
||||
from src import config
|
||||
from src.agents.common import BaseAgent, get_mcp_tools, load_chat_model
|
||||
from src.agents.common.middlewares import context_aware_prompt, context_based_model
|
||||
from src.agents.common.toolkits.mysql import get_mysql_tools
|
||||
from src.utils import logger
|
||||
|
||||
@ -25,11 +24,13 @@ class SqlReporterAgent(BaseAgent):
|
||||
if self.graph:
|
||||
return self.graph
|
||||
|
||||
context = self.context_schema.from_file(module_name=self.module_name)
|
||||
|
||||
# 创建 SqlReporterAgent
|
||||
graph = create_agent(
|
||||
model=load_chat_model(config.default_model), # 默认模型,会被 middleware 覆盖
|
||||
system_prompt=context.system_prompt,
|
||||
tools=await self.get_tools(),
|
||||
middleware=[context_aware_prompt, context_based_model],
|
||||
checkpointer=await self._get_checkpointer(),
|
||||
)
|
||||
|
||||
|
||||
@ -220,7 +220,7 @@
|
||||
<div class="sidebar-footer" v-if="!isEmptyConfig">
|
||||
<div class="form-actions">
|
||||
<a-button @click="saveConfig" class="save-btn" :class="{'changed': agentStore.hasConfigChanges}">
|
||||
{{ needsReload ? '保存配置并重新加载' : '保存配置' }}
|
||||
保存配置并重新加载
|
||||
</a-button>
|
||||
</div>
|
||||
</div>
|
||||
@ -334,12 +334,6 @@ const isEmptyConfig = computed(() => {
|
||||
return !selectedAgentId.value || Object.keys(configurableItems.value).length === 0;
|
||||
});
|
||||
|
||||
const needsReload = computed(() => {
|
||||
return selectedAgent.value &&
|
||||
selectedAgent.value.capabilities &&
|
||||
selectedAgent.value.capabilities.includes('reload_graph');
|
||||
});
|
||||
|
||||
const filteredTools = computed(() => {
|
||||
const toolsList = availableTools.value ? Object.values(availableTools.value) : [];
|
||||
if (!toolsSearchText.value) {
|
||||
@ -536,12 +530,7 @@ const saveConfig = async () => {
|
||||
message.info('检测到无效配置项,已自动过滤');
|
||||
}
|
||||
|
||||
const options = {};
|
||||
if (selectedAgent.value && selectedAgent.value.capabilities && selectedAgent.value.capabilities.includes('reload_graph')) {
|
||||
options.reload_graph = true;
|
||||
}
|
||||
|
||||
await agentStore.saveAgentConfig(options);
|
||||
await agentStore.saveAgentConfig();
|
||||
message.success('配置已保存到服务器');
|
||||
} catch (error) {
|
||||
console.error('保存配置到服务器出错:', error);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user