如何将控制流和状态更新与 Command 结合使用¶
将控制流(边)和状态更新(节点)结合使用可能很有用。例如,您可能希望在同一节点中同时执行状态更新并决定接下来要转到哪个节点。LangGraph 提供了一种通过从节点函数返回 Command
对象来实现此目的的方法
def my_node(state: State) -> Command[Literal["my_other_node"]]:
return Command(
# state update
update={"foo": "bar"},
# control flow
goto="my_other_node"
)
如果您正在使用 子图,您可能希望从子图中的节点导航到不同的子图(即父图中的不同节点)。为此,您可以在 Command
中指定 graph=Command.PARENT
def my_node(state: State) -> Command[Literal["my_other_node"]]:
return Command(
update={"foo": "bar"},
goto="other_subgraph", # where `other_subgraph` is a node in the parent graph
graph=Command.PARENT
)
使用 Command.PARENT
进行状态更新
当您从子图节点向父图节点发送更新,且更新的键在父图和子图 状态模式 中共享时,您必须为您在父图状态中更新的键定义一个 reducer。请参阅下面的 示例。
本指南展示了如何使用 Command
在您的 LangGraph 应用程序中添加动态控制流。
设置¶
首先,让我们安装所需的软件包
设置 LangSmith 以进行 LangGraph 开发
注册 LangSmith 以快速发现问题并提高 LangGraph 项目的性能。LangSmith 使您可以使用跟踪数据来调试、测试和监控使用 LangGraph 构建的 LLM 应用程序 — 阅读 此处 了解更多关于如何开始的信息。
让我们创建一个包含 3 个节点的简单图:A、B 和 C。我们将首先执行节点 A,然后根据节点 A 的输出决定接下来转到节点 B 还是节点 C。
基本用法¶
import random
from typing_extensions import TypedDict, Literal
from langgraph.graph import StateGraph, START
from langgraph.types import Command
# Define graph state
class State(TypedDict):
foo: str
# Define the nodes
def node_a(state: State) -> Command[Literal["node_b", "node_c"]]:
print("Called A")
value = random.choice(["a", "b"])
# this is a replacement for a conditional edge function
if value == "a":
goto = "node_b"
else:
goto = "node_c"
# note how Command allows you to BOTH update the graph state AND route to the next node
return Command(
# this is the state update
update={"foo": value},
# this is a replacement for an edge
goto=goto,
)
def node_b(state: State):
print("Called B")
return {"foo": state["foo"] + "b"}
def node_c(state: State):
print("Called C")
return {"foo": state["foo"] + "c"}
API 参考: StateGraph | START | Command
我们现在可以使用上述节点创建 StateGraph
。请注意,该图没有用于路由的 条件边!这是因为控制流是在 node_a
内部使用 Command
定义的。
builder = StateGraph(State)
builder.add_edge(START, "node_a")
builder.add_node(node_a)
builder.add_node(node_b)
builder.add_node(node_c)
# NOTE: there are no edges between nodes A, B and C!
graph = builder.compile()
重要提示
您可能已经注意到我们使用了 Command
作为返回类型注释,例如 Command[Literal["node_b", "node_c"]]
。这对于图渲染是必要的,并告诉 LangGraph node_a
可以导航到 node_b
和 node_c
。
如果我们多次运行该图,我们会看到它根据节点 A 中的随机选择采用不同的路径(A -> B 或 A -> C)。
导航到父图中的节点¶
现在让我们演示如何从子图内部导航到父图中的不同节点。我们将通过将上述示例中的 node_a
更改为单节点图来实现这一点,该图将作为子图添加到我们的父图中。
import operator
from typing_extensions import Annotated
class State(TypedDict):
# NOTE: we define a reducer here
foo: Annotated[str, operator.add]
def node_a(state: State):
print("Called A")
value = random.choice(["a", "b"])
# this is a replacement for a conditional edge function
if value == "a":
goto = "node_b"
else:
goto = "node_c"
# note how Command allows you to BOTH update the graph state AND route to the next node
return Command(
update={"foo": value},
goto=goto,
# this tells LangGraph to navigate to node_b or node_c in the parent graph
# NOTE: this will navigate to the closest parent graph relative to the subgraph
graph=Command.PARENT,
)
subgraph = StateGraph(State).add_node(node_a).add_edge(START, "node_a").compile()
def node_b(state: State):
print("Called B")
# NOTE: since we've defined a reducer, we don't need to manually append
# new characters to existing 'foo' value. instead, reducer will append these
# automatically (via operator.add)
return {"foo": "b"}
def node_c(state: State):
print("Called C")
return {"foo": "c"}
builder = StateGraph(State)
builder.add_edge(START, "subgraph")
builder.add_node("subgraph", subgraph)
builder.add_node(node_b)
builder.add_node(node_c)
graph = builder.compile()