加入队列¶
本指南假设您已经了解什么是双发短信,如果您还不了解,可以在双发短信概念指南中学习。
该指南涵盖了双发短信中的enqueue
选项,它会将中断请求加入队列,并按照客户端接收的顺序执行它们。下面是使用enqueue
选项的一个快速示例。
设置¶
首先,我们将定义一个用于打印JS和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"
}
然后,让我们导入所需的包并实例化我们的客户端、助手和线程。
创建运行¶
现在让我们开始两个运行,第二个运行会中断第一个运行,并采用多任务策略“入队”:
first_run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "旧金山的天气怎么样?"}]},
)
second_run = await client.runs.create(
thread["thread_id"],
assistant_id,
input={"messages": [{"role": "user", "content": "纽约的天气怎么样?"}]},
multitask_strategy="enqueue",
)
const firstRun = await client.runs.create(
thread["thread_id"],
assistantId,
input={"messages": [{"role": "user", "content": "旧金山的天气怎么样?"}]},
)
const secondRun = await client.runs.create(
thread["thread_id"],
assistantId,
input={"messages": [{"role": "user", "content": "纽约的天气怎么样?"}]},
multitask_strategy="enqueue",
)
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\": \"enqueue\"
}"
查看运行结果¶
验证线程从两次运行中都有数据:
source pretty_print.sh && curl --request GET \
--url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/<RUN_ID>/join && \
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_01Dez1sJre4oA2Y7NsKJV6VT', 'input': {'query': 'weather in san francisco'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}]
工具调用:
tavily_search_results_json (toolu_01Dez1sJre4oA2Y7NsKJV6VT)
调用ID:toolu_01Dez1sJre4oA2Y7NsKJV6VT
参数:
query: weather in san francisco
================================= 工具消息 =================================
名称:tavily_search_results_json
[{"url": "https://www.accuweather.com/en/us/san-francisco/94103/weather-forecast/347629", "content": "获取旧金山当前和未来的天气状况,包括温度、降水、风速、空气质量等。查看小时预报、10天展望、雷达地图、警报和过敏信息。"}]
================================== AI 消息 ==================================
根据AccuWeather的数据,旧金山当前的天气状况如下:
温度:57°F(14°C)
天气状况:大部分晴朗
风速:WSW 10英里/小时
湿度:72%
接下来的几天天气预报显示部分晴朗天空,白天最高气温在50到60华氏度之间,夜间最低气温在40到50华氏度之间。典型的温和干燥天气。
AccuWeather预报的一些关键细节:
今天:大部分晴朗,最高气温62°F(17°C)
今晚:部分多云,最低气温49°F(9°C)
明天:部分晴朗,最高气温59°F(15°C)
周六:大部分晴朗,最高气温64°F(18°C)
周日:部分晴朗,最高气温61°F(16°C)
总结来说,未来几天旧金山的天气预计为春季天气,白天有阳光和云彩,夜间气温在40多度,白天在60多度。没有降雨预报,典型的干燥条件。
================================ 人类消息 =================================
纽约市的天气怎么样?
================================== AI 消息 ==================================
[{'text': '以下是纽约市当前天气状况和预报:', 'type': 'text'}, {'id': 'toolu_01FFft5Sx9oS6AdVJuRWWcGp', 'input': {'query': 'weather in new york city'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}]
工具调用:
tavily_search_results_json (toolu_01FFft5Sx9oS6AdVJuRWWcGp)
调用ID:toolu_01FFft5Sx9oS6AdVJuRWWcGp
参数:
query: weather in new york city
================================= 工具消息 =================================
名称: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 消息 ==================================
根据WeatherAPI提供的天气数据:
纽约市当前天气状况(截至当地时间下午2点):
- 温度:85°F(29°C)
- 天气状况:晴朗
- 风速:2英里/小时(4公里/小时),来自东南偏南方向
- 湿度:63%
- 体感温度:85°F(30°C)
预报显示接下来几天将持续晴朗温暖的天气:
今天:晴朗,最高气温85°F(29°C)
今晚:晴朗,最低气温68°F(20°C)
明天:晴朗,最高气温88°F(31°C)
星期四:大部分晴朗,最高气温90°F(32°C)
星期五:部分多云,最高气温87°F(31°C)
所以纽约市目前正经历美丽的晴朗天气,气温在80多华氏度(约30摄氏度)。湿度适中,在60%左右。总体来说,这是理想的晚春/初夏户外活动条件。