断点¶
断点可以在定义好的点暂停图执行,并允许您逐步执行每个阶段。它们使用 LangGraph 的持久化层,该层会在每个步骤之后保存图状态。
使用断点,您可以随时检查图的状态和节点输入。执行会**无限期**暂停,直到您恢复,因为检查点会保存状态。
设置断点¶
graph = graph_builder.compile( # (1)!
interrupt_before=["node_a"], # (2)!
interrupt_after=["node_b", "node_c"], # (3)!
)
- 断点在
compile
期间设置。 interrupt_before
指定了在节点执行前应该暂停执行的节点。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)!
)
- 调用
client.runs.wait
时带上interrupt_before
和interrupt_after
参数。这是一个运行时配置,可以在每次调用时更改。 interrupt_before
指定了在节点执行前应该暂停执行的节点。interrupt_after
指定了在节点执行后应该暂停执行的节点。
await client.runs.wait( // (1)!
threadID,
assistantID,
{
input: input,
interruptBefore: ["node_a"], // (2)!
interruptAfter: ["node_b", "node_c"] // (3)!
}
)
- 调用
client.runs.wait
时带上interruptBefore
和interruptAfter
参数。这是一个运行时配置,可以在每次调用时更改。 interruptBefore
指定了在节点执行前应该暂停执行的节点。interruptAfter
指定了在节点执行后应该暂停执行的节点。
提示
此示例展示了如何添加**静态**断点。有关如何添加断点的更多选项,请参阅此指南。
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)!
)
- 图会一直运行,直到遇到第一个断点。
- 通过将输入设置为
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)!
);
- 图会一直运行,直到遇到第一个断点。
- 通过将输入设置为
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>
}"
恢复图
了解更多¶
- LangGraph 断点指南:了解有关在 LangGraph 中添加断点的更多信息。