回滚¶
本指南假设您已经了解什么是双重发送,您可以在双重发送概念指南中了解相关内容。
本指南涵盖了双重发送的rollback
选项,该选项会中断之前的图运行,并开始一个新的图运行,其中包含双重发送的内容。此选项与interrupt
选项非常相似,但在这种情况下,第一次运行将完全从数据库中删除,并且无法重新启动。以下是使用rollback
选项的一个快速示例。
设置¶
首先,我们将定义一个用于打印JS和CURL模型输出的快速辅助函数(如果你使用Python可以跳过这一步):
# PLACE THIS IN A FILE CALLED 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"
}
现在,让我们导入所需的包并实例化我们的客户端、助手和线程。
创建运行¶
现在让我们使用多任务参数设置为"回滚"的线程进行运行:
# 第一次运行将被回滚
rolled_back_run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "旧金山的天气怎么样?"}]},
)
run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "纽约的天气怎么样?"}]},
multitask_strategy="rollback",
)
# 等待第二次运行完成
await client.runs.join(thread["thread_id"], run["run_id"])
// 第一次运行将被中断
let rolledBackRun = await client.runs.create(
thread["thread_id"],
assistantId,
{ input: { messages: [{ role: "human", content: "旧金山的天气怎么样?" }] } }
);
let run = await client.runs.create(
thread["thread_id"],
assistant_id,
{
input: { messages: [{ role: "human", content: "纽约的天气怎么样?" }] },
multitaskStrategy: "rollback"
}
);
// 等待第二次运行完成
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\": \"旧金山的天气怎么样?\"}]},
}" && 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\": \"rollback\"
}" && 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
输出:
================================ 人类消息 =================================
nyc 的天气如何?
================================== AI 消息 ==================================
[{'id': 'toolu_01JzPqefao1gxwajHQ3Yh3JD', 'input': {'query': 'weather in nyc'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}]
工具调用:
tavily_search_results_json (toolu_01JzPqefao1gxwajHQ3Yh3JD)
调用 ID: toolu_01JzPqefao1gxwajHQ3Yh3JD
参数:
query: weather in nyc
================================= 工具消息 =================================
名称: tavily_search_results_json
[{"url": "https://www.weatherapi.com/", "content": "{'location': {'name': 'New York', 'region': 'New York', 'country': 'United States of America', 'lat': 40.71, 'lon': -74.01, 'tz_id': 'America/New_York', 'localtime_epoch': 1718734479, 'localtime': '2024-06-18 14:14'}, 'current': {'last_updated_epoch': 1718733600, 'last_updated': '2024-06-18 14:00', 'temp_c': 29.4, 'temp_f': 84.9, 'is_day': 1, 'condition': {'text': 'Sunny', 'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png', 'code': 1000}, 'wind_mph': 2.2, 'wind_kph': 3.6, 'wind_degree': 158, 'wind_dir': 'SSE', 'pressure_mb': 1025.0, 'pressure_in': 30.26, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 63, 'cloud': 0, 'feelslike_c': 31.3, 'feelslike_f': 88.3, 'windchill_c': 28.3, 'windchill_f': 82.9, 'heatindex_c': 29.6, 'heatindex_f': 85.3, 'dewpoint_c': 18.4, 'dewpoint_f': 65.2, 'vis_km': 16.0, 'vis_miles': 9.0, 'uv': 7.0, 'gust_mph': 16.5, 'gust_kph': 26.5}}"}]
================================== AI 消息 ==================================
天气 API 的结果显示,纽约市当前的天气是晴朗,气温约为 85°F (29°C)。风速较轻,约为 2-3 英里/小时,来自东南偏南方向。总体来看,这看起来像是一个晴朗的夏日。
验证原始回滚的运行已被删除
输出:
原始运行已被正确删除