2025-10-31 14:16:07 +08:00
|
|
|
|
from langgraph.constants import END
|
2025-10-27 15:32:12 +08:00
|
|
|
|
from langgraph.graph import StateGraph
|
2025-08-31 00:34:26 +08:00
|
|
|
|
|
2025-10-27 15:32:12 +08:00
|
|
|
|
from src.agents.common.toolagent import ToolAgent
|
2025-08-31 00:34:26 +08:00
|
|
|
|
|
2025-10-27 15:32:12 +08:00
|
|
|
|
from .context import Context
|
2025-11-01 21:34:16 +08:00
|
|
|
|
from .state import State
|
2025-10-27 15:32:12 +08:00
|
|
|
|
from .tools import get_tools
|
2025-08-31 00:34:26 +08:00
|
|
|
|
|
2025-11-01 21:34:16 +08:00
|
|
|
|
|
2025-10-27 15:32:12 +08:00
|
|
|
|
def tools_branch_continue(state: State):
|
|
|
|
|
|
messages = state.messages
|
|
|
|
|
|
last_message = messages[-1]
|
|
|
|
|
|
# 如果无工具调用则结束
|
|
|
|
|
|
if not last_message.tool_calls:
|
|
|
|
|
|
return "end"
|
|
|
|
|
|
else:
|
|
|
|
|
|
return "continue"
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-10-25 20:01:23 +08:00
|
|
|
|
|
2025-10-27 15:32:12 +08:00
|
|
|
|
class ReActAgent(ToolAgent):
|
2025-10-31 14:16:07 +08:00
|
|
|
|
name = "ReActAgent"
|
|
|
|
|
|
description = "符合 ReAct 范式的智能体,可以通过调用工具来完成复杂任务。"
|
2025-10-25 20:01:23 +08:00
|
|
|
|
|
2025-11-01 21:34:16 +08:00
|
|
|
|
"""
|
2025-10-27 15:32:12 +08:00
|
|
|
|
提示词示例:
|
|
|
|
|
|
你是一个智能体助手
|
2025-03-29 19:13:56 +08:00
|
|
|
|
|
2025-10-27 15:32:12 +08:00
|
|
|
|
你的所有需要计算的工作不能自己计算,只能通过调用加密计算机工具进行,你只能通过调用该工具的方式进行计算才能得到正确答案。计算顺序是正常的。
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-10-27 15:32:12 +08:00
|
|
|
|
例如 2+3+2,你必须调用两次加密计算机工具进行运算,最终结果为9。
|
2025-11-01 21:34:16 +08:00
|
|
|
|
"""
|
2025-10-25 20:01:23 +08:00
|
|
|
|
|
2025-10-27 15:32:12 +08:00
|
|
|
|
# TODO:[已完成] React智能体
|
|
|
|
|
|
'''
|
|
|
|
|
|
提示词示例:
|
|
|
|
|
|
你是一个智能体助手
|
2025-03-29 19:13:56 +08:00
|
|
|
|
|
2025-10-27 15:32:12 +08:00
|
|
|
|
你的所有需要计算的工作不能自己计算,只能通过调用加密计算机工具进行,你只能通过调用该工具的方式进行计算才能得到正确答案。计算顺序是正常的。
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-10-27 15:32:12 +08:00
|
|
|
|
例如 2+3+2,你必须调用两次加密计算机工具进行运算,最终结果为9。
|
|
|
|
|
|
'''
|
2025-03-29 19:13:56 +08:00
|
|
|
|
|
2025-08-31 00:34:26 +08:00
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
|
super().__init__(**kwargs)
|
2025-10-27 15:32:12 +08:00
|
|
|
|
self.graph = None
|
|
|
|
|
|
self.checkpointer = None
|
|
|
|
|
|
self.context_schema = Context
|
|
|
|
|
|
self.agent_tools = None
|
2025-08-31 00:34:26 +08:00
|
|
|
|
|
2025-10-25 20:01:23 +08:00
|
|
|
|
def get_tools(self):
|
2025-10-27 15:32:12 +08:00
|
|
|
|
return get_tools()
|
2025-10-25 20:01:23 +08:00
|
|
|
|
|
2025-05-20 22:21:51 +08:00
|
|
|
|
async def get_graph(self, **kwargs):
|
2025-10-27 15:32:12 +08:00
|
|
|
|
# 创建 ReActAgent
|
|
|
|
|
|
"""构建图"""
|
2025-08-31 00:34:26 +08:00
|
|
|
|
if self.graph:
|
|
|
|
|
|
return self.graph
|
|
|
|
|
|
|
2025-10-27 15:32:12 +08:00
|
|
|
|
builder = StateGraph(State, context_schema=self.context_schema)
|
|
|
|
|
|
builder.add_node("agent", self.llm_call)
|
|
|
|
|
|
builder.add_node("tools", self.dynamic_tools_node)
|
|
|
|
|
|
builder.set_entry_point("agent")
|
|
|
|
|
|
# 添加条件边:agent 决定是否调用工具继续还是结束对话
|
|
|
|
|
|
builder.add_conditional_edges(
|
|
|
|
|
|
"agent",
|
|
|
|
|
|
tools_branch_continue,
|
|
|
|
|
|
{
|
|
|
|
|
|
"continue": "tools", # 调用工具
|
|
|
|
|
|
"end": END, # 结束对话
|
|
|
|
|
|
},
|
2025-10-25 20:01:23 +08:00
|
|
|
|
)
|
2025-10-27 15:32:12 +08:00
|
|
|
|
builder.add_edge("tools", "agent")
|
|
|
|
|
|
self.checkpointer = await self._get_checkpointer()
|
|
|
|
|
|
graph = builder.compile(checkpointer=self.checkpointer, name=self.name)
|
2025-08-31 00:34:26 +08:00
|
|
|
|
self.graph = graph
|
2025-07-26 03:36:54 +08:00
|
|
|
|
return graph
|
2025-10-27 15:32:12 +08:00
|
|
|
|
|