如何流式传输图的完整状态¶
前提条件
本指南介绍如何使用 stream_mode="values"
,它在每个超步流式传输状态的值。这与使用 stream_mode="updates"
不同:它不是仅流式传输来自每个节点的状态更新,而是在该超步流式传输整个图状态。
设置¶
首先,让我们设置客户端和线程
输出
{
'thread_id': 'bfc68029-1f7b-400f-beab-6f9032a52da4',
'created_at': '2024-06-24T21:30:07.980789+00:00',
'updated_at': '2024-06-24T21:30:07.980789+00:00',
'metadata': {},
'status': 'idle',
'config': {},
'values': None
}
以值模式流式传输图¶
现在我们可以按值流式传输,这会在每个节点完成执行后流式传输图的完整状态
const input = {"messages": [{"role": "user", "content": "what's the weather in la"}]}
const streamResponse = client.runs.stream(
thread["thread_id"],
assistantID,
{
input,
streamMode: "values"
}
);
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 la\"}]},
\"stream_mode\": [
\"values\"
]
}" | \
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": "f08791ce-0a3d-44e0-836c-ff62cd2e2786"}
Receiving new event of type: values...
{
"messages": [
{
"role": "human",
"content": "what's the weather in la"
}
]
}
Receiving new event of type: values...
{
"messages": [
{
"content": "what's the weather in la",
"type": "human",
...
},
{
"content": "",
"type": "ai",
"tool_calls": [
{
"name": "tavily_search_results_json",
"args": {
"query": "weather in los angeles"
},
"id": "toolu_01E5mSaZWm5rWJnCqmt63v4g"
}
],
...
}
]
}
...
Receiving new event of type: values...
{
"messages": [
{
"content": "what's the weather in la",
"type": "human",
...
},
{
"content": "",
"type": "ai",
"tool_calls": [
{
"name": "tavily_search_results_json",
"args": {
"query": "weather in los angeles"
},
"id": "toolu_01E5mSaZWm5rWJnCqmt63v4g"
}
],
...
}
{
"content": [
{
"url": "https://www.weatherapi.com/",
"content": "{\"location\": {\"name\": \"Los Angeles\", \"region\": \"California\", \"country\": \"United States of America\", \"lat\": 34.05, \"lon\": -118.24, \"tz_id\": \"America/Los_Angeles\", \"localtime_epoch\": 1716310320, \"localtime\": \"2024-05-21 9:52\"}, \"current\": {\"last_updated_epoch\": 1716309900, \"last_updated\": \"2024-05-21 09:45\", \"temp_c\": 16.7, \"temp_f\": 62.1, \"is_day\": 1, \"condition\": {\"text\": \"Overcast\", \"icon\": \"//cdn.weatherapi.com/weather/64x64/day/122.png\", \"code\": 1009}, \"wind_mph\": 8.1, \"wind_kph\": 13.0, \"wind_degree\": 250, \"wind_dir\": \"WSW\", \"pressure_mb\": 1015.0, \"pressure_in\": 29.97, \"precip_mm\": 0.0, \"precip_in\": 0.0, \"humidity\": 65, \"cloud\": 100, \"feelslike_c\": 16.7, \"feelslike_f\": 62.1, \"vis_km\": 16.0, \"vis_miles\": 9.0, \"uv\": 5.0, \"gust_mph\": 12.5, \"gust_kph\": 20.2}}"
}
],
"type": "tool",
"name": "tavily_search_results_json",
"tool_call_id": "toolu_01E5mSaZWm5rWJnCqmt63v4g"
...
},
{
"content": "Based on the weather API results, the current weather in Los Angeles is overcast with a temperature of around 62°F (17°C). There are light winds from the west-southwest around 8-13 mph. The humidity is 65% and visibility is good at 9 miles. Overall, mild spring weather conditions in LA.",
"type": "ai",
...
}
]
}
Receiving new event of type: end...
None
如果我们只想获得最终结果,我们可以使用此端点,只需跟踪我们收到的最后一个值
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 la\"}]},
\"stream_mode\": [
\"values\"
]
}" | \
sed 's/\r$//' | \
awk '
/^data:/ {
sub(/^data: /, "", $0)
data_content = $0
}
END {
if (data_content != "") {
print data_content
}
}
'
输出
{
"messages": [
{
"content": "what's the weather in la",
"type": "human",
...
},
{
"type": "ai",
"tool_calls": [
{
"name": "tavily_search_results_json",
"args": {
"query": "weather in los angeles"
},
"id": "toolu_01E5mSaZWm5rWJnCqmt63v4g"
}
],
...
}
{
"content": [
{
"url": "https://www.weatherapi.com/",
"content": "{\"location\": {\"name\": \"Los Angeles\", \"region\": \"California\", \"country\": \"United States of America\", \"lat\": 34.05, \"lon\": -118.24, \"tz_id\": \"America/Los_Angeles\", \"localtime_epoch\": 1716310320, \"localtime\": \"2024-05-21 9:52\"}, \"current\": {\"last_updated_epoch\": 1716309900, \"last_updated\": \"2024-05-21 09:45\", \"temp_c\": 16.7, \"temp_f\": 62.1, \"is_day\": 1, \"condition\": {\"text\": \"Overcast\", \"icon\": \"//cdn.weatherapi.com/weather/64x64/day/122.png\", \"code\": 1009}, \"wind_mph\": 8.1, \"wind_kph\": 13.0, \"wind_degree\": 250, \"wind_dir\": \"WSW\", \"pressure_mb\": 1015.0, \"pressure_in\": 29.97, \"precip_mm\": 0.0, \"precip_in\": 0.0, \"humidity\": 65, \"cloud\": 100, \"feelslike_c\": 16.7, \"feelslike_f\": 62.1, \"vis_km\": 16.0, \"vis_miles\": 9.0, \"uv\": 5.0, \"gust_mph\": 12.5, \"gust_kph\": 20.2}}"
}
],
"type": "tool",
"name": "tavily_search_results_json",
"tool_call_id": "toolu_01E5mSaZWm5rWJnCqmt63v4g"
...
},
{
"content": "Based on the weather API results, the current weather in Los Angeles is overcast with a temperature of around 62°F (17°C). There are light winds from the west-southwest around 8-13 mph. The humidity is 65% and visibility is good at 9 miles. Overall, mild spring weather conditions in LA.",
"type": "ai",
...
}
]
}