Skip to content

断点

断点会在定义的点暂停图的执行,并允许你逐步查看每个阶段。它们使用 LangGraph 的 持久化层,该层会在每一步之后保存图的状态。

通过断点,你可以在任何时刻检查图的状态和节点输入。执行会 无限期地 暂停,直到你恢复执行,因为检查点机制会保留状态。

设置断点

graph = graph_builder.compile( # (1)!
    interrupt_before=["node_a"], # (2)!
    interrupt_after=["node_b", "node_c"], # (3)!
)
  1. 断点在 compile 阶段设置。
  2. interrupt_before 指定在节点执行前应暂停的节点。
  3. interrupt_after 指定在节点执行后应暂停的节点。
await client.runs.wait( # (1)!
    thread_id,
    assistant_id,
    inputs=inputs,
    interrupt_before=["node_a"], # (2)!
    interrupt_after=["node_b", "node_c"] # (3)!
)
  1. 使用 interrupt_beforeinterrupt_after 参数调用 client.runs.wait。这是运行时配置,每次调用都可以更改。
  2. interrupt_before 指定在节点执行前应暂停的节点。
  3. interrupt_after 指定在节点执行后应暂停的节点。
await client.runs.wait( // (1)!
  threadID,
  assistantID,
  {
    input: input,
    interruptBefore: ["node_a"], // (2)!
    interruptAfter: ["node_b", "node_c"] // (3)!
  }
)
  1. 使用 interruptBeforeinterruptAfter 参数调用 client.runs.wait。这是运行时配置,每次调用都可以更改。
  2. interruptBefore 指定在节点执行前应暂停的节点。
  3. interruptAfter 指定在节点执行后应暂停的节点。
curl --request POST \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/wait \
--header 'Content-Type: application/json' \
--data "{
  \"assistant_id\": \"agent\",
  \"interrupt_before\": [\"node_a\"],
  \"interrupt_after\": [\"node_b\", \"node_c\"],
  \"input\": <INPUT>
}"

Tip

此示例展示了如何添加 静态 断点。有关如何添加断点的更多选项,请参阅 此指南

from langgraph_sdk import get_client
client = get_client(url=<DEPLOYMENT_URL>)

# 使用名为 "agent" 的已部署图
assistant_id = "agent"

# 创建线程
thread = await client.threads.create()
thread_id = thread["thread_id"]

# 运行图直到遇到断点
result = await client.runs.wait(
    thread_id,
    assistant_id,
    input=inputs   # (1)!
)

# 恢复图的执行
await client.runs.wait(
    thread_id,
    assistant_id,
    input=None   # (2)!
)
  1. 图会运行到第一个断点处停止。
  2. 通过将输入设为 None 来恢复图的执行。这将运行图直到下一个断点被触发。
import { Client } from "@langchain/langgraph-sdk";
const client = new Client({ apiUrl: <DEPLOYMENT_URL> });

// 使用名为 "agent" 的已部署图
const assistantID = "agent";

// 创建线程
const thread = await client.threads.create();
const threadID = thread["thread_id"];

// 运行图直到遇到断点
const result = await client.runs.wait(
  threadID,
  assistantID,
  { input: input }   // (1)!
);

// 恢复图的执行
await client.runs.wait(
  threadID,
  assistantID,
  { input: null }   // (2)!
);
  1. 图会运行到第一个断点处停止。
  2. 通过将输入设为 null 来恢复图的执行。这将运行图直到下一个断点被触发。

创建线程:

curl --request POST \
--url <DEPLOYMENT_URL>/threads \
--header 'Content-Type: application/json' \
--data '{}'

运行图直到遇到断点:

curl --request POST \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/wait \
--header 'Content-Type: application/json' \
--data "{
  \"assistant_id\": \"agent\",
  \"input\": <INPUT>
}"

恢复图的执行:

curl --request POST \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/wait \
--header 'Content-Type: application/json' \
--data "{
  \"assistant_id\": \"agent\"
}"

学习更多