多智能体系统¶
智能体是一个使用大型语言模型(LLM)来决定应用程序控制流的系统。随着您开发这些系统,它们可能会随着时间变得更加复杂,从而难以管理和扩展。例如,您可能会遇到以下问题:
- 智能体拥有过多的工具,导致它对下一步调用哪个工具做出糟糕的决策
- 上下文变得过于复杂,单个智能体难以跟踪
- 系统需要多个专业领域(例如,规划师、研究员、数学专家等)
为了解决这些问题,您可以考虑将应用程序拆分为多个更小、独立的智能体,并将它们组合成一个多智能体系统。这些独立的智能体可以像一个提示和一次LLM调用那样简单,也可以像一个ReAct智能体那样复杂(甚至更复杂!)。
使用多智能体系统的主要优势是:
- 模块化:独立的智能体使得开发、测试和维护智能体系统变得更容易。
- 专业化:您可以创建专注于特定领域的专家智能体,这有助于提高整个系统的性能。
- 控制:您可以明确控制智能体如何通信(而不是依赖函数调用)。
多智能体架构¶
在多智能体系统中连接智能体有几种方式:
- 网络:每个智能体可以与其他所有智能体通信。任何智能体都可以决定下一步调用哪个智能体。
- 主管:每个智能体与一个主管智能体通信。主管智能体决定下一步应该调用哪个智能体。
- 分层:您可以定义一个由多个主管组成的多智能体系统。这是主管架构的泛化,允许更复杂的控制流。
- 自定义多智能体工作流:每个智能体只与一部分智能体通信。流的部分是确定性的,只有某些智能体可以决定下一步调用哪个智能体。
交接¶
在多智能体架构中,智能体可以表示为图节点。每个智能体节点执行其步骤,并决定是结束执行还是路由到另一个智能体,包括可能路由到自身(例如,循环运行)。多智能体交互中的常见模式是交接,即一个智能体将控制权交给另一个智能体。交接允许您指定:
- 目的地:要导航到的目标智能体(例如,要去的节点名称)
- 负载:要传递给该智能体的信息(例如,状态更新)
要在 LangGraph 中实现交接,智能体节点可以返回Command
对象,该对象允许您结合控制流和状态更新。
const agent = (state: typeof StateAnnotation.State) => {
const goto = getNextAgent(...) // 'agent' / 'another_agent'
return new Command({
// Specify which agent to call next
goto: goto,
// Update the graph state
update: {
foo: "bar",
}
});
};
在更复杂的场景中,如果每个智能体节点本身都是一个图(即子图),则某个智能体子图中的节点可能希望导航到不同的智能体。例如,如果您有两个智能体,alice
和 bob
(父图中的子图节点),并且 alice
需要导航到 bob
,您可以在 Command
对象中设置 graph=Command.PARENT
。
const some_node_inside_alice = (state) => {
return new Command({
goto: "bob",
update: {
foo: "bar",
},
// specify which graph to navigate to (defaults to the current graph)
graph: Command.PARENT,
})
}
网络¶
在此架构中,智能体被定义为图节点。每个智能体可以与其他所有智能体通信(多对多连接),并且可以决定下一步调用哪个智能体。此架构适用于没有明确的智能体层次结构或特定调用序列的问题。
import {
StateGraph,
Annotation,
MessagesAnnotation,
Command
} from "@langchain/langgraph";
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({
model: "gpt-4o-mini",
});
const agent1 = async (state: typeof MessagesAnnotation.State) => {
// you can pass relevant parts of the state to the LLM (e.g., state.messages)
// to determine which agent to call next. a common pattern is to call the model
// with a structured output (e.g. force it to return an output with a "next_agent" field)
const response = await model.withStructuredOutput(...).invoke(...);
return new Command({
update: {
messages: [response.content],
},
goto: response.next_agent,
});
};
const agent2 = async (state: typeof MessagesAnnotation.State) => {
const response = await model.withStructuredOutput(...).invoke(...);
return new Command({
update: {
messages: [response.content],
},
goto: response.next_agent,
});
};
const agent3 = async (state: typeof MessagesAnnotation.State) => {
...
return new Command({
update: {
messages: [response.content],
},
goto: response.next_agent,
});
};
const graph = new StateGraph(MessagesAnnotation)
.addNode("agent1", agent1, {
ends: ["agent2", "agent3" "__end__"],
})
.addNode("agent2", agent2, {
ends: ["agent1", "agent3", "__end__"],
})
.addNode("agent3", agent3, {
ends: ["agent1", "agent2", "__end__"],
})
.addEdge("__start__", "agent1")
.compile();
主管型¶
在此架构中,我们将智能体定义为节点,并添加一个主管节点(LLM),由它决定下一步应该调用哪个智能体节点。我们使用Command
根据主管的决策将执行路由到适当的智能体节点。此架构也适用于并行运行多个智能体或使用map-reduce模式。
import {
StateGraph,
MessagesAnnotation,
Command,
} from "@langchain/langgraph";
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({
model: "gpt-4o-mini",
});
const supervisor = async (state: typeof MessagesAnnotation.State) => {
// you can pass relevant parts of the state to the LLM (e.g., state.messages)
// to determine which agent to call next. a common pattern is to call the model
// with a structured output (e.g. force it to return an output with a "next_agent" field)
const response = await model.withStructuredOutput(...).invoke(...);
// route to one of the agents or exit based on the supervisor's decision
// if the supervisor returns "__end__", the graph will finish execution
return new Command({
goto: response.next_agent,
});
};
const agent1 = async (state: typeof MessagesAnnotation.State) => {
// you can pass relevant parts of the state to the LLM (e.g., state.messages)
// and add any additional logic (different models, custom prompts, structured output, etc.)
const response = await model.invoke(...);
return new Command({
goto: "supervisor",
update: {
messages: [response],
},
});
};
const agent2 = async (state: typeof MessagesAnnotation.State) => {
const response = await model.invoke(...);
return new Command({
goto: "supervisor",
update: {
messages: [response],
},
});
};
const graph = new StateGraph(MessagesAnnotation)
.addNode("supervisor", supervisor, {
ends: ["agent1", "agent2", "__end__"],
})
.addNode("agent1", agent1, {
ends: ["supervisor"],
})
.addNode("agent2", agent2, {
ends: ["supervisor"],
})
.addEdge("__start__", "supervisor")
.compile();
请查看此教程,了解主管多智能体架构的示例。
自定义多智能体工作流¶
在此架构中,我们将单个智能体添加为图节点,并提前在自定义工作流中定义智能体的调用顺序。在 LangGraph 中,工作流可以通过两种方式定义:
-
显式控制流(普通边):LangGraph 允许您通过普通图边明确定义应用程序的控制流(即智能体通信的序列)。这是上述架构中最具确定性的变体——我们总是提前知道下一步将调用哪个智能体。
-
动态控制流(条件边):在 LangGraph 中,您可以允许 LLM 决定应用程序控制流的一部分。这可以通过使用
Command
来实现。
import {
StateGraph,
MessagesAnnotation,
} from "@langchain/langgraph";
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({
model: "gpt-4o-mini",
});
const agent1 = async (state: typeof MessagesAnnotation.State) => {
const response = await model.invoke(...);
return { messages: [response] };
};
const agent2 = async (state: typeof MessagesAnnotation.State) => {
const response = await model.invoke(...);
return { messages: [response] };
};
const graph = new StateGraph(MessagesAnnotation)
.addNode("agent1", agent1)
.addNode("agent2", agent2)
// define the flow explicitly
.addEdge("__start__", "agent1")
.addEdge("agent1", "agent2")
.compile();
智能体之间的通信¶
构建多智能体系统时最重要的事情是弄清楚智能体如何通信。有几个不同的考虑因素:
图状态¶
要通过图状态进行通信,需要将单个智能体定义为图节点。这些节点可以作为函数或整个子图添加。在图执行的每一步,智能体节点接收图的当前状态,执行智能体代码,然后将更新后的状态传递给下一个节点。
通常,智能体节点共享一个单一的状态模式。但是,您可能希望设计具有不同状态模式的智能体节点。
不同的状态模式¶
一个智能体可能需要与其余智能体具有不同的状态模式。例如,搜索智能体可能只需要跟踪查询和检索到的文档。在 LangGraph 中,可以通过两种方式实现这一点:
- 定义具有独立状态模式的子图智能体。如果子图和父图之间没有共享状态键(通道),那么添加输入/输出转换非常重要,这样父图就知道如何与子图通信。
- 定义具有私有输入状态模式的智能体节点函数,该模式与整体图状态模式不同。这允许传递仅用于执行特定智能体所需的信息。
共享消息列表¶
智能体之间最常见的通信方式是通过共享状态通道,通常是消息列表。这假定状态中总是至少有一个由智能体共享的通道(键)。通过共享消息列表进行通信时,还有一个额外的考虑:智能体应该分享它们的完整思考过程历史还是只分享最终结果?
分享完整历史记录¶
智能体可以与所有其他智能体分享其完整思考过程的历史(即“草稿本”)。这个“草稿本”通常看起来像消息列表。分享完整思考过程的好处是,它可能有助于其他智能体做出更好的决策,并提高整个系统的推理能力。缺点是,随着智能体数量和复杂性的增加,“草稿本”会迅速增长,可能需要额外的内存管理策略。
分享最终结果¶
智能体可以拥有自己的私有“草稿本”,只与其余智能体分享最终结果。这种方法可能更适用于拥有许多智能体或更复杂的系统。在这种情况下,您需要定义具有不同状态模式的智能体。
对于作为工具调用的智能体,主管根据工具模式确定输入。此外,LangGraph 允许在运行时将状态传递给单个工具,因此下属智能体可以在需要时访问父状态。