跳到内容

断点

断点可以在定义的点暂停图的执行,并让你逐步执行每个阶段。它们使用 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. 调用 client.runs.wait 时,需传入 interrupt_beforeinterrupt_after 参数。这是一个运行时配置,可以在每次调用时更改。
  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. 调用 client.runs.wait 时,需传入 interruptBeforeinterruptAfter 参数。这是一个运行时配置,可以在每次调用时更改。
  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>
}"

提示

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

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

# Using the graph deployed with the name "agent"
assistant_id = "agent"

# create a thread
thread = await client.threads.create()
thread_id = thread["thread_id"]

# Run the graph until the breakpoint
result = await client.runs.wait(
    thread_id,
    assistant_id,
    input=inputs   # (1)!
)

# Resume the graph
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> });

// Using the graph deployed with the name "agent"
const assistantID = "agent";

// create a thread
const thread = await client.threads.create();
const threadID = thread["thread_id"];

// Run the graph until the breakpoint
const result = await client.runs.wait(
  threadID,
  assistantID,
  { input: input }   // (1)!
);

// Resume the graph
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\"
}"

了解更多