refactor: 删除重复的智能体
This commit is contained in:
parent
0b0dbe6a69
commit
8987cf5d8a
@ -1,3 +0,0 @@
|
|||||||
from .graph import SampleMultiAgent
|
|
||||||
|
|
||||||
__all__ = ["SampleMultiAgent"]
|
|
||||||
@ -1,25 +0,0 @@
|
|||||||
from dataclasses import dataclass, field
|
|
||||||
from typing import Annotated
|
|
||||||
|
|
||||||
from src.agents.common.context import BaseContext
|
|
||||||
from src.agents.common.mcp import MCP_SERVERS
|
|
||||||
from src.agents.common.tools import gen_tool_info
|
|
||||||
|
|
||||||
from .tools import get_tools
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(kw_only=True)
|
|
||||||
class Context(BaseContext):
|
|
||||||
tools: Annotated[list[dict], {"__template_metadata__": {"kind": "tools"}}] = field(
|
|
||||||
default_factory=list,
|
|
||||||
metadata={
|
|
||||||
"name": "工具",
|
|
||||||
"options": gen_tool_info(get_tools()), # 这里的选择是所有的工具
|
|
||||||
"description": "工具列表",
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
mcps: list[str] = field(
|
|
||||||
default_factory=list,
|
|
||||||
metadata={"name": "MCP服务器", "options": list(MCP_SERVERS.keys()), "description": "MCP服务器列表"},
|
|
||||||
)
|
|
||||||
@ -1,61 +0,0 @@
|
|||||||
from langgraph.graph import END, START, StateGraph
|
|
||||||
from langgraph.prebuilt import tools_condition
|
|
||||||
|
|
||||||
from src.agents.common.toolagent import ToolAgent
|
|
||||||
|
|
||||||
from .context import Context
|
|
||||||
from .state import State
|
|
||||||
from .tools import get_tools
|
|
||||||
|
|
||||||
|
|
||||||
class SampleMultiAgent(ToolAgent):
|
|
||||||
name = "MultiAgent智能体"
|
|
||||||
description = "Supervisor智能体,具有调用其他子智能体的能力(在工具中添加)"
|
|
||||||
|
|
||||||
# TODO[已完成]: 通过将其他agent封装为工具的方式添加了多智能体调度
|
|
||||||
'''
|
|
||||||
你是一个多智能体核心,通过多智能体调用的方式帮助用户完成一系列任务:
|
|
||||||
|
|
||||||
1.当你需要知识库问答功能时,请调用对话聊天智能体实现
|
|
||||||
2.当你需要加密计算的时候,请调用加密计算智能体实现
|
|
||||||
'''
|
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
|
||||||
super().__init__(**kwargs)
|
|
||||||
self.graph = None
|
|
||||||
self.checkpointer = None
|
|
||||||
self.context_schema = Context
|
|
||||||
self.agent_tools = None
|
|
||||||
|
|
||||||
def get_tools(self):
|
|
||||||
return get_tools()
|
|
||||||
|
|
||||||
async def get_graph(self, **kwargs):
|
|
||||||
"""构建图"""
|
|
||||||
if self.graph:
|
|
||||||
return self.graph
|
|
||||||
|
|
||||||
builder = StateGraph(State, context_schema=self.context_schema)
|
|
||||||
builder.add_node("chatbot", self.llm_call)
|
|
||||||
builder.add_node("tools", self.dynamic_tools_node)
|
|
||||||
builder.add_edge(START, "chatbot")
|
|
||||||
builder.add_conditional_edges(
|
|
||||||
"chatbot",
|
|
||||||
tools_condition,
|
|
||||||
)
|
|
||||||
builder.add_edge("tools", "chatbot")
|
|
||||||
builder.add_edge("chatbot", END)
|
|
||||||
|
|
||||||
self.checkpointer = await self._get_checkpointer()
|
|
||||||
graph = builder.compile(checkpointer=self.checkpointer, name=self.name)
|
|
||||||
self.graph = graph
|
|
||||||
return graph
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
# asyncio.run(main())
|
|
||||||
@ -1,22 +0,0 @@
|
|||||||
"""Define the state structures for the agent."""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from collections.abc import Sequence
|
|
||||||
from dataclasses import dataclass, field
|
|
||||||
from typing import Annotated
|
|
||||||
|
|
||||||
from langchain.messages import AnyMessage
|
|
||||||
from langgraph.graph import add_messages
|
|
||||||
|
|
||||||
from src.agents.common.state import BaseState
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class State(BaseState):
|
|
||||||
"""Defines the input state for the agent, representing a narrower interface to the outside world.
|
|
||||||
|
|
||||||
This class is used to define the initial state and structure of incoming data.
|
|
||||||
"""
|
|
||||||
|
|
||||||
messages: Annotated[Sequence[AnyMessage], add_messages] = field(default_factory=list)
|
|
||||||
@ -1,76 +0,0 @@
|
|||||||
import os
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from langchain.tools import tool
|
|
||||||
from langchain_core.runnables import RunnableConfig
|
|
||||||
|
|
||||||
from src.agents import agent_manager
|
|
||||||
from src.agents.common.toolkits.mysql import get_mysql_tools
|
|
||||||
from src.agents.common.tools import get_buildin_tools
|
|
||||||
from src.utils import logger
|
|
||||||
|
|
||||||
# TODO[修改建议]:能不能通过前端直接指定子智能体?
|
|
||||||
# 调用子智能体后的日志是输出到tool_calls的
|
|
||||||
@tool(name_or_callable="对话聊天智能体", description="调用指定智能体进行对话聊天的功能")
|
|
||||||
async def call_chatbot(query: str, config: RunnableConfig) -> str:
|
|
||||||
"""
|
|
||||||
调用指定chatbot智能体进行对话聊天的功能
|
|
||||||
|
|
||||||
Args:
|
|
||||||
query: 根据需要构造的提问
|
|
||||||
config: LangGraph运行时配置(自动注入)
|
|
||||||
Returns:
|
|
||||||
str: 最终的回答结果
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
input = [{"role": "user", "content": query}]
|
|
||||||
chatbot = agent_manager.get_agent("ChatbotAgent")
|
|
||||||
configurable = config.get("configurable",{})
|
|
||||||
input_context = {
|
|
||||||
"thread_id":configurable.get("thread_id"),
|
|
||||||
"user_id": configurable.get("user_id"),
|
|
||||||
}
|
|
||||||
message = await chatbot.invoke_messages(input,input_context=input_context)
|
|
||||||
# 直接获取最后一个消息的内容
|
|
||||||
final_answer = message.get('messages', [])[-1].content
|
|
||||||
logger.info(f"ChatbotAgent: {final_answer}")
|
|
||||||
return final_answer
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"CallAgent error: {e}")
|
|
||||||
raise
|
|
||||||
|
|
||||||
@tool(name_or_callable="加密计算智能体", description="调用指定智能体进行加密计算的功能")
|
|
||||||
async def call_react_agent(query: str, config: RunnableConfig) -> str:
|
|
||||||
"""
|
|
||||||
调用指定智能体进行加密计算的功能
|
|
||||||
|
|
||||||
Args:
|
|
||||||
query: 根据需要构造的提问
|
|
||||||
config: LangGraph运行时配置(自动注入)
|
|
||||||
Returns:
|
|
||||||
str: 最终的回答结果
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
input = [{"role": "user", "content": query}]
|
|
||||||
chatbot = agent_manager.get_agent("ReActAgent")
|
|
||||||
configurable = config.get("configurable",{})
|
|
||||||
input_context = {
|
|
||||||
"thread_id":configurable.get("thread_id"),
|
|
||||||
"user_id": configurable.get("user_id"),
|
|
||||||
}
|
|
||||||
message = await chatbot.invoke_messages(input,input_context=input_context)
|
|
||||||
# 直接获取最后一个消息的内容
|
|
||||||
final_answer = message.get('messages', [])[-1].content
|
|
||||||
logger.info(f"ReActAgent: {final_answer}")
|
|
||||||
return final_answer
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"CallAgent error: {e}")
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
|
||||||
def get_tools() -> list[Any]:
|
|
||||||
"""获取所有可运行的工具(给大模型使用)"""
|
|
||||||
tools = get_buildin_tools()
|
|
||||||
tools.append(call_chatbot)
|
|
||||||
tools.append(call_react_agent)
|
|
||||||
return tools
|
|
||||||
Loading…
Reference in New Issue
Block a user