跳转到内容

如何向子图添加线程级持久化

先决条件

本指南假设您熟悉以下内容

本指南展示了如何向使用子图的图添加线程级持久化。

设置

首先,让我们安装所需的软件包

%%capture --no-stderr
%pip install -U langgraph

设置 LangSmith 以进行 LangGraph 开发

注册 LangSmith 以快速发现问题并提高 LangGraph 项目的性能。LangSmith 允许您使用跟踪数据来调试、测试和监控使用 LangGraph 构建的 LLM 应用程序 — 阅读此处了解更多关于如何开始的信息。

定义具有持久化的图

要向具有子图的图添加持久化,您只需在编译父图时传递一个检查点器。LangGraph 将自动将检查点器传播到子子图。

注意

不应在编译子图时提供检查点器。相反,您必须定义一个单一的检查点器,并将其传递给 parent_graph.compile(),LangGraph 将自动将检查点器传播到子子图。如果您将检查点器传递给 subgraph.compile(),它将被简单地忽略。当您添加调用子图的节点函数时,这也适用。

让我们定义一个带有单个子图节点的简单图,以展示如何执行此操作。

from langgraph.graph import START, StateGraph
from langgraph.checkpoint.memory import MemorySaver
from typing import TypedDict


# subgraph


class SubgraphState(TypedDict):
    foo: str  # note that this key is shared with the parent graph state
    bar: str


def subgraph_node_1(state: SubgraphState):
    return {"bar": "bar"}


def subgraph_node_2(state: SubgraphState):
    # note that this node is using a state key ('bar') that is only available in the subgraph
    # and is sending update on the shared state key ('foo')
    return {"foo": state["foo"] + state["bar"]}


subgraph_builder = StateGraph(SubgraphState)
subgraph_builder.add_node(subgraph_node_1)
subgraph_builder.add_node(subgraph_node_2)
subgraph_builder.add_edge(START, "subgraph_node_1")
subgraph_builder.add_edge("subgraph_node_1", "subgraph_node_2")
subgraph = subgraph_builder.compile()


# parent graph


class State(TypedDict):
    foo: str


def node_1(state: State):
    return {"foo": "hi! " + state["foo"]}


builder = StateGraph(State)
builder.add_node("node_1", node_1)
# note that we're adding the compiled subgraph as a node to the parent graph
builder.add_node("node_2", subgraph)
builder.add_edge(START, "node_1")
builder.add_edge("node_1", "node_2")

API 参考: START | StateGraph | MemorySaver

<langgraph.graph.state.StateGraph at 0x106d2fa10>

我们现在可以使用内存检查点器 (MemorySaver) 编译图。

checkpointer = MemorySaver()
# You must only pass checkpointer when compiling the parent graph.
# LangGraph will automatically propagate the checkpointer to the child subgraphs.
graph = builder.compile(checkpointer=checkpointer)

验证持久化是否工作

现在让我们运行该图,并检查父图和子图的持久化状态,以验证持久化是否工作。我们应该期望在 state.values 中看到父图和子图的最终执行结果。

config = {"configurable": {"thread_id": "1"}}

for _, chunk in graph.stream({"foo": "foo"}, config, subgraphs=True):
    print(chunk)
{'node_1': {'foo': 'hi! foo'}}
{'subgraph_node_1': {'bar': 'bar'}}
{'subgraph_node_2': {'foo': 'hi! foobar'}}
{'node_2': {'foo': 'hi! foobar'}}
我们现在可以通过使用与调用图时相同的配置调用 graph.get_state() 来查看父图状态。

graph.get_state(config).values
{'foo': 'hi! foobar'}

要查看子图状态,我们需要做两件事

  1. 找到子图的最新配置值
  2. 使用 graph.get_state() 来检索最近子图配置的该值。

要找到正确的配置,我们可以检查父图的状态历史记录,并在我们从 node_2(带有子图的节点)返回结果之前找到状态快照

state_with_subgraph = [
    s for s in graph.get_state_history(config) if s.next == ("node_2",)
][0]

状态快照将包括接下来要执行的 tasks 列表。当使用子图时,tasks 将包含我们可以用来检索子图状态的配置

subgraph_config = state_with_subgraph.tasks[0].state
subgraph_config
{'configurable': {'thread_id': '1',
  'checkpoint_ns': 'node_2:6ef111a6-f290-7376-0dfc-a4152307bc5b'}}
graph.get_state(subgraph_config).values
{'foo': 'hi! foobar', 'bar': 'bar'}

如果您想了解更多关于如何为人工参与环路工作流修改子图状态的信息,请查看此操作指南

评论