中断¶
本指南假设你已经了解双文本发送(double-texting)的概念,你可以在双文本发送概念指南中学习相关内容。
本指南涵盖了双文本发送的 interrupt
选项,该选项会中断图的上一次运行,并使用双文本启动一个新的运行。此选项不会删除第一次运行,而是将其保留在数据库中,但将其状态设置为 interrupted
(已中断)。以下是使用 interrupt
选项的一个简单示例。
设置¶
首先,我们将定义一个快速辅助函数,用于打印出 JavaScript 和 CURL 模型的输出(如果使用 Python,可以跳过此步骤):
# 将此代码放在名为 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"
}
现在,让我们导入所需的包,并实例化客户端、助手和线程。
创建运行实例¶
现在我们可以启动两个运行实例,并等待第二个运行实例完成:
# 第一个运行实例将被中断
interrupted_run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "what's the weather in sf?"}]},
)
# 稍作休眠以获取第一个运行实例的部分输出
await asyncio.sleep(2)
run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "what's the weather in nyc?"}]},
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: "what's the weather in sf?" }] } }
);
// 稍作休眠以获取第一个运行实例的部分输出
await new Promise(resolve => setTimeout(resolve, 2000));
let run = await client.runs.create(
thread["thread_id"],
assistantId,
{
input: { messages: [{ role: "human", content: "what's the weather in nyc?" }] },
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\": \"what\'s the weather in sf?\"}]},
}" && 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\": \"what\'s the weather in nyc?\"}]},
\"multitask_strategy\": \"interrupt\"
}" && curl --request GET \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/<RUN_ID>/join
查看运行结果¶
我们可以看到,线程包含第一次运行的部分数据 + 第二次运行的数据
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': 'weather in san francisco'}, '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 英里每小时。温度。旧金山天气预报。Weather Underground 提供本地及长期天气..."}]
================================ 人类消息 =================================
纽约市的天气如何?
================================== AI 消息 ==================================
[{'id': 'toolu_01KtE1m1ifPLQAx4fQLyZL9Q', 'input': {'query': 'weather in new york city'}, '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": "获取纽约市 6 月的月度天气预报,包括每日高低温、历史平均值,助您提前规划。"}]
================================== AI 消息 ==================================
搜索结果提供了纽约市的天气预报和相关信息。根据 AccuWeather 的首要结果,以下是关于纽约市天气的一些关键细节:
- 这是纽约市 6 月的月度天气预报。
- 包含每日高低温,助您提前规划。
- 还提供了纽约市 6 月的历史平均值作为参考。
- 通过访问 AccuWeather 页面,可找到更详细的每日或每小时预报,包括降水概率、湿度、风力等信息。
总之,此次搜索为您提供了纽约市未来一个月预期天气状况的便捷概述,让您了解出行或制定计划时的准备方向。如果您需要其他详细信息,请告知!
验证原始的、被中断的运行是否已中断
输出:
'interrupted'