Skip to content

如何流式传输图的状态

先决条件

本指南介绍如何使用stream_mode="values",该模式在每个超级步骤中流式传输状态的值。这与使用stream_mode="updates"不同:后者只流式传输每个节点对状态的更新,而前者则在整个超级步骤中流式传输整个图的状态。

设置

首先让我们设置客户端和线程:

from langgraph_sdk import get_client

client = get_client(url=<DEPLOYMENT_URL>)
# 使用名为"agent"的图
assistant_id = "agent"
# 创建线程
thread = await client.threads.create()
print(thread)
import { Client } from "@langchain/langgraph-sdk";

const client = new Client({ apiUrl: <DEPLOYMENT_URL> });
// 使用名为"agent"的图
const assistantID = "agent";
// 创建线程
const thread = await client.threads.create();
console.log(thread);
curl --request POST \
  --url <DEPLOYMENT_URL>/threads \
  --header 'Content-Type: application/json' \
  --data '{}'

输出:

{
  '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
}

基于值模式的流图

现在我们可以基于值进行流传输,这将在每个节点执行完毕后流传输整个图的状态:

input = {"messages": [{"role": "user", "content": "洛杉矶的天气怎么样"}]}

# 流式传输值
async for chunk in client.runs.stream(
    thread["thread_id"],
    assistant_id, 
    input=input,
    stream_mode="values"
):
    print(f"接收新的事件类型:{chunk.event}...")
    print(chunk.data)
    print("\n\n")
const input = {"messages": [{"role": "user", "content": "洛杉矶的天气怎么样"}]}

const streamResponse = client.runs.stream(
  thread["thread_id"],
  assistantID,
  {
    input,
    streamMode: "values"
  }
);
for await (const chunk of streamResponse) {
  console.log(`接收新的事件类型:${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\": \"洛杉矶的天气怎么样\"}]},
   \"stream_mode\": [
     \"values\"
   ]
 }" | \
 sed 's/\r$//' | \
 awk '
 /^event:/ {
     if (data_content != "") {
         print data_content "\n"
     }
     sub(/^event: /, "接收事件类型:", $0)
     printf "%s...\n", $0
     data_content = ""
 }
 /^data:/ {
     sub(/^data: /, "", $0)
     data_content = $0
 }
 END {
     if (data_content != "") {
         print data_content "\n"
     }
 }
 ' 

输出:

接收事件类型:metadata...
{"run_id": "f08791ce-0a3d-44e0-836c-ff62cd2e2786"}



接收事件类型:values...
{
  "messages": [
    {
      "role": "human",
      "content": "洛杉矶的天气怎么样"
    }
  ]
}



接收事件类型:values...
{
  "messages": [
    {
      "content": "洛杉矶的天气怎么样",
      "type": "human",
      ...
    },
    {
      "content": "",
      "type": "ai",
      "tool_calls": [
        {
          "name": "tavily_search_results_json",
          "args": {
            "query": "weather in los angeles"
          },
          "id": "toolu_01E5mSaZWm5rWJnCqmt63v4g"
        }
      ],
      ...
    }
  ]
}

...

接收事件类型:values...
{
  "messages": [
    {
      "content": "洛杉矶的天气怎么样",
      "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": "根据天气API结果,洛杉矶当前的天气是多云,气温约为62°F(17°C)。有来自西西南方向的轻风,风速为8-13英里每小时。湿度为65%,能见度良好,为9英里。总体来说,洛杉矶春季温和的天气条件。",
      "type": "ai",
      ...
    }
  ]
}



接收事件类型:end...
None

如果我们只想获取最终结果,可以使用此端点并跟踪我们收到的最后一个值。

final_answer = None
async for chunk in client.runs.stream(
    thread["thread_id"],
    assistant_id,
    input=input,
    stream_mode="values"
):
    if chunk.event == "values":
        final_answer = chunk.data
let finalAnswer;
const streamResponse = client.runs.stream(
  thread["thread_id"],
  assistantID,
  {
    input,
    streamMode: "values"
  }
);
for await (const chunk of streamResponse) {
  finalAnswer = chunk.data;
}
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\": \"洛杉矶的天气怎么样\"}]},
   \"stream_mode\": [
     \"values\"
   ]
 }" | \
 sed 's/\r$//' | \
 awk '
 /^data:/ { 
     sub(/^data: /, "", $0)   
     data_content = $0          
 }    
 END {                                               
     if (data_content != "") {
         print data_content
     }
 }         
 '

输出:

{
  "messages": [
    {
      "content": "洛杉矶的天气怎么样",
      "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": "根据天气API结果,洛杉矶当前的天气是多云,气温约为62°F(17°C)。有来自西西南方向的轻风,风速为8-13英里每小时。湿度为65%,能见度良好,为9英里。总体来说,洛杉矶春季温和的天气条件。",
      "type": "ai",
      ...
    }
  ]
}

Comments