如何添加断点¶
在创建 LangGraph 代理时,添加人工参与组件通常会很好。当赋予他们访问工具的权限时,这会很有帮助。通常在这些情况下,您可能希望在采取行动之前手动批准操作。
这可以通过多种方式实现,但主要支持的方式是在节点执行之前添加“中断”。这会在该节点处中断执行。然后您可以从该点恢复以继续。
设置¶
图表的代码¶
在本操作指南中,我们使用了一个简单的 ReAct 风格的托管图表(您可以在此处查看定义它的完整代码)。重要的是,这里有两个节点(一个名为 agent
,调用 LLM;一个名为 action
,调用工具),以及一个来自 agent
的路由函数,用于确定下一步是调用 action
还是直接结束图表运行(action
节点在执行后始终调用 agent
节点)。
SDK 初始化¶
添加断点¶
我们现在想在图表运行中添加一个断点,我们将在调用工具之前执行此操作。我们可以通过添加 interrupt_before=["action"]
来做到这一点,这告诉我们在调用 action 节点之前中断。我们可以在编译图表或启动运行时执行此操作。在这里,我们将在启动运行时执行此操作,如果您想在编译时执行此操作,您需要编辑定义图表的 python 文件,并在调用 .compile
时添加 interrupt_before
参数。
首先,让我们通过 SDK 访问我们托管的 LangGraph 实例
现在,让我们在工具节点之前使用断点编译它
input = {"messages": [{"role": "user", "content": "what's the weather in sf"}]}
async for chunk in client.runs.stream(
thread["thread_id"],
assistant_id,
input=input,
stream_mode="updates",
interrupt_before=["action"],
):
print(f"Receiving new event of type: {chunk.event}...")
print(chunk.data)
print("\n\n")
const input = { messages: [{ role: "human", content: "what's the weather in sf" }] };
const streamResponse = client.runs.stream(
thread["thread_id"],
assistantId,
{
input: input,
streamMode: "updates",
interruptBefore: ["action"]
}
);
for await (const chunk of streamResponse) {
console.log(`Receiving new event of type: ${chunk.event}...`);
console.log(chunk.data);
console.log("\n\n");
}
curl --request POST \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/stream \
--header 'Content-Type: application/json' \
--data "{
\"assistant_id\": \"agent\",
\"input\": {\"messages\": [{\"role\": \"human\", \"content\": \"what's the weather in sf\"}]},
\"interrupt_before\": [\"action\"],
\"stream_mode\": [
\"messages\"
]
}" | \
sed 's/\r$//' | \
awk '
/^event:/ {
if (data_content != "") {
print data_content "\n"
}
sub(/^event: /, "Receiving event of type: ", $0)
printf "%s...\n", $0
data_content = ""
}
/^data:/ {
sub(/^data: /, "", $0)
data_content = $0
}
END {
if (data_content != "") {
print data_content "\n"
}
}
'
输出
Receiving new event of type: metadata...
{'run_id': '3b77ef83-687a-4840-8858-0371f91a92c3'}
Receiving new event of type: data...
{'agent': {'messages': [{'content': [{'id': 'toolu_01HwZqM1ptX6E15A5LAmyZTB', 'input': {'query': 'weather in san francisco'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}], 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'ai', 'name': None, 'id': 'run-e5d17791-4d37-4ad2-815f-a0c4cba62585', 'example': False, 'tool_calls': [{'name': 'tavily_search_results_json', 'args': {'query': 'weather in san francisco'}, 'id': 'toolu_01HwZqM1ptX6E15A5LAmyZTB'}], 'invalid_tool_calls': []}]}}
Receiving new event of type: end...
None