中断¶
本指南假设您已了解双重发送的概念,您可以在双重发送概念指南中了解相关信息。
本指南涵盖了双重发送的interrupt选项,该选项会中断先前的图形运行,并用双重发送开始新运行。此选项不会删除第一次运行,而是将其保留在数据库中,但将其状态设置为interrupted。下面是使用interrupt选项的快速示例。
设置¶
首先,我们将定义一个快速的辅助函数,用于打印JS和CURL模型输出(如果使用Python,可以跳过此步骤):
function prettyPrint(m) {
const padded = " " + m['type'] + " ";
const sepLen = Math.floor((80 - padded.length) / 2);
const sep = "=".repeat(sepLen);
const secondSep = sep + (padded.length % 2 ? "=" : "");
console.log(`${sep}${padded}${secondSep}`);
console.log("\n\n");
console.log(m.content);
}
# 将此内容放入名为 pretty_print.sh 的文件中
pretty_print() {
local type="$1"
local content="$2"
local padded=" $type "
local total_width=80
local sep_len=$(( (total_width - ${#padded}) / 2 ))
local sep=$(printf '=%.0s' $(eval "echo {1.."${sep_len}"}"))
local second_sep=$sep
if (( (total_width - ${#padded}) % 2 )); then
second_sep="${second_sep}="
fi
echo "${sep}${padded}${second_sep}"
echo
echo "$content"
}
现在,让我们导入所需的包并实例化我们的客户端、助手和线程。
import asyncio
from langchain_core.messages import convert_to_messages
from langgraph_sdk import get_client
client = get_client(url=<DEPLOYMENT_URL>)
# 使用名为 "agent" 的图形
assistant_id = "agent"
thread = await client.threads.create()
import { Client } from "@langchain/langgraph-sdk";
const client = new Client({ apiUrl: <DEPLOYMENT_URL> });
// 使用名为 "agent" 的图形
const assistantId = "agent";
const thread = await client.threads.create();
curl --request POST \
--url <DEPLOYMENT_URL>/threads \
--header 'Content-Type: application/json' \
--data '{}'
创建运行¶
现在我们可以启动两个运行,并在第二个运行完成之前加入它:
# 第一次运行将被中断
interrupted_run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "在旧金山的天气怎么样?"}]},
)
# 等待一下以获取第一次运行的部分输出
await asyncio.sleep(2)
run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "在纽约的天气怎么样?"}]},
multitask_strategy="interrupt",
)
# 等待直到第二次运行完成
await client.runs.join(thread["thread_id"], run["run_id"])
// 第一次运行将被中断
let interruptedRun = await client.runs.create(
thread["thread_id"],
assistantId,
{ input: { messages: [{ role: "human", content: "在旧金山的天气怎么样?" }] } }
);
// 等待一下以获取第一次运行的部分输出
await new Promise(resolve => setTimeout(resolve, 2000));
let run = await client.runs.create(
thread["thread_id"],
assistantId,
{
input: { messages: [{ role: "human", content: "在纽约的天气怎么样?" }] },
multitaskStrategy: "interrupt"
}
);
// 等待直到第二次运行完成
await client.runs.join(thread["thread_id"], run["run_id"]);
curl --request POST \
--url <DEPLOY<ENT_URL>>/threads/<THREAD_ID>/runs \
--header 'Content-Type: application/json' \
--data "{
\"assistant_id\": \"agent\",
\"input\": {\"messages\": [{\"role\": \"human\", \"content\": \"在旧金山的天气怎么样?\"}]},
}" && sleep 2 && curl --request POST \
--url <DEPLOY<ENT_URL>>/threads/<THREAD_ID>/runs \
--header 'Content-Type: application/json' \
--data "{
\"assistant_id\": \"agent\",
\"input\": {\"messages\": [{\"role\": \"human\", \"content\": \"在纽约的天气怎么样?\"}]},
\"multitask_strategy\": \"interrupt\"
}" && curl --request GET \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/<RUN_ID>/join
查看运行结果¶
我们可以看到,该线程有来自第一次运行的部分数据 + 第二次运行的数据
state = await client.threads.get_state(thread["thread_id"])
for m in convert_to_messages(state["values"]["messages"]):
m.pretty_print()
const state = await client.threads.getState(thread["thread_id"]);
for (const m of state['values']['messages']) {
prettyPrint(m);
}
source pretty_print.sh && curl --request GET \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/state | \
jq -c '.values.messages[]' | while read -r element; do
type=$(echo "$element" | jq -r '.type')
content=$(echo "$element" | jq -r '.content | if type == "array" then tostring else . end')
pretty_print "$type" "$content"
done
输出:
=============================== 人类消息 =================================
在旧金山的天气怎么样? ================================== AI 消息 ==================================
[{'id': 'toolu_01MjNtVJwEcpujRGrf3x6Pih', 'input': {'query': '旧金山的天气'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}] 工具调用: tavily_search_results_json (toolu_01MjNtVJwEcpujRGrf3x6Pih) 调用 ID: toolu_01MjNtVJwEcpujRGrf3x6Pih 参数: 查询: 旧金山的天气 ================================= 工具消息 ================================= 名称: tavily_search_results_json
[{"url": "https://www.wunderground.com/hourly/us/ca/san-francisco/KCASANFR2002/date/2024-6-18", "content": "最高温度64华氏度。西风10到20英里每小时。时不时有几片云。最低温度49华氏度。西风10到20英里每小时。旧金山天气预报。天气地下提供当地和长时间天气..."}] =============================== 人类消息 =================================
在纽约的天气怎么样? ================================== AI 消息 ==================================
[{'id': 'toolu_01KtE1m1ifPLQAx4fQLyZL9Q', 'input': {'query': '纽约市的天气'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}] 工具调用: tavily_search_results_json (toolu_01KtE1m1ifPLQAx4fQLyZL9Q) 调用 ID: toolu_01KtE1m1ifPLQAx4fQLyZL9Q 参数: 查询: 纽约市的天气 ================================= 工具消息 ================================= 名称: tavily_search_results_json
[{"url": "https://www.accuweather.com/en/us/new-york/10021/june-weather/349727", "content": "查看纽约,纽约的每月天气预报,包括每日最高/最低,历史平均,以帮助您提前计划。"}] ================================= AI 消息 ==================================
搜索结果提供了纽约市的天气预报和信息。根据AccuWeather的最佳结果,以下是关于纽约市天气的一些关键细节:
- 这是纽约市六月份的每月天气预报。
- 包括每日最高和最低温度,以帮助提前计划。
- 六月份在纽约市的历史平均值也提供作为参考。
- 访问AccuWeather页面可以找到更详细的每日或每小时预报,包括降水几率、湿度、风速等信息。
因此,总而言之,搜索为您提供了关于下个月纽约市天气状况的便利概述,以便您了解旅行或制定计划时应如何准备。如需任何其他详细信息,请告知我!
确认原始的中断运行已被中断
输出:
'interrupted'