当前位置: 首页 > article >正文

【Agent的革命之路——LangGraph】工作流中的 Command 模式

这篇文章我们将 LangGraph中的控制流(边)和状态更新(节点)结合起来使用。比如,我们希望同时执行状态更新并决定下一步要转到哪个节点,且这些操作在同一个节点中完成。而正好 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"
    )

如果我们使用的是 subgraphs(子图) 我们还可以通过 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
    )

当我们从子图节点向父图节点发送更新的时候,且更新的键同时存在于父图和子图的状态模式中时,我们必须为父图状态中正在更新的键定义一个 reducer。
下面我们开始我们的例子。

定义节点和State

定义 graph State:

class State(TypedDict):
    foo: str

定义节点A,

def node_a(state: State) -> Command[Literal["node_b", "node_c"]]:
    print("Called A")
    value = random.choice(["a", "b"])
    # 判断应该跳转到哪个节点
    if value == "a":
        goto = "node_b"
    else:
        goto = "node_c"

    # Command 允许我们同时更新图状态并路由到下一个节点。
    return Command(
        # 更新图中的状态
        update={"foo": value},
        # 替换edge
        goto=goto,
    )

定义节点b或者c:

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"}

定义好了上面节点之后,现在,我们可以使用上述节点创建 StateGraph。需要注意的是,该图没有定义条件的路由edge!这是因为控制流是用 Command 内部 node_a 定义的。

builder = StateGraph(State)
builder.add_edge(START, "node_a")
builder.add_node(node_a)
builder.add_node(node_b)
builder.add_node(node_c)

graph = builder.compile()

不知道大家注意到没有,我们的 node_a 使用了 Command 作为返回类型注解,例如 Command[Literal[“node_b”, “node_c”]]。这一步对于图的渲染是非常必要的,它会告诉 LangGraph:node_a 可以导航到 node_b 和 node_c。
现在我们看看图的·1展示效果:

from IPython.display import display, Image

display(Image(graph.get_graph().draw_mermaid_png()))

在这里插入图片描述
我们来试试多次调用graph:

graph.invoke({"foo": ""})

将会看到,graph会根据节点A中的随机选择,采用不同的路径(A-> B或A-> C)。

导航到 parent graph 中的节点

现在,让我们演示如何从 subgraph 内部导航到 parent graph 中的不同节点。我们将通过将上述示例中的 node_a 转换为一个单节点图来实现这一点,然后将其作为子图添加到 parent graph 中。

import operator
from typing_extensions import Annotated


class State(TypedDict):
    # N定义一个reducer 自动附加消息
    foo: Annotated[str, operator.add]


def node_a(state: State):
    print("Called A")
    value = random.choice(["a", "b"])
    if value == "a":
        goto = "node_b"
    else:
        goto = "node_c"

    # Command 允许我们同时更新图状态并路由到下一个节点。
    return Command(
        update={"foo": value},
        goto=goto,
        # 这会告诉 LangGraph 导航到父图中的 node_b 或 node_c 这将导航到相对于子图的最近的父图
        graph=Command.PARENT,
    )

subgraph = StateGraph(State).add_node(node_a).add_edge(START, "node_a").compile()

def node_b(state: State):
    print("Called B")
    # 由于我们已经定义了一个 reducer,因此不需要手动将新字符附加到现有的 foo 值上。相反,reducer 会自动(通过 operator.add)将这些值附加到现有值中。
    return {"foo": "b"}


def node_c(state: State):
    print("Called C")
    return {"foo": "c"}

然后我们来构造graph:

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()

查看效果:

Called A
Called C
{'foo': 'bc'}

到这里我们的代码就结束了,我们通过这种方式展示了如何使用 LangGraph 的 Command 对象在子图中更新状态并导航到父图中的不同节点。并通过 reducer 简化状态管理,就是自动保存消息链。


http://www.kler.cn/a/557171.html

相关文章:

  • XiaoMi Mi5(gemini) 刷入Ubuntu Touch 16.04——安卓手机刷入Linux
  • CLAM模型使用教程
  • 想学python进来看看把
  • 在本地使用 Llama 3.2-Vision:分步指南
  • python学智能算法(二)|模拟退火算法:进阶分析
  • Jetson Orin Nano烧录系统
  • 【多语言生态篇一】【DeepSeek×Java:Spring Boot微服务集成全栈指南 】
  • 1.3 AI量化炒股的基本流程
  • flink系列之:使用flink cdc3从mysql数据库同步数据到doris和starrocks
  • docker查看正在运行的容器详细教程
  • java八股文-消息队列
  • AtCoder Beginner Contest (ABC)394(ABCD)
  • MongoDB学习
  • Python爬虫selenium验证-中文识别点选+图片验证码案例
  • vue中的watch 和 computed 的区别
  • 软件架构设计:软件工程
  • Golang通过 并发计算平方 示例演示并发
  • 【Postgresql】Linux 部署 Postgresql 数据库 (图文教程)
  • 直角三角堰计算公式
  • 开发指南103-jpa的find**/get**全解