langgraph中graph的astream(stream)方法分别实现异步(同步)流式应答,在langgraph-api服务也是核心方法,实现与前端的对接,必须要把这个方法弄明白。该方法中最重要的参数是stream_mode,本文首先通过运行程序把stream_mode的每种类型及组合分析清楚,然后说明langgraph-api中如何基于astream的返回,包装成流失应答返回前端。

      1.测试代码

        仍使用已有的简单的带搜索工具的chatbot,具体代码如下:

from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph.message import add_messages
from langchain_core.messages import ToolMessage
from langchain_core.tools import InjectedToolCallId, tool
from langgraph.types import Command, interrupt
import os
from langchain_openai import ChatOpenAI
from typing_extensions import TypedDict
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langchain_tavily import TavilySearch
from langgraph.prebuilt import ToolNode, tools_condition

os.environ["TAVILY_API_KEY"] = "tvly-……"
class State(TypedDict):
    messages: Annotated[list, add_messages]
graph_builder = StateGraph(State)

llm = ChatOpenAI(
    model = 'qwen-plus',
    api_key = "sk-……",
    base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1")


tool = TavilySearch(max_results=2)
tools = [tool]

llm_with_tools = llm.bind_tools(tools)


def chatbot(state: State):#定义chatbot节点
    message = llm_with_tools.invoke(state["messages"])
    assert len(message.tool_calls) <= 1
    return {"messages": [message]}

graph_builder.add_node("chatbot", chatbot)#增加chatbot节点到工作流图

tool_node = ToolNode(tools=tools) #生成工具节点checkpointer (type <class 'langgraph.checkpoint.memory.InMemorySaver'>). With LangGraph API, persistence is handled automatically by the platform, so providing a custom checkpointer (type <class 'langgraph.checkpoint.memory.InMemorySaver'>) here isn't necessary and will be ignored when deployed.

graph_builder.add_node("tools", tool_node) #把工具节点增加到工作流图中

graph_builder.add_conditional_edges( "chatbot", tools_condition,)#增加条件边
graph_builder.add_edge("tools", "chatbot")#增加从tools—>chatbot的边
graph_builder.add_edge(START, "chatbot")#增加从START—>chatbot的边

graph = graph_builder.compile()

       工作流图如下:

      2.stream_mode解析

      2.1方法简介

        根据官网文档,astream方法声明如下:

astream(
    input: InputT | Command | None,
    config: RunnableConfig | None = None,
    *,
    context: ContextT | None = None,
    stream_mode: (
        StreamMode | Sequence[StreamMode] | None
    ) = None,
    print_mode: StreamMode | Sequence[StreamMode] = (),
    output_keys: str | Sequence[str] | None = None,
    interrupt_before: All | Sequence[str] | None = None,
    interrupt_after: All | Sequence[str] | None = None,
    durability: Durability | None = None,
    subgraphs: bool = False,
    debug: bool | None = None,
    **kwargs: Unpack[DeprecatedKwargs]
) -> AsyncIterator[dict[str, Any] | Any]

        astream(stream)共有5中模式:分别是values、updates、debug、messages和custom。其中:

  values:输出图中每个节点执行完成后的状态值

  updates:仅输出每个节点执行后对状态的更新值

  debug:输出数据在图中流转过程中的调试数据

  messages:流式输出在节点内部调用大模型或工具时返回值

  custom:定制在节点内部执行过程中的流式输出

      2.2values

        执行以下代码,观察输出:

user_inputs = "who is current president of American?"
inputs = {"messages": [{"role": "user", "content": user_input}]}
async for event in graph.astream(input=inputs, stream_mode="values"):
    print(event)
    print("\n")

        输出内容如下:

#第一条数据是执行完第一个节点(也就是START节点)后的状态值,该值作为下一个节点也就是chatbot节点的输出。可见其中仅有用户输入的问题。

{'messages': [HumanMessage(content='who is current president of American?', additional_kwargs={}, response_metadata={}, id='98464c2a-9063-493f-a7bf-67b8f3fb5e3a')]}

#第二条数据是第一次(也就是从START进入)执行完chatbot节点后的状态值,该值作为下一个节点(也就是tools节点)的输入。可以看到其中增加了function calling的结果数据
{'messages': [HumanMessage(content='who is current president of American?', additional_kwargs={}, response_metadata={}, id='98464c2a-9063-493f-a7bf-67b8f3fb5e3a'), AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_0fff6bdff29b48fb99dadf', 'function': {'arguments': '{"query": "current president of the United States"}', 'name': 'tavily_search'}, 'type': 'function', 'index': 0}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 1809, 'total_tokens': 1835, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'qwen-plus', 'system_fingerprint': None, 'id': 'chatcmpl-67476768-8eea-4168-b2b3-6bd4046c3270', 'service_tier': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run--4fc80709-61e7-4537-8ef9-7ed9b26e81a0-0', tool_calls=[{'name': 'tavily_search', 'args': {'query': 'current president of the United States'}, 'id': 'call_0fff6bdff29b48fb99dadf', 'type': 'tool_call'}], usage_metadata={'input_tokens': 1809, 'output_tokens': 26, 'total_tokens': 1835, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}})]}

#第三条数据是执行完tools节点后的状态值。可以看到,是在原有数据基础上增加了搜索结果,该数据作为下一节点(也就是chatbot节点)的输入
{'messages': [HumanMessage(content='who is current president of American?', additional_kwargs={}, response_metadata={}, id='98464c2a-9063-493f-a7bf-67b8f3fb5e3a'), AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_0fff6bdff29b48fb99dadf', 'function': {'arguments': '{"query": "current president of the United States"}', 'name': 'tavily_search'}, 'type': 'function', 'index': 0}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 1809, 'total_tokens': 1835, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'qwen-plus', 'system_fingerprint': None, 'id': 'chatcmpl-67476768-8eea-4168-b2b3-6bd4046c3270', 'service_tier': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run--4fc80709-61e7-4537-8ef9-7ed9b26e81a0-0', tool_calls=[{'name': 'tavily_search', 'args': {'query': 'current president of the United States'}, 'id': 'call_0fff6bdff29b48fb99dadf', 'type': 'tool_call'}], usage_metadata={'input_tokens': 1809, 'output_tokens': 26, 'total_tokens': 1835, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}}), ToolMessage(content='{"query": "current president of the United States", "follow_up_questions": null, "answer": null, "images": [], "results": [{"url": "https://www.usa.gov/presidents", "title": "Presidents, vice presidents, and first ladies | USAGov", "content": "The 47th and current president of the United States is Donald John Trump. He was sworn into office on January 20, 2025.", "score": 0.8952393, "raw_content": null}, {"url": "https://en.wikipedia.org/wiki/President_of_the_United_States", "title": "President of the United States", "content": "In all, 45 individuals have served 47 presidencies spanning 60 four-year terms. Donald Trump is the 47th and current president since January 20, 2025.", "score": 0.8430832, "raw_content": null}], "response_time": 1.09, "request_id": "847d6368-3cc4-4376-bdea-0d0a732254ba"}', name='tavily_search', id='f01a5d9f-ea5a-4cc4-a74f-ed0f69289371', tool_call_id='call_0fff6bdff29b48fb99dadf')]}

#第4条数据是第二次执行完chatbot节点后的状态值,可也看到是在第3条数据基础上增加了大模型最后的应答数据。该数据作为END节点的输入。
{'messages': [HumanMessage(content='who is current president of American?', additional_kwargs={}, response_metadata={}, id='98464c2a-9063-493f-a7bf-67b8f3fb5e3a'), AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_0fff6bdff29b48fb99dadf', 'function': {'arguments': '{"query": "current president of the United States"}', 'name': 'tavily_search'}, 'type': 'function', 'index': 0}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 1809, 'total_tokens': 1835, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'qwen-plus', 'system_fingerprint': None, 'id': 'chatcmpl-67476768-8eea-4168-b2b3-6bd4046c3270', 'service_tier': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run--4fc80709-61e7-4537-8ef9-7ed9b26e81a0-0', tool_calls=[{'name': 'tavily_search', 'args': {'query': 'current president of the United States'}, 'id': 'call_0fff6bdff29b48fb99dadf', 'type': 'tool_call'}], usage_metadata={'input_tokens': 1809, 'output_tokens': 26, 'total_tokens': 1835, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}}), ToolMessage(content='{"query": "current president of the United States", "follow_up_questions": null, "answer": null, "images": [], "results": [{"url": "https://www.usa.gov/presidents", "title": "Presidents, vice presidents, and first ladies | USAGov", "content": "The 47th and current president of the United States is Donald John Trump. He was sworn into office on January 20, 2025.", "score": 0.8952393, "raw_content": null}, {"url": "https://en.wikipedia.org/wiki/President_of_the_United_States", "title": "President of the United States", "content": "In all, 45 individuals have served 47 presidencies spanning 60 four-year terms. Donald Trump is the 47th and current president since January 20, 2025.", "score": 0.8430832, "raw_content": null}], "response_time": 1.09, "request_id": "847d6368-3cc4-4376-bdea-0d0a732254ba"}', name='tavily_search', id='f01a5d9f-ea5a-4cc4-a74f-ed0f69289371', tool_call_id='call_0fff6bdff29b48fb99dadf'), AIMessage(content='The current president of the United States is Donald John Trump, who was sworn into office on January 20, 2025.', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 29, 'prompt_tokens': 2114, 'total_tokens': 2143, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'qwen-plus', 'system_fingerprint': None, 'id': 'chatcmpl-eb711b57-311b-49a3-80b7-9c63035b988a', 'service_tier': None, 'finish_reason': 'stop', 'logprobs': None}, id='run--f824483c-b1c9-4db9-96a8-29adfe4f455e-0', usage_metadata={'input_tokens': 2114, 'output_tokens': 29, 'total_tokens': 2143, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}})]}

      2.3updates

        执行以下代码,观察输出:

user_inputs = "who is current president of American?"
inputs = {"messages": [{"role": "user", "content": user_input}]}
async for event in graph.astream(input=inputs, stream_mode="updates"):
    print(event)
    print("\n")

       输出内容如下:

#第1条数据是chatbot节点对状态的更新数据,这里就是function calling结果

{'chatbot': {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_f57b24a4fb0f439fa61d27', 'function': {'arguments': '{"query": "current president of the United States"}', 'name': 'tavily_search'}, 'type': 'function', 'index': 0}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 1809, 'total_tokens': 1835, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'qwen-plus', 'system_fingerprint': None, 'id': 'chatcmpl-b64c587f-4894-4388-8056-bf72868f8c7f', 'service_tier': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run--77a1894c-cf29-4790-a0ca-3526bab41e14-0', tool_calls=[{'name': 'tavily_search', 'args': {'query': 'current president of the United States'}, 'id': 'call_f57b24a4fb0f439fa61d27', 'type': 'tool_call'}], usage_metadata={'input_tokens': 1809, 'output_tokens': 26, 'total_tokens': 1835, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}})]}}

#第2条数据是tools节点对于状态的更新数据,这里就是搜索结果
{'tools': {'messages': [ToolMessage(content='{"query": "current president of the United States", "follow_up_questions": null, "answer": null, "images": [], "results": [{"url": "https://www.usa.gov/presidents", "title": "Presidents, vice presidents, and first ladies | USAGov", "content": "The 47th and current president of the United States is Donald John Trump. He was sworn into office on January 20, 2025.", "score": 0.8952393, "raw_content": null}, {"url": "https://en.wikipedia.org/wiki/President_of_the_United_States", "title": "President of the United States", "content": "In all, 45 individuals have served 47 presidencies spanning 60 four-year terms. Donald Trump is the 47th and current president since January 20, 2025.", "score": 0.8430832, "raw_content": null}], "response_time": 0.89, "request_id": "d7d99787-8b26-4215-8e64-f6a150a6e448"}', name='tavily_search', id='46e3caa2-1317-4484-84d6-b7a37acacd88', tool_call_id='call_f57b24a4fb0f439fa61d27')]}}

#第3条数据是chatbot第二次执行时对状态的更新数据,这里就是最终的大模型应答
{'chatbot': {'messages': [AIMessage(content='The current president of the United States is Donald John Trump, who was sworn into office on January 20, 2025.', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 29, 'prompt_tokens': 2118, 'total_tokens': 2147, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'qwen-plus', 'system_fingerprint': None, 'id': 'chatcmpl-2d5c4b32-84a3-41ac-b67a-bf177e1dd9dc', 'service_tier': None, 'finish_reason': 'stop', 'logprobs': None}, id='run--48e403b7-ffd2-44d2-90b6-29cbe603ebef-0', usage_metadata={'input_tokens': 2118, 'output_tokens': 29, 'total_tokens': 2147, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}})]}}
 

      2.4debug

        执行以下代码,观察输出:

user_inputs = "who is current president of American?"
inputs = {"messages": [{"role": "user", "content": user_input}]}
async for event in graph.astream(input=inputs, stream_mode="debug"):
    print(event)
    print("\n")

        输出内容如下:

#在输出中step表明执行的第几步,name表明该步对应的节点。每个节点对应两条数据,第1条为执行内部处理的输入,对应的type为task;第2条为内部处理的输出,对应type为task_result。第1条数据中payload内的input取自状态中的最后一个数据。第2条数据中payload中的result为处理结果

#第一次进入chatbot后,生成使用大模型function calling的请求数据

{'step': 1, 'timestamp': '2025-09-12T08:48:48.678450+00:00', 'type': 'task', 'payload': {'id': 'da4aa395-26c1-3f04-d2e7-642a3875bddd', 'name': 'chatbot', 'input': {'messages': [HumanMessage(content='who is current president of American?', additional_kwargs={}, response_metadata={}, id='3d0d10db-e47d-4a80-8f5d-ddee9c565c56')]}, 'triggers': ('branch:to:chatbot',)}}

#第一次进入chatbot后,调用大模型function calling生成的结果数据
{'step': 1, 'timestamp': '2025-09-12T08:48:49.920693+00:00', 'type': 'task_result', 'payload': {'id': 'da4aa395-26c1-3f04-d2e7-642a3875bddd', 'name': 'chatbot', 'error': None, 'result': [('messages', [AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_466560f326514d6087802a', 'function': {'arguments': '{"query": "current president of the United States"}', 'name': 'tavily_search'}, 'type': 'function', 'index': 0}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 1809, 'total_tokens': 1835, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'qwen-plus', 'system_fingerprint': None, 'id': 'chatcmpl-76c50100-c878-4e4c-91e6-cfee592fa7e3', 'service_tier': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run--2628b086-1816-4f82-b83a-aa930eceee61-0', tool_calls=[{'name': 'tavily_search', 'args': {'query': 'current president of the United States'}, 'id': 'call_466560f326514d6087802a', 'type': 'tool_call'}], usage_metadata={'input_tokens': 1809, 'output_tokens': 26, 'total_tokens': 1835, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}})])], 'interrupts': []}}

#在tools节点生成调用搜索引擎的请求数据
{'step': 2, 'timestamp': '2025-09-12T08:48:49.921121+00:00', 'type': 'task', 'payload': {'id': '1c3c56aa-cb8b-2c48-e0df-c6cca6d12925', 'name': 'tools', 'input': {'messages': [HumanMessage(content='who is current president of American?', additional_kwargs={}, response_metadata={}, id='3d0d10db-e47d-4a80-8f5d-ddee9c565c56'), AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_466560f326514d6087802a', 'function': {'arguments': '{"query": "current president of the United States"}', 'name': 'tavily_search'}, 'type': 'function', 'index': 0}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 1809, 'total_tokens': 1835, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'qwen-plus', 'system_fingerprint': None, 'id': 'chatcmpl-76c50100-c878-4e4c-91e6-cfee592fa7e3', 'service_tier': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run--2628b086-1816-4f82-b83a-aa930eceee61-0', tool_calls=[{'name': 'tavily_search', 'args': {'query': 'current president of the United States'}, 'id': 'call_466560f326514d6087802a', 'type': 'tool_call'}], usage_metadata={'input_tokens': 1809, 'output_tokens': 26, 'total_tokens': 1835, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}})]}, 'triggers': ('branch:to:tools',)}}

#在tools节点内容调用搜索引擎后获取的结果数据
{'step': 2, 'timestamp': '2025-09-12T08:48:51.618077+00:00', 'type': 'task_result', 'payload': {'id': '1c3c56aa-cb8b-2c48-e0df-c6cca6d12925', 'name': 'tools', 'error': None, 'result': [('messages', [ToolMessage(content='{"query": "current president of the United States", "follow_up_questions": null, "answer": null, "images": [], "results": [{"url": "https://www.usa.gov/presidents", "title": "Presidents, vice presidents, and first ladies | USAGov", "content": "The 47th and current president of the United States is Donald John Trump. He was sworn into office on January 20, 2025.", "score": 0.8952393, "raw_content": null}, {"url": "https://en.wikipedia.org/wiki/President_of_the_United_States", "title": "President of the United States", "content": "In all, 45 individuals have served 47 presidencies spanning 60 four-year terms. Donald Trump is the 47th and current president since January 20, 2025.", "score": 0.8430832, "raw_content": null}], "response_time": 0.8, "request_id": "50e0e516-c2e0-4df7-8f1b-4836d07189f3"}', name='tavily_search', id='ee9fe717-7ce6-4a56-9399-08eb8832f30e', tool_call_id='call_466560f326514d6087802a')])], 'interrupts': []}}

#第二次进入chtboth后,调用大模型生成应答结果的请求数据
{'step': 3, 'timestamp': '2025-09-12T08:48:51.618485+00:00', 'type': 'task', 'payload': {'id': '6830e13a-d91f-8103-6bcd-94fa79bfc282', 'name': 'chatbot', 'input': {'messages': [HumanMessage(content='who is current president of American?', additional_kwargs={}, response_metadata={}, id='3d0d10db-e47d-4a80-8f5d-ddee9c565c56'), AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_466560f326514d6087802a', 'function': {'arguments': '{"query": "current president of the United States"}', 'name': 'tavily_search'}, 'type': 'function', 'index': 0}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 26, 'prompt_tokens': 1809, 'total_tokens': 1835, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'qwen-plus', 'system_fingerprint': None, 'id': 'chatcmpl-76c50100-c878-4e4c-91e6-cfee592fa7e3', 'service_tier': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run--2628b086-1816-4f82-b83a-aa930eceee61-0', tool_calls=[{'name': 'tavily_search', 'args': {'query': 'current president of the United States'}, 'id': 'call_466560f326514d6087802a', 'type': 'tool_call'}], usage_metadata={'input_tokens': 1809, 'output_tokens': 26, 'total_tokens': 1835, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}}), ToolMessage(content='{"query": "current president of the United States", "follow_up_questions": null, "answer": null, "images": [], "results": [{"url": "https://www.usa.gov/presidents", "title": "Presidents, vice presidents, and first ladies | USAGov", "content": "The 47th and current president of the United States is Donald John Trump. He was sworn into office on January 20, 2025.", "score": 0.8952393, "raw_content": null}, {"url": "https://en.wikipedia.org/wiki/President_of_the_United_States", "title": "President of the United States", "content": "In all, 45 individuals have served 47 presidencies spanning 60 four-year terms. Donald Trump is the 47th and current president since January 20, 2025.", "score": 0.8430832, "raw_content": null}], "response_time": 0.8, "request_id": "50e0e516-c2e0-4df7-8f1b-4836d07189f3"}', name='tavily_search', id='ee9fe717-7ce6-4a56-9399-08eb8832f30e', tool_call_id='call_466560f326514d6087802a')]}, 'triggers': ('branch:to:chatbot',)}}

#第二次进入chatbot后,调用大模型后生成应答结果
{'step': 3, 'timestamp': '2025-09-12T08:48:53.616934+00:00', 'type': 'task_result', 'payload': {'id': '6830e13a-d91f-8103-6bcd-94fa79bfc282', 'name': 'chatbot', 'error': None, 'result': [('messages', [AIMessage(content='The current president of the United States is Donald John Trump, who was sworn into office on January 20, 2025.', additional_kwargs={'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 29, 'prompt_tokens': 2116, 'total_tokens': 2145, 'completion_tokens_details': None, 'prompt_tokens_details': {'audio_tokens': None, 'cached_tokens': 0}}, 'model_name': 'qwen-plus', 'system_fingerprint': None, 'id': 'chatcmpl-5dccdde8-786c-499e-82d3-cbf161eb0bb0', 'service_tier': None, 'finish_reason': 'stop', 'logprobs': None}, id='run--79a8ec21-9f6c-44cd-b995-3238e6e0ba02-0', usage_metadata={'input_tokens': 2116, 'output_tokens': 29, 'total_tokens': 2145, 'input_token_details': {'cache_read': 0}, 'output_token_details': {}})])], 'interrupts': []}}

      2.5message

        执行以下代码,观察输出:

user_inputs = "who is current president of American?"
inputs = {"messages": [{"role": "user", "content": user_input}]}
async for event in graph.astream(input=inputs, stream_mode="messages"):
    print(event)
    print("\n")

        输出如下:

#以下输出中,前5条数据是chatbot使用大模型的function calling时生成的流式输出,其中:

1)四条数据中的 id='run--f8018e47-95b9-4f02-9fe4-4145db863670'是相同的,表明是同一个请求

2) {'langgraph_step': 1, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',)说明执行的是图的第一步,节点为chatbot

(AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_3bbc32f8b9e843149aa225', 'function': {'arguments': '{"query', 'name': 'tavily_search'}, 'type': 'function'}]}, response_metadata={}, id='run--f8018e47-95b9-4f02-9fe4-4145db863670', tool_calls=[{'name': 'tavily_search', 'args': {}, 'id': 'call_3bbc32f8b9e843149aa225', 'type': 'tool_call'}], tool_call_chunks=[{'name': 'tavily_search', 'args': '{"query', 'id': 'call_3bbc32f8b9e843149aa225', 'index': 0, 'type': 'tool_call_chunk'}]), {'langgraph_step': 1, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:1660097c-5bed-98aa-fbed-4322b6676a87', 'checkpoint_ns': 'chatbot:1660097c-5bed-98aa-fbed-4322b6676a87', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None})


(AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': '', 'function': {'arguments': '": "current president of the', 'name': None}, 'type': 'function'}]}, response_metadata={}, id='run--f8018e47-95b9-4f02-9fe4-4145db863670', invalid_tool_calls=[{'name': None, 'args': '": "current president of the', 'id': '', 'error': None, 'type': 'invalid_tool_call'}], tool_call_chunks=[{'name': None, 'args': '": "current president of the', 'id': '', 'index': 0, 'type': 'tool_call_chunk'}]), {'langgraph_step': 1, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:1660097c-5bed-98aa-fbed-4322b6676a87', 'checkpoint_ns': 'chatbot:1660097c-5bed-98aa-fbed-4322b6676a87', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None})


(AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': '', 'function': {'arguments': ' United States"}', 'name': None}, 'type': 'function'}]}, response_metadata={}, id='run--f8018e47-95b9-4f02-9fe4-4145db863670', invalid_tool_calls=[{'name': None, 'args': ' United States"}', 'id': '', 'error': None, 'type': 'invalid_tool_call'}], tool_call_chunks=[{'name': None, 'args': ' United States"}', 'id': '', 'index': 0, 'type': 'tool_call_chunk'}]), {'langgraph_step': 1, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:1660097c-5bed-98aa-fbed-4322b6676a87', 'checkpoint_ns': 'chatbot:1660097c-5bed-98aa-fbed-4322b6676a87', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None})


(AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': '', 'function': {'arguments': None, 'name': None}, 'type': 'function'}]}, response_metadata={}, id='run--f8018e47-95b9-4f02-9fe4-4145db863670', tool_calls=[{'name': '', 'args': {}, 'id': '', 'type': 'tool_call'}], tool_call_chunks=[{'name': None, 'args': None, 'id': '', 'index': 0, 'type': 'tool_call_chunk'}]), {'langgraph_step': 1, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:1660097c-5bed-98aa-fbed-4322b6676a87', 'checkpoint_ns': 'chatbot:1660097c-5bed-98aa-fbed-4322b6676a87', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None})


(AIMessageChunk(content='', additional_kwargs={}, response_metadata={'finish_reason': 'tool_calls', 'model_name': 'qwen-plus'}, id='run--f8018e47-95b9-4f02-9fe4-4145db863670'), {'langgraph_step': 1, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:1660097c-5bed-98aa-fbed-4322b6676a87', 'checkpoint_ns': 'chatbot:1660097c-5bed-98aa-fbed-4322b6676a87', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None})

#这是调用搜索工具后生成的流式应答,其中:

{'langgraph_step': 2, 'langgraph_node': 'tools', 'langgraph_triggers': ('branch:to:tools',)说明执行搜索的节点是tools,并且是图中的第2步
(ToolMessage(content='{"query": "current president of the United States", "follow_up_questions": null, "answer": null, "images": [], "results": [{"url": "https://www.usa.gov/presidents", "title": "Presidents, vice presidents, and first ladies | USAGov", "content": "The 47th and current president of the United States is Donald John Trump. He was sworn into office on January 20, 2025.", "score": 0.8952393, "raw_content": null}, {"url": "https://en.wikipedia.org/wiki/President_of_the_United_States", "title": "President of the United States", "content": "In all, 45 individuals have served 47 presidencies spanning 60 four-year terms. Donald Trump is the 47th and current president since January 20, 2025.", "score": 0.8430832, "raw_content": null}], "response_time": 0.86, "request_id": "b5a38bdb-10ad-4cc6-a83c-c51ba104ad53"}', name='tavily_search', id='91c4963d-1668-4879-baac-1e84ef564de5', tool_call_id='call_3bbc32f8b9e843149aa225'), {'langgraph_step': 2, 'langgraph_node': 'tools', 'langgraph_triggers': ('branch:to:tools',), 'langgraph_path': ('__pregel_pull', 'tools'), 'langgraph_checkpoint_ns': 'tools:958d3f36-ea68-b1dd-8c27-2b85f282dec3'})

#以下10条数据时携带搜索结果调用大模型后生成的流式应答,其中:

1)id='run--df8107ff-d3d7-4221-a9bd-f14e6a534217'在这10条数据中相同,表明是同一个应答

2) {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',)说明执行的是图中第3步,执行节点为chatbot
(AIMessageChunk(content='The', additional_kwargs={}, response_metadata={}, id='run--df8107ff-d3d7-4221-a9bd-f14e6a534217'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None})


(AIMessageChunk(content=' current', additional_kwargs={}, response_metadata={}, id='run--df8107ff-d3d7-4221-a9bd-f14e6a534217'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None})


(AIMessageChunk(content=' president', additional_kwargs={}, response_metadata={}, id='run--df8107ff-d3d7-4221-a9bd-f14e6a534217'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None})


(AIMessageChunk(content=' of the United', additional_kwargs={}, response_metadata={}, id='run--df8107ff-d3d7-4221-a9bd-f14e6a534217'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None})


(AIMessageChunk(content=' States is Donald John', additional_kwargs={}, response_metadata={}, id='run--df8107ff-d3d7-4221-a9bd-f14e6a534217'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None})


(AIMessageChunk(content=' Trump, who was', additional_kwargs={}, response_metadata={}, id='run--df8107ff-d3d7-4221-a9bd-f14e6a534217'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None})


(AIMessageChunk(content=' sworn into office on', additional_kwargs={}, response_metadata={}, id='run--df8107ff-d3d7-4221-a9bd-f14e6a534217'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None})


(AIMessageChunk(content=' January 20, ', additional_kwargs={}, response_metadata={}, id='run--df8107ff-d3d7-4221-a9bd-f14e6a534217'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None})


(AIMessageChunk(content='2025.', additional_kwargs={}, response_metadata={}, id='run--df8107ff-d3d7-4221-a9bd-f14e6a534217'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None})


(AIMessageChunk(content='', additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'qwen-plus'}, id='run--df8107ff-d3d7-4221-a9bd-f14e6a534217'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'checkpoint_ns': 'chatbot:4ba85f70-4ea1-7b5e-b69a-be3913d9cd34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None})

        采用如下代码看输出更加一步了然,其中的token是大模型或工具返回的数据,metadata是langgraph追加的数据。

user_inputs = "who is current president of American?"
inputs = {"messages": [{"role": "user", "content": user_input}]}
#'metadata', 'messages', 'debug', 'updates', 'values', 'custom',  output_keys=['messages']
async for token, metadata in graph.astream(input=inputs, stream_mode="messages"):
    print(token)
    print("############\n")
    print(metadata)
    print("@@@@@@@@@@@@@\n")

        输出如下:

content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_3875906f53154ec68cbed1', 'function': {'arguments': '{"query', 'name': 'tavily_search'}, 'type': 'function'}]} response_metadata={} id='run--fa569299-8678-4876-b93c-ca12daa1ab7d' tool_calls=[{'name': 'tavily_search', 'args': {}, 'id': 'call_3875906f53154ec68cbed1', 'type': 'tool_call'}] tool_call_chunks=[{'name': 'tavily_search', 'args': '{"query', 'id': 'call_3875906f53154ec68cbed1', 'index': 0, 'type': 'tool_call_chunk'}]
############

{'langgraph_step': 1, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:a6ab221d-9d90-0b3a-ed77-32b58926ee68', 'checkpoint_ns': 'chatbot:a6ab221d-9d90-0b3a-ed77-32b58926ee68', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}
@@@@@@@@@@@@@

content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': '', 'function': {'arguments': '": "current president of the', 'name': None}, 'type': 'function'}]} response_metadata={} id='run--fa569299-8678-4876-b93c-ca12daa1ab7d' invalid_tool_calls=[{'name': None, 'args': '": "current president of the', 'id': '', 'error': None, 'type': 'invalid_tool_call'}] tool_call_chunks=[{'name': None, 'args': '": "current president of the', 'id': '', 'index': 0, 'type': 'tool_call_chunk'}]
############

{'langgraph_step': 1, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:a6ab221d-9d90-0b3a-ed77-32b58926ee68', 'checkpoint_ns': 'chatbot:a6ab221d-9d90-0b3a-ed77-32b58926ee68', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}
@@@@@@@@@@@@@

content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': '', 'function': {'arguments': ' United States"}', 'name': None}, 'type': 'function'}]} response_metadata={} id='run--fa569299-8678-4876-b93c-ca12daa1ab7d' invalid_tool_calls=[{'name': None, 'args': ' United States"}', 'id': '', 'error': None, 'type': 'invalid_tool_call'}] tool_call_chunks=[{'name': None, 'args': ' United States"}', 'id': '', 'index': 0, 'type': 'tool_call_chunk'}]
############

{'langgraph_step': 1, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:a6ab221d-9d90-0b3a-ed77-32b58926ee68', 'checkpoint_ns': 'chatbot:a6ab221d-9d90-0b3a-ed77-32b58926ee68', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}
@@@@@@@@@@@@@

content='' additional_kwargs={} response_metadata={'finish_reason': 'tool_calls', 'model_name': 'qwen-plus'} id='run--fa569299-8678-4876-b93c-ca12daa1ab7d'
############

{'langgraph_step': 1, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:a6ab221d-9d90-0b3a-ed77-32b58926ee68', 'checkpoint_ns': 'chatbot:a6ab221d-9d90-0b3a-ed77-32b58926ee68', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}
@@@@@@@@@@@@@

content='{"query": "current president of the United States", "follow_up_questions": null, "answer": null, "images": [], "results": [{"url": "https://www.usa.gov/presidents", "title": "Presidents, vice presidents, and first ladies | USAGov", "content": "The 47th and current president of the United States is Donald John Trump. He was sworn into office on January 20, 2025.", "score": 0.8952393, "raw_content": null}, {"url": "https://en.wikipedia.org/wiki/President_of_the_United_States", "title": "President of the United States", "content": "In all, 45 individuals have served 47 presidencies spanning 60 four-year terms. Donald Trump is the 47th and current president since January 20, 2025.", "score": 0.8430832, "raw_content": null}], "response_time": 1.19, "request_id": "614ba955-1699-4b92-91ee-1207349f4585"}' name='tavily_search' id='7dfd2357-e2a4-4432-87dc-e9a9d44278a5' tool_call_id='call_3875906f53154ec68cbed1'
############

{'langgraph_step': 2, 'langgraph_node': 'tools', 'langgraph_triggers': ('branch:to:tools',), 'langgraph_path': ('__pregel_pull', 'tools'), 'langgraph_checkpoint_ns': 'tools:31c61b6f-7feb-a1db-9ab0-a356570766f2'}
@@@@@@@@@@@@@

content='The' additional_kwargs={} response_metadata={} id='run--5a0ac932-c4a2-4531-8653-50b7ae41d677'
############

{'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}
@@@@@@@@@@@@@

content=' current' additional_kwargs={} response_metadata={} id='run--5a0ac932-c4a2-4531-8653-50b7ae41d677'
############

{'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}
@@@@@@@@@@@@@

content=' president' additional_kwargs={} response_metadata={} id='run--5a0ac932-c4a2-4531-8653-50b7ae41d677'
############

{'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}
@@@@@@@@@@@@@

content=' of the United' additional_kwargs={} response_metadata={} id='run--5a0ac932-c4a2-4531-8653-50b7ae41d677'
############

{'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}
@@@@@@@@@@@@@

content=' States is Donald John' additional_kwargs={} response_metadata={} id='run--5a0ac932-c4a2-4531-8653-50b7ae41d677'
############

{'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}
@@@@@@@@@@@@@

content=' Trump, who was' additional_kwargs={} response_metadata={} id='run--5a0ac932-c4a2-4531-8653-50b7ae41d677'
############

{'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}
@@@@@@@@@@@@@

content=' sworn into office on' additional_kwargs={} response_metadata={} id='run--5a0ac932-c4a2-4531-8653-50b7ae41d677'
############

{'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}
@@@@@@@@@@@@@

content=' January 20, ' additional_kwargs={} response_metadata={} id='run--5a0ac932-c4a2-4531-8653-50b7ae41d677'
############

{'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}
@@@@@@@@@@@@@

content='2025.' additional_kwargs={} response_metadata={} id='run--5a0ac932-c4a2-4531-8653-50b7ae41d677'
############

{'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}
@@@@@@@@@@@@@

content='' additional_kwargs={} response_metadata={'finish_reason': 'stop', 'model_name': 'qwen-plus'} id='run--5a0ac932-c4a2-4531-8653-50b7ae41d677'
############

{'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'checkpoint_ns': 'chatbot:95333fea-dace-e8a4-26a4-4842dcf55f73', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}
@@@@@@@@@@@@@

      2.6custom

        custom模式下,直接在节点内部定义需要返回的结果数据,而不需要中间结果数据,为程序提供了更大的灵活性。

        此时,需要修改图中节点代码,具体如下:

from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph.message import add_messages
from langchain_core.messages import ToolMessage
from langchain_core.tools import InjectedToolCallId, tool
from langgraph.types import Command, interrupt
import os
from langchain_openai import ChatOpenAI
from typing_extensions import TypedDict
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langchain_tavily import TavilySearch
from langgraph.prebuilt import ToolNode, tools_condition
from typing import Annotated, Literal
from langgraph.config import get_stream_writer

os.environ["TAVILY_API_KEY"] = "tvly-……"
class State(TypedDict):
    messages: Annotated[list, add_messages]
graph_builder = StateGraph(State)

llm = ChatOpenAI(
    model = 'qwen-plus',
    api_key = "sk-……",
    base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1")


tool = TavilySearch(max_results=2)
tools = [tool]

llm_with_tools = llm.bind_tools(tools)

#重点代码。必须要增加类似的方法

def generate_custom_stream(type: Literal["think","normal"], content: str):
    content = "\n"+content+"\n"
    custom_stream_writer = get_stream_writer()
    return custom_stream_writer({type:content})


def chatbot(state: State):#定义chatbot节点
    think_response = llm_with_tools.invoke(["Please reasoning:"] + state["messages"])
    normal_response = llm_with_tools.invoke(state["messages"])
   #以下两行代码直接生成返回的流数据,在遍历时会接收到

    generate_custom_stream("think", think_response.content)
    generate_custom_stream("normal", normal_response.content)
    return {"messages": [normal_response]}


graph_builder.add_node("chatbot", chatbot)#增加chatbot节点到工作流图

tool_node = ToolNode(tools=tools) #生成工具节点checkpointer (type <class 'langgraph.checkpoint.memory.InMemorySaver'>). With LangGraph API, persistence is handled automatically by the platform, so providing a custom checkpointer (type <class 'langgraph.checkpoint.memory.InMemorySaver'>) here isn't necessary and will be ignored when deployed.

graph_builder.add_node("tools", tool_node) #把工具节点增加到工作流图中

graph_builder.add_conditional_edges( "chatbot", tools_condition,)#增加条件边
graph_builder.add_edge("tools", "chatbot")#增加从tools—>chatbot的边
graph_builder.add_edge(START, "chatbot")#增加从START—>chatbot的边

graph = graph_builder.compile()
graph.name

        运行以下代码,查看输出结果:

user_input = "who is current president of American?"
inputs = {"messages": [{"role": "user", "content": user_input}]}
async for event in graph.astream(input=inputs, stream_mode="custom"):
    print(event)
    print("\n")

        输出结果如下:

#可见仅返回了思考数据和结果数据

{'think': '\n\n'}


{'normal': '\n\n'}


{'think': '\nThe current president of the United States is Donald John Trump, who was sworn into office on January 20, 2025.\n'}


{'normal': '\nThe 47th and current president of the United States is Donald John Trump. He was sworn into office on January 20, 2025.\n'}

      2.7混合模式

        stream_mode参数还可以是以上多种模式的组合,此时输出中会增加一个模式字段,表明该条数据的stream_mode。

        运行如下代码,并查看输出:

#因为在langgraph-api中采用了5种模式的组合,所以这里也用这5中模式做测试

user_inputs = "who is current president of American?"
inputs = {"messages": [{"role": "user", "content": user_input}]}
async for event in graph.astream(input=inputs, stream_mode=['messages', 'debug', 'updates', 'values', 'custom'], subgraphs=True):
    print(event)
    print("\n")

        输出内容如下:

        每条数据中的第一项则表明是哪类数据,从数据中可以看到五种类型的数据全部出现。数据的具体内容与前面每一种模式的数据分别保持一致,不再赘述。

((), 'values', {'messages': [HumanMessage(content='who is current president of American?', additional_kwargs={}, response_metadata={}, id='669fd68b-d54c-4d9c-8a85-965029c349d3')]})


((), 'debug', {'step': 1, 'timestamp': '2025-09-12T14:15:42.533580+00:00', 'type': 'task', 'payload': {'id': 'ed55e846-5192-a61d-74dd-073e921c5258', 'name': 'chatbot', 'input': {'messages': [HumanMessage(content='who is current president of American?', additional_kwargs={}, response_metadata={}, id='669fd68b-d54c-4d9c-8a85-965029c349d3')]}, 'triggers': ('branch:to:chatbot',)}})


((), 'messages', (AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_fd6099ddf10f476fab5a9c', 'function': {'arguments': '{"query', 'name': 'tavily_search'}, 'type': 'function'}]}, response_metadata={}, id='run--53614636-2082-4247-aa8a-a50f9a1dc10c', tool_calls=[{'name': 'tavily_search', 'args': {}, 'id': 'call_fd6099ddf10f476fab5a9c', 'type': 'tool_call'}], tool_call_chunks=[{'name': 'tavily_search', 'args': '{"query', 'id': 'call_fd6099ddf10f476fab5a9c', 'index': 0, 'type': 'tool_call_chunk'}]), {'langgraph_step': 1, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:ed55e846-5192-a61d-74dd-073e921c5258', 'checkpoint_ns': 'chatbot:ed55e846-5192-a61d-74dd-073e921c5258', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}))


((), 'messages', (AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': '', 'function': {'arguments': '": "current president of the', 'name': None}, 'type': 'function'}]}, response_metadata={}, id='run--53614636-2082-4247-aa8a-a50f9a1dc10c', invalid_tool_calls=[{'name': None, 'args': '": "current president of the', 'id': '', 'error': None, 'type': 'invalid_tool_call'}], tool_call_chunks=[{'name': None, 'args': '": "current president of the', 'id': '', 'index': 0, 'type': 'tool_call_chunk'}]), {'langgraph_step': 1, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:ed55e846-5192-a61d-74dd-073e921c5258', 'checkpoint_ns': 'chatbot:ed55e846-5192-a61d-74dd-073e921c5258', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}))


((), 'messages', (AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': '', 'function': {'arguments': ' United States"}', 'name': None}, 'type': 'function'}]}, response_metadata={}, id='run--53614636-2082-4247-aa8a-a50f9a1dc10c', invalid_tool_calls=[{'name': None, 'args': ' United States"}', 'id': '', 'error': None, 'type': 'invalid_tool_call'}], tool_call_chunks=[{'name': None, 'args': ' United States"}', 'id': '', 'index': 0, 'type': 'tool_call_chunk'}]), {'langgraph_step': 1, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:ed55e846-5192-a61d-74dd-073e921c5258', 'checkpoint_ns': 'chatbot:ed55e846-5192-a61d-74dd-073e921c5258', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}))


((), 'messages', (AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': '', 'function': {'arguments': None, 'name': None}, 'type': 'function'}]}, response_metadata={}, id='run--53614636-2082-4247-aa8a-a50f9a1dc10c', tool_calls=[{'name': '', 'args': {}, 'id': '', 'type': 'tool_call'}], tool_call_chunks=[{'name': None, 'args': None, 'id': '', 'index': 0, 'type': 'tool_call_chunk'}]), {'langgraph_step': 1, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:ed55e846-5192-a61d-74dd-073e921c5258', 'checkpoint_ns': 'chatbot:ed55e846-5192-a61d-74dd-073e921c5258', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}))


((), 'messages', (AIMessageChunk(content='', additional_kwargs={}, response_metadata={'finish_reason': 'tool_calls', 'model_name': 'qwen-plus'}, id='run--53614636-2082-4247-aa8a-a50f9a1dc10c'), {'langgraph_step': 1, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:ed55e846-5192-a61d-74dd-073e921c5258', 'checkpoint_ns': 'chatbot:ed55e846-5192-a61d-74dd-073e921c5258', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}))


((), 'updates', {'chatbot': {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_fd6099ddf10f476fab5a9c', 'function': {'arguments': '{"query": "current president of the United States"}', 'name': 'tavily_search'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'model_name': 'qwen-plus'}, id='run--53614636-2082-4247-aa8a-a50f9a1dc10c', tool_calls=[{'name': 'tavily_search', 'args': {'query': 'current president of the United States'}, 'id': 'call_fd6099ddf10f476fab5a9c', 'type': 'tool_call'}])]}})


((), 'debug', {'step': 1, 'timestamp': '2025-09-12T14:15:43.889664+00:00', 'type': 'task_result', 'payload': {'id': 'ed55e846-5192-a61d-74dd-073e921c5258', 'name': 'chatbot', 'error': None, 'result': [('messages', [AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_fd6099ddf10f476fab5a9c', 'function': {'arguments': '{"query": "current president of the United States"}', 'name': 'tavily_search'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'model_name': 'qwen-plus'}, id='run--53614636-2082-4247-aa8a-a50f9a1dc10c', tool_calls=[{'name': 'tavily_search', 'args': {'query': 'current president of the United States'}, 'id': 'call_fd6099ddf10f476fab5a9c', 'type': 'tool_call'}])])], 'interrupts': []}})


((), 'values', {'messages': [HumanMessage(content='who is current president of American?', additional_kwargs={}, response_metadata={}, id='669fd68b-d54c-4d9c-8a85-965029c349d3'), AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_fd6099ddf10f476fab5a9c', 'function': {'arguments': '{"query": "current president of the United States"}', 'name': 'tavily_search'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'model_name': 'qwen-plus'}, id='run--53614636-2082-4247-aa8a-a50f9a1dc10c', tool_calls=[{'name': 'tavily_search', 'args': {'query': 'current president of the United States'}, 'id': 'call_fd6099ddf10f476fab5a9c', 'type': 'tool_call'}])]})


((), 'debug', {'step': 2, 'timestamp': '2025-09-12T14:15:43.898071+00:00', 'type': 'task', 'payload': {'id': '0af385e6-5e27-104a-d28b-016b9c3f920a', 'name': 'tools', 'input': {'messages': [HumanMessage(content='who is current president of American?', additional_kwargs={}, response_metadata={}, id='669fd68b-d54c-4d9c-8a85-965029c349d3'), AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_fd6099ddf10f476fab5a9c', 'function': {'arguments': '{"query": "current president of the United States"}', 'name': 'tavily_search'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'model_name': 'qwen-plus'}, id='run--53614636-2082-4247-aa8a-a50f9a1dc10c', tool_calls=[{'name': 'tavily_search', 'args': {'query': 'current president of the United States'}, 'id': 'call_fd6099ddf10f476fab5a9c', 'type': 'tool_call'}])]}, 'triggers': ('branch:to:tools',)}})


((), 'messages', (ToolMessage(content='{"query": "current president of the United States", "follow_up_questions": null, "answer": null, "images": [], "results": [{"url": "https://www.usa.gov/presidents", "title": "Presidents, vice presidents, and first ladies | USAGov", "content": "The 47th and current president of the United States is Donald John Trump. He was sworn into office on January 20, 2025.", "score": 0.8952393, "raw_content": null}, {"url": "https://en.wikipedia.org/wiki/President_of_the_United_States", "title": "President of the United States", "content": "In all, 45 individuals have served 47 presidencies spanning 60 four-year terms. Donald Trump is the 47th and current president since January 20, 2025.", "score": 0.8430832, "raw_content": null}], "response_time": 1.61, "request_id": "bb514848-63a8-4af0-a423-1245678ca93e"}', name='tavily_search', id='c64754e6-bc97-4264-94ae-ca1805423efe', tool_call_id='call_fd6099ddf10f476fab5a9c'), {'langgraph_step': 2, 'langgraph_node': 'tools', 'langgraph_triggers': ('branch:to:tools',), 'langgraph_path': ('__pregel_pull', 'tools'), 'langgraph_checkpoint_ns': 'tools:0af385e6-5e27-104a-d28b-016b9c3f920a'}))


((), 'updates', {'tools': {'messages': [ToolMessage(content='{"query": "current president of the United States", "follow_up_questions": null, "answer": null, "images": [], "results": [{"url": "https://www.usa.gov/presidents", "title": "Presidents, vice presidents, and first ladies | USAGov", "content": "The 47th and current president of the United States is Donald John Trump. He was sworn into office on January 20, 2025.", "score": 0.8952393, "raw_content": null}, {"url": "https://en.wikipedia.org/wiki/President_of_the_United_States", "title": "President of the United States", "content": "In all, 45 individuals have served 47 presidencies spanning 60 four-year terms. Donald Trump is the 47th and current president since January 20, 2025.", "score": 0.8430832, "raw_content": null}], "response_time": 1.61, "request_id": "bb514848-63a8-4af0-a423-1245678ca93e"}', name='tavily_search', id='c64754e6-bc97-4264-94ae-ca1805423efe', tool_call_id='call_fd6099ddf10f476fab5a9c')]}})


((), 'debug', {'step': 2, 'timestamp': '2025-09-12T14:15:39.875980+00:00', 'type': 'task_result', 'payload': {'id': '0af385e6-5e27-104a-d28b-016b9c3f920a', 'name': 'tools', 'error': None, 'result': [('messages', [ToolMessage(content='{"query": "current president of the United States", "follow_up_questions": null, "answer": null, "images": [], "results": [{"url": "https://www.usa.gov/presidents", "title": "Presidents, vice presidents, and first ladies | USAGov", "content": "The 47th and current president of the United States is Donald John Trump. He was sworn into office on January 20, 2025.", "score": 0.8952393, "raw_content": null}, {"url": "https://en.wikipedia.org/wiki/President_of_the_United_States", "title": "President of the United States", "content": "In all, 45 individuals have served 47 presidencies spanning 60 four-year terms. Donald Trump is the 47th and current president since January 20, 2025.", "score": 0.8430832, "raw_content": null}], "response_time": 1.61, "request_id": "bb514848-63a8-4af0-a423-1245678ca93e"}', name='tavily_search', id='c64754e6-bc97-4264-94ae-ca1805423efe', tool_call_id='call_fd6099ddf10f476fab5a9c')])], 'interrupts': []}})


((), 'values', {'messages': [HumanMessage(content='who is current president of American?', additional_kwargs={}, response_metadata={}, id='669fd68b-d54c-4d9c-8a85-965029c349d3'), AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_fd6099ddf10f476fab5a9c', 'function': {'arguments': '{"query": "current president of the United States"}', 'name': 'tavily_search'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'model_name': 'qwen-plus'}, id='run--53614636-2082-4247-aa8a-a50f9a1dc10c', tool_calls=[{'name': 'tavily_search', 'args': {'query': 'current president of the United States'}, 'id': 'call_fd6099ddf10f476fab5a9c', 'type': 'tool_call'}]), ToolMessage(content='{"query": "current president of the United States", "follow_up_questions": null, "answer": null, "images": [], "results": [{"url": "https://www.usa.gov/presidents", "title": "Presidents, vice presidents, and first ladies | USAGov", "content": "The 47th and current president of the United States is Donald John Trump. He was sworn into office on January 20, 2025.", "score": 0.8952393, "raw_content": null}, {"url": "https://en.wikipedia.org/wiki/President_of_the_United_States", "title": "President of the United States", "content": "In all, 45 individuals have served 47 presidencies spanning 60 four-year terms. Donald Trump is the 47th and current president since January 20, 2025.", "score": 0.8430832, "raw_content": null}], "response_time": 1.61, "request_id": "bb514848-63a8-4af0-a423-1245678ca93e"}', name='tavily_search', id='c64754e6-bc97-4264-94ae-ca1805423efe', tool_call_id='call_fd6099ddf10f476fab5a9c')]})


((), 'debug', {'step': 3, 'timestamp': '2025-09-12T14:15:39.884988+00:00', 'type': 'task', 'payload': {'id': '34f44918-c519-0fea-0da7-c96519f64d34', 'name': 'chatbot', 'input': {'messages': [HumanMessage(content='who is current president of American?', additional_kwargs={}, response_metadata={}, id='669fd68b-d54c-4d9c-8a85-965029c349d3'), AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_fd6099ddf10f476fab5a9c', 'function': {'arguments': '{"query": "current president of the United States"}', 'name': 'tavily_search'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'model_name': 'qwen-plus'}, id='run--53614636-2082-4247-aa8a-a50f9a1dc10c', tool_calls=[{'name': 'tavily_search', 'args': {'query': 'current president of the United States'}, 'id': 'call_fd6099ddf10f476fab5a9c', 'type': 'tool_call'}]), ToolMessage(content='{"query": "current president of the United States", "follow_up_questions": null, "answer": null, "images": [], "results": [{"url": "https://www.usa.gov/presidents", "title": "Presidents, vice presidents, and first ladies | USAGov", "content": "The 47th and current president of the United States is Donald John Trump. He was sworn into office on January 20, 2025.", "score": 0.8952393, "raw_content": null}, {"url": "https://en.wikipedia.org/wiki/President_of_the_United_States", "title": "President of the United States", "content": "In all, 45 individuals have served 47 presidencies spanning 60 four-year terms. Donald Trump is the 47th and current president since January 20, 2025.", "score": 0.8430832, "raw_content": null}], "response_time": 1.61, "request_id": "bb514848-63a8-4af0-a423-1245678ca93e"}', name='tavily_search', id='c64754e6-bc97-4264-94ae-ca1805423efe', tool_call_id='call_fd6099ddf10f476fab5a9c')]}, 'triggers': ('branch:to:chatbot',)}})


((), 'messages', (AIMessageChunk(content='The', additional_kwargs={}, response_metadata={}, id='run--c759c8f3-5e53-4a9b-899e-842fa1126edb'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}))


((), 'messages', (AIMessageChunk(content=' current', additional_kwargs={}, response_metadata={}, id='run--c759c8f3-5e53-4a9b-899e-842fa1126edb'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}))


((), 'messages', (AIMessageChunk(content=' president', additional_kwargs={}, response_metadata={}, id='run--c759c8f3-5e53-4a9b-899e-842fa1126edb'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}))


((), 'messages', (AIMessageChunk(content=' of the United', additional_kwargs={}, response_metadata={}, id='run--c759c8f3-5e53-4a9b-899e-842fa1126edb'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}))


((), 'messages', (AIMessageChunk(content=' States is Donald John', additional_kwargs={}, response_metadata={}, id='run--c759c8f3-5e53-4a9b-899e-842fa1126edb'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}))


((), 'messages', (AIMessageChunk(content=' Trump, who was', additional_kwargs={}, response_metadata={}, id='run--c759c8f3-5e53-4a9b-899e-842fa1126edb'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}))


((), 'messages', (AIMessageChunk(content=' sworn into office on January', additional_kwargs={}, response_metadata={}, id='run--c759c8f3-5e53-4a9b-899e-842fa1126edb'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}))


((), 'messages', (AIMessageChunk(content=' 20, 2', additional_kwargs={}, response_metadata={}, id='run--c759c8f3-5e53-4a9b-899e-842fa1126edb'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}))


((), 'messages', (AIMessageChunk(content='025.', additional_kwargs={}, response_metadata={}, id='run--c759c8f3-5e53-4a9b-899e-842fa1126edb'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}))


((), 'messages', (AIMessageChunk(content='', additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'qwen-plus'}, id='run--c759c8f3-5e53-4a9b-899e-842fa1126edb'), {'langgraph_step': 3, 'langgraph_node': 'chatbot', 'langgraph_triggers': ('branch:to:chatbot',), 'langgraph_path': ('__pregel_pull', 'chatbot'), 'langgraph_checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'checkpoint_ns': 'chatbot:34f44918-c519-0fea-0da7-c96519f64d34', 'ls_provider': 'openai', 'ls_model_name': 'qwen-plus', 'ls_model_type': 'chat', 'ls_temperature': None}))


((), 'updates', {'chatbot': {'messages': [AIMessage(content='The current president of the United States is Donald John Trump, who was sworn into office on January 20, 2025.', additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'qwen-plus'}, id='run--c759c8f3-5e53-4a9b-899e-842fa1126edb')]}})


((), 'debug', {'step': 3, 'timestamp': '2025-09-12T14:15:41.638900+00:00', 'type': 'task_result', 'payload': {'id': '34f44918-c519-0fea-0da7-c96519f64d34', 'name': 'chatbot', 'error': None, 'result': [('messages', [AIMessage(content='The current president of the United States is Donald John Trump, who was sworn into office on January 20, 2025.', additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'qwen-plus'}, id='run--c759c8f3-5e53-4a9b-899e-842fa1126edb')])], 'interrupts': []}})


((), 'values', {'messages': [HumanMessage(content='who is current president of American?', additional_kwargs={}, response_metadata={}, id='669fd68b-d54c-4d9c-8a85-965029c349d3'), AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_fd6099ddf10f476fab5a9c', 'function': {'arguments': '{"query": "current president of the United States"}', 'name': 'tavily_search'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'model_name': 'qwen-plus'}, id='run--53614636-2082-4247-aa8a-a50f9a1dc10c', tool_calls=[{'name': 'tavily_search', 'args': {'query': 'current president of the United States'}, 'id': 'call_fd6099ddf10f476fab5a9c', 'type': 'tool_call'}]), ToolMessage(content='{"query": "current president of the United States", "follow_up_questions": null, "answer": null, "images": [], "results": [{"url": "https://www.usa.gov/presidents", "title": "Presidents, vice presidents, and first ladies | USAGov", "content": "The 47th and current president of the United States is Donald John Trump. He was sworn into office on January 20, 2025.", "score": 0.8952393, "raw_content": null}, {"url": "https://en.wikipedia.org/wiki/President_of_the_United_States", "title": "President of the United States", "content": "In all, 45 individuals have served 47 presidencies spanning 60 four-year terms. Donald Trump is the 47th and current president since January 20, 2025.", "score": 0.8430832, "raw_content": null}], "response_time": 1.61, "request_id": "bb514848-63a8-4af0-a423-1245678ca93e"}', name='tavily_search', id='c64754e6-bc97-4264-94ae-ca1805423efe', tool_call_id='call_fd6099ddf10f476fab5a9c'), AIMessage(content='The current president of the United States is Donald John Trump, who was sworn into office on January 20, 2025.', additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'qwen-plus'}, id='run--c759c8f3-5e53-4a9b-899e-842fa1126edb')]})


 

    3.langgraph-api中代码分析

       以https://blog.csdn.net/zhangbaolin/article/details/151396521?spm=1001.2014.3001.5502为例进行分析。前端发起请求时数据如下:

{
  "input": {
    "messages": [
      {
        "type": "human",
        "content": [
          {
            "type": "text",
            "text": "美国现任总统是谁?"
          }
        ]
      }
    ]
  },
  "stream_mode": [
    "values",
    "messages-tuple",
    "custom"
  ],
  "stream_subgraphs": true,
  "stream_resumable": true,
  "assistant_id": "agent",
  "on_disconnect": "continue"
}
 

        核心代码在langgraph-api/stream.py的astream_state中,相关代码如下:

async def astream_state(#zbl 核心代码
    run: Run,
    attempt: int,
    done: ValueEvent,
    *,
    on_checkpoint: Callable[[CheckpointPayload | None], None] = lambda _: None,
    on_task_result: Callable[[TaskResultPayload], None] = lambda _: None,
) -> AnyStream:

    ……

    #stream_mode为请求数据中的stream_mode,包括values,messages_tuple和custom

    stream_mode: list[StreamMode] = kwargs.pop("stream_mode")
    feedback_keys = kwargs.pop("feedback_keys", None)
    stream_modes_set: set[StreamMode] = set(stream_mode) - {"events"}

    #经过以下几行代码处理,stream_modes_set中的数据为

    #messages', 'debug', 'updates', 'values', 'custom'
    if "debug" not in stream_modes_set:
        stream_modes_set.add("debug")
    if "messages-tuple" in stream_modes_set and not isinstance(graph, BaseRemotePregel):
        stream_modes_set.remove("messages-tuple")
        stream_modes_set.add("messages")
    if "updates" not in stream_modes_set:
        stream_modes_set.add("updates")
        only_interrupt_updates = True
    else:
        only_interrupt_updates = False

    ……

    else:
        output_keys = kwargs.pop("output_keys", graph.output_channels)
        if USE_RUNTIME_CONTEXT_API:
            kwargs["context"] = context
        async with (
            stack,
            aclosing(
                graph.astream(
                    input,
                    config,
                    stream_mode=list(stream_modes_set),
                    output_keys=output_keys,
                    **kwargs,
                )
            ) as stream,
        ):
            sentinel = object()
            while True:
                event = await wait_if_not_done(anext(stream, sentinel), done)#迭代
                if event is sentinel:
                    break
                if subgraphs:#走这个分支,根据上面的数据知道此时ns为""

                    #mode为模式,chunk为数据
                    ns, mode, chunk = cast(tuple[str, str, dict[str, Any]], event)
                else:
                    mode, chunk = cast(tuple[str, dict[str, Any]], event)
                    ns = None
                # --- begin shared logic with astream_events ---
                if mode == "debug":#mode为debug时处理检查点
                    if chunk["type"] == "checkpoint":
                        checkpoint = _preprocess_debug_checkpoint(chunk["payload"])
                        chunk["payload"] = checkpoint
                        on_checkpoint(checkpoint)
                    elif chunk["type"] == "task_result":
                        on_task_result(chunk["payload"])
                if mode == "messages":#mode为messages时直接yield
                    if "messages-tuple" in stream_mode:
                        if subgraphs and ns:
                            yield f"messages|{'|'.join(ns)}", chunk
                        else:
                            yield "messages", chunk
                    else:
                        msg_, meta = cast(
                            tuple[BaseMessage | dict, dict[str, Any]], chunk
                        )
                        msg = (
                            convert_to_messages([msg_])[0]
                            if isinstance(msg_, dict)
                            else cast(BaseMessage, msg_)
                        )

                        if msg.id in messages:
                            messages[msg.id] += msg
                        else:
                            messages[msg.id] = msg
                            yield "messages/metadata", {msg.id: {"metadata": meta}}
                        yield (
                            (
                                "messages/partial"
                                if isinstance(msg, BaseMessageChunk)
                                else "messages/complete"
                            ),
                            [message_chunk_to_message(messages[msg.id])],
                        )
                elif mode in stream_mode:#如果mode是values,直接yield
                    if subgraphs and ns:
                        yield f"{mode}|{'|'.join(ns)}", chunk
                    else:
                        yield mode, chunk
                elif (#mode为update时处理中断事件
                    mode == "updates"
                    and isinstance(chunk, dict)
                    and "__interrupt__" in chunk
                    and len(chunk["__interrupt__"]) > 0
                    and only_interrupt_updates
                ):
                    # We always want to return interrupt events by default.
                    # If updates aren't specified as a stream mode, we return these as values events.
                    # If the interrupt doesn't have any actions (e.g. interrupt before or after a node is specified), we don't return the interrupt at all today.
                    if subgraphs and ns:
                        yield "values|{'|'.join(ns)}", chunk
                    else:
                        yield "values", chunk

        langgraph-api返回前端数据如下:

event: metadata
data: {"run_id":"01993d66-30c2-73c6-aa95-80e787711830","attempt":1}
id: 1757671732119-0

event: values
data: {"messages":[{"content":[{"type":"text","text":"朝鲜劳动党现任总书记是谁?"}],"additional_kwargs":{},"response_metadata":{},"type":"human","name":null,"id":"b92d5ae7-0bc4-463f-ab49-45e4cbe56f26","example":false}]}
id: 1757671732128-0

event: messages
data: [{"content":"","additional_kwargs":{"tool_calls":[{"index":0,"id":"call_aa235e37f2854788a2e9bb","function":{"arguments":"{\"query","name":"tavily_search"},"type":"function"}]},"response_metadata":{},"type":"AIMessageChunk","name":null,"id":"run--b9aec800-2046-43ca-975b-400372de3d31","example":false,"tool_calls":[{"name":"tavily_search","args":{},"id":"call_aa235e37f2854788a2e9bb","type":"tool_call"}],"invalid_tool_calls":[],"usage_metadata":null,"tool_call_chunks":[{"name":"tavily_search","args":"{\"query","id":"call_aa235e37f2854788a2e9bb","index":0,"type":"tool_call_chunk"}]},{"created_by":"system","graph_id":"agent","assistant_id":"fe096781-5601-53d2-b2f6-0d3403f7e9ca","run_attempt":1,"langgraph_version":"0.6.7","langgraph_api_version":"0.4.15","langgraph_plan":"developer","langgraph_host":"self-hosted","langgraph_api_url":"http://0.0.0.0:2024","langgraph_auth_user_id":"","langgraph_request_id":"e51c74fe-ccb0-43fc-a9ac-a9bebd0ed480","run_id":"01993d66-30c2-73c6-aa95-80e787711830","thread_id":"6ba24e75-5283-413f-ab91-37c50e1096a4","user_id":"","langgraph_step":1,"langgraph_node":"chatbot","langgraph_triggers":["branch:to:chatbot"],"langgraph_path":["__pregel_pull","chatbot"],"langgraph_checkpoint_ns":"chatbot:a3da8228-1679-a73a-c23c-8488374e3941","checkpoint_ns":"chatbot:a3da8228-1679-a73a-c23c-8488374e3941","ls_provider":"openai","ls_model_name":"qwen-plus","ls_model_type":"chat","ls_temperature":null}]
id: 1757671732932-0

event: messages
data: [{"content":"","additional_kwargs":{"tool_calls":[{"index":0,"id":"","function":{"arguments":"\": \"朝鲜劳动党现任","name":null},"type":"function"}]},"response_metadata":{},"type":"AIMessageChunk","name":null,"id":"run--b9aec800-2046-43ca-975b-400372de3d31","example":false,"tool_calls":[],"invalid_tool_calls":[{"name":null,"args":"\": \"朝鲜劳动党现任","id":"","error":null,"type":"invalid_tool_call"}],"usage_metadata":null,"tool_call_chunks":[{"name":null,"args":"\": \"朝鲜劳动党现任","id":"","index":0,"type":"tool_call_chunk"}]},{"created_by":"system","graph_id":"agent","assistant_id":"fe096781-5601-53d2-b2f6-0d3403f7e9ca","run_attempt":1,"langgraph_version":"0.6.7","langgraph_api_version":"0.4.15","langgraph_plan":"developer","langgraph_host":"self-hosted","langgraph_api_url":"http://0.0.0.0:2024","langgraph_auth_user_id":"","langgraph_request_id":"e51c74fe-ccb0-43fc-a9ac-a9bebd0ed480","run_id":"01993d66-30c2-73c6-aa95-80e787711830","thread_id":"6ba24e75-5283-413f-ab91-37c50e1096a4","user_id":"","langgraph_step":1,"langgraph_node":"chatbot","langgraph_triggers":["branch:to:chatbot"],"langgraph_path":["__pregel_pull","chatbot"],"langgraph_checkpoint_ns":"chatbot:a3da8228-1679-a73a-c23c-8488374e3941","checkpoint_ns":"chatbot:a3da8228-1679-a73a-c23c-8488374e3941","ls_provider":"openai","ls_model_name":"qwen-plus","ls_model_type":"chat","ls_temperature":null}]
id: 1757671733030-0

event: messages
data: [{"content":"","additional_kwargs":{"tool_calls":[{"index":0,"id":"","function":{"arguments":"总书记是谁?\"}","name":null},"type":"function"}]},"response_metadata":{},"type":"AIMessageChunk","name":null,"id":"run--b9aec800-2046-43ca-975b-400372de3d31","example":false,"tool_calls":[],"invalid_tool_calls":[{"name":null,"args":"总书记是谁?\"}","id":"","error":null,"type":"invalid_tool_call"}],"usage_metadata":null,"tool_call_chunks":[{"name":null,"args":"总书记是谁?\"}","id":"","index":0,"type":"tool_call_chunk"}]},{"created_by":"system","graph_id":"agent","assistant_id":"fe096781-5601-53d2-b2f6-0d3403f7e9ca","run_attempt":1,"langgraph_version":"0.6.7","langgraph_api_version":"0.4.15","langgraph_plan":"developer","langgraph_host":"self-hosted","langgraph_api_url":"http://0.0.0.0:2024","langgraph_auth_user_id":"","langgraph_request_id":"e51c74fe-ccb0-43fc-a9ac-a9bebd0ed480","run_id":"01993d66-30c2-73c6-aa95-80e787711830","thread_id":"6ba24e75-5283-413f-ab91-37c50e1096a4","user_id":"","langgraph_step":1,"langgraph_node":"chatbot","langgraph_triggers":["branch:to:chatbot"],"langgraph_path":["__pregel_pull","chatbot"],"langgraph_checkpoint_ns":"chatbot:a3da8228-1679-a73a-c23c-8488374e3941","checkpoint_ns":"chatbot:a3da8228-1679-a73a-c23c-8488374e3941","ls_provider":"openai","ls_model_name":"qwen-plus","ls_model_type":"chat","ls_temperature":null}]
id: 1757671733093-0

event: messages
data: [{"content":"","additional_kwargs":{},"response_metadata":{"finish_reason":"tool_calls","model_name":"qwen-plus"},"type":"AIMessageChunk","name":null,"id":"run--b9aec800-2046-43ca-975b-400372de3d31","example":false,"tool_calls":[],"invalid_tool_calls":[],"usage_metadata":null,"tool_call_chunks":[]},{"created_by":"system","graph_id":"agent","assistant_id":"fe096781-5601-53d2-b2f6-0d3403f7e9ca","run_attempt":1,"langgraph_version":"0.6.7","langgraph_api_version":"0.4.15","langgraph_plan":"developer","langgraph_host":"self-hosted","langgraph_api_url":"http://0.0.0.0:2024","langgraph_auth_user_id":"","langgraph_request_id":"e51c74fe-ccb0-43fc-a9ac-a9bebd0ed480","run_id":"01993d66-30c2-73c6-aa95-80e787711830","thread_id":"6ba24e75-5283-413f-ab91-37c50e1096a4","user_id":"","langgraph_step":1,"langgraph_node":"chatbot","langgraph_triggers":["branch:to:chatbot"],"langgraph_path":["__pregel_pull","chatbot"],"langgraph_checkpoint_ns":"chatbot:a3da8228-1679-a73a-c23c-8488374e3941","checkpoint_ns":"chatbot:a3da8228-1679-a73a-c23c-8488374e3941","ls_provider":"openai","ls_model_name":"qwen-plus","ls_model_type":"chat","ls_temperature":null}]
id: 1757671733100-0

event: values
data: {"messages":[{"content":[{"type":"text","text":"朝鲜劳动党现任总书记是谁?"}],"additional_kwargs":{},"response_metadata":{},"type":"human","name":null,"id":"b92d5ae7-0bc4-463f-ab49-45e4cbe56f26","example":false},{"content":"","additional_kwargs":{"tool_calls":[{"index":0,"id":"call_aa235e37f2854788a2e9bb","function":{"arguments":"{\"query\": \"朝鲜劳动党现任总书记是谁?\"}","name":"tavily_search"},"type":"function"}]},"response_metadata":{"finish_reason":"tool_calls","model_name":"qwen-plus"},"type":"ai","name":null,"id":"run--b9aec800-2046-43ca-975b-400372de3d31","example":false,"tool_calls":[{"name":"tavily_search","args":{"query":"朝鲜劳动党现任总书记是谁?"},"id":"call_aa235e37f2854788a2e9bb","type":"tool_call"}],"invalid_tool_calls":[],"usage_metadata":null}]}
id: 1757671733113-0

event: messages
data: [{"content":"{\"query\": \"朝鲜劳动党现任总书记是谁?\", \"follow_up_questions\": null, \"answer\": null, \"images\": [], \"results\": [{\"url\": \"https://zh.wikipedia.org/zh-hans/%E6%9C%9D%E9%AE%AE%E5%8B%9E%E5%8B%95%E9%BB%A8%E7%B8%BD%E6%9B%B8%E8%A8%98\", \"title\": \"朝鲜劳动党总书记 - 维基百科\", \"content\": \"2021年1月10日,朝鲜劳动党第八次代表大会决议恢复总书记的头衔,并推举金正恩为总书记。\", \"score\": 0.8588255, \"raw_content\": null}, {\"url\": \"https://baike.baidu.com/item/%E6%9C%9D%E9%B2%9C%E5%8A%B3%E5%8A%A8%E5%85%9A%E6%80%BB%E4%B9%A6%E8%AE%B0/3895680\", \"title\": \"朝鲜劳动党总书记\", \"content\": \"中文名. 朝鲜劳动党总书记 · 外文名. 조선로동당총비서 · 别名. 朝鲜劳动党中央委员会总书记 · 实际地位. 朝鲜党和国家的最高领导人 · 现任领导. 金正恩.\", \"score\": 0.8585411, \"raw_content\": null}], \"response_time\": 1.29, \"request_id\": \"255b4a09-2b3f-41fd-a83d-6ac0588511b8\"}","additional_kwargs":{},"response_metadata":{},"type":"tool","name":"tavily_search","id":"23151eca-3d44-47e1-a0d0-67e59ed1f5de","tool_call_id":"call_aa235e37f2854788a2e9bb","artifact":null,"status":"success"},{"created_by":"system","graph_id":"agent","assistant_id":"fe096781-5601-53d2-b2f6-0d3403f7e9ca","run_attempt":1,"langgraph_version":"0.6.7","langgraph_api_version":"0.4.15","langgraph_plan":"developer","langgraph_host":"self-hosted","langgraph_api_url":"http://0.0.0.0:2024","langgraph_auth_user_id":"","langgraph_request_id":"e51c74fe-ccb0-43fc-a9ac-a9bebd0ed480","run_id":"01993d66-30c2-73c6-aa95-80e787711830","thread_id":"6ba24e75-5283-413f-ab91-37c50e1096a4","user_id":"","langgraph_step":2,"langgraph_node":"tools","langgraph_triggers":["branch:to:tools"],"langgraph_path":["__pregel_pull","tools"],"langgraph_checkpoint_ns":"tools:aa1eba2e-0854-3888-fb18-9c0e525c949f"}]
id: 1757671735247-0

event: values
data: {"messages":[{"content":[{"type":"text","text":"朝鲜劳动党现任总书记是谁?"}],"additional_kwargs":{},"response_metadata":{},"type":"human","name":null,"id":"b92d5ae7-0bc4-463f-ab49-45e4cbe56f26","example":false},{"content":"","additional_kwargs":{"tool_calls":[{"index":0,"id":"call_aa235e37f2854788a2e9bb","function":{"arguments":"{\"query\": \"朝鲜劳动党现任总书记是谁?\"}","name":"tavily_search"},"type":"function"}]},"response_metadata":{"finish_reason":"tool_calls","model_name":"qwen-plus"},"type":"ai","name":null,"id":"run--b9aec800-2046-43ca-975b-400372de3d31","example":false,"tool_calls":[{"name":"tavily_search","args":{"query":"朝鲜劳动党现任总书记是谁?"},"id":"call_aa235e37f2854788a2e9bb","type":"tool_call"}],"invalid_tool_calls":[],"usage_metadata":null},{"content":"{\"query\": \"朝鲜劳动党现任总书记是谁?\", \"follow_up_questions\": null, \"answer\": null, \"images\": [], \"results\": [{\"url\": \"https://zh.wikipedia.org/zh-hans/%E6%9C%9D%E9%AE%AE%E5%8B%9E%E5%8B%95%E9%BB%A8%E7%B8%BD%E6%9B%B8%E8%A8%98\", \"title\": \"朝鲜劳动党总书记 - 维基百科\", \"content\": \"2021年1月10日,朝鲜劳动党第八次代表大会决议恢复总书记的头衔,并推举金正恩为总书记。\", \"score\": 0.8588255, \"raw_content\": null}, {\"url\": \"https://baike.baidu.com/item/%E6%9C%9D%E9%B2%9C%E5%8A%B3%E5%8A%A8%E5%85%9A%E6%80%BB%E4%B9%A6%E8%AE%B0/3895680\", \"title\": \"朝鲜劳动党总书记\", \"content\": \"中文名. 朝鲜劳动党总书记 · 外文名. 조선로동당총비서 · 别名. 朝鲜劳动党中央委员会总书记 · 实际地位. 朝鲜党和国家的最高领导人 · 现任领导. 金正恩.\", \"score\": 0.8585411, \"raw_content\": null}], \"response_time\": 1.29, \"request_id\": \"255b4a09-2b3f-41fd-a83d-6ac0588511b8\"}","additional_kwargs":{},"response_metadata":{},"type":"tool","name":"tavily_search","id":"23151eca-3d44-47e1-a0d0-67e59ed1f5de","tool_call_id":"call_aa235e37f2854788a2e9bb","artifact":null,"status":"success"}]}
id: 1757671735256-0

event: messages
data: [{"content":"朝鲜","additional_kwargs":{},"response_metadata":{},"type":"AIMessageChunk","name":null,"id":"run--2cb7c2b5-9217-4145-90c9-8029462f4dbf","example":false,"tool_calls":[],"invalid_tool_calls":[],"usage_metadata":null,"tool_call_chunks":[]},{"created_by":"system","graph_id":"agent","assistant_id":"fe096781-5601-53d2-b2f6-0d3403f7e9ca","run_attempt":1,"langgraph_version":"0.6.7","langgraph_api_version":"0.4.15","langgraph_plan":"developer","langgraph_host":"self-hosted","langgraph_api_url":"http://0.0.0.0:2024","langgraph_auth_user_id":"","langgraph_request_id":"e51c74fe-ccb0-43fc-a9ac-a9bebd0ed480","run_id":"01993d66-30c2-73c6-aa95-80e787711830","thread_id":"6ba24e75-5283-413f-ab91-37c50e1096a4","user_id":"","langgraph_step":3,"langgraph_node":"chatbot","langgraph_triggers":["branch:to:chatbot"],"langgraph_path":["__pregel_pull","chatbot"],"langgraph_checkpoint_ns":"chatbot:06887461-c51c-2408-1eb8-45cebc9278d1","checkpoint_ns":"chatbot:06887461-c51c-2408-1eb8-45cebc9278d1","ls_provider":"openai","ls_model_name":"qwen-plus","ls_model_type":"chat","ls_temperature":null}]
id: 1757671735804-0

event: messages
data: [{"content":"劳动党现任","additional_kwargs":{},"response_metadata":{},"type":"AIMessageChunk","name":null,"id":"run--2cb7c2b5-9217-4145-90c9-8029462f4dbf","example":false,"tool_calls":[],"invalid_tool_calls":[],"usage_metadata":null,"tool_call_chunks":[]},{"created_by":"system","graph_id":"agent","assistant_id":"fe096781-5601-53d2-b2f6-0d3403f7e9ca","run_attempt":1,"langgraph_version":"0.6.7","langgraph_api_version":"0.4.15","langgraph_plan":"developer","langgraph_host":"self-hosted","langgraph_api_url":"http://0.0.0.0:2024","langgraph_auth_user_id":"","langgraph_request_id":"e51c74fe-ccb0-43fc-a9ac-a9bebd0ed480","run_id":"01993d66-30c2-73c6-aa95-80e787711830","thread_id":"6ba24e75-5283-413f-ab91-37c50e1096a4","user_id":"","langgraph_step":3,"langgraph_node":"chatbot","langgraph_triggers":["branch:to:chatbot"],"langgraph_path":["__pregel_pull","chatbot"],"langgraph_checkpoint_ns":"chatbot:06887461-c51c-2408-1eb8-45cebc9278d1","checkpoint_ns":"chatbot:06887461-c51c-2408-1eb8-45cebc9278d1","ls_provider":"openai","ls_model_name":"qwen-plus","ls_model_type":"chat","ls_temperature":null}]
id: 1757671735808-0

event: messages
data: [{"content":"总书记","additional_kwargs":{},"response_metadata":{},"type":"AIMessageChunk","name":null,"id":"run--2cb7c2b5-9217-4145-90c9-8029462f4dbf","example":false,"tool_calls":[],"invalid_tool_calls":[],"usage_metadata":null,"tool_call_chunks":[]},{"created_by":"system","graph_id":"agent","assistant_id":"fe096781-5601-53d2-b2f6-0d3403f7e9ca","run_attempt":1,"langgraph_version":"0.6.7","langgraph_api_version":"0.4.15","langgraph_plan":"developer","langgraph_host":"self-hosted","langgraph_api_url":"http://0.0.0.0:2024","langgraph_auth_user_id":"","langgraph_request_id":"e51c74fe-ccb0-43fc-a9ac-a9bebd0ed480","run_id":"01993d66-30c2-73c6-aa95-80e787711830","thread_id":"6ba24e75-5283-413f-ab91-37c50e1096a4","user_id":"","langgraph_step":3,"langgraph_node":"chatbot","langgraph_triggers":["branch:to:chatbot"],"langgraph_path":["__pregel_pull","chatbot"],"langgraph_checkpoint_ns":"chatbot:06887461-c51c-2408-1eb8-45cebc9278d1","checkpoint_ns":"chatbot:06887461-c51c-2408-1eb8-45cebc9278d1","ls_provider":"openai","ls_model_name":"qwen-plus","ls_model_type":"chat","ls_temperature":null}]
id: 1757671735810-0

event: messages
data: [{"content":"是","additional_kwargs":{},"response_metadata":{},"type":"AIMessageChunk","name":null,"id":"run--2cb7c2b5-9217-4145-90c9-8029462f4dbf","example":false,"tool_calls":[],"invalid_tool_calls":[],"usage_metadata":null,"tool_call_chunks":[]},{"created_by":"system","graph_id":"agent","assistant_id":"fe096781-5601-53d2-b2f6-0d3403f7e9ca","run_attempt":1,"langgraph_version":"0.6.7","langgraph_api_version":"0.4.15","langgraph_plan":"developer","langgraph_host":"self-hosted","langgraph_api_url":"http://0.0.0.0:2024","langgraph_auth_user_id":"","langgraph_request_id":"e51c74fe-ccb0-43fc-a9ac-a9bebd0ed480","run_id":"01993d66-30c2-73c6-aa95-80e787711830","thread_id":"6ba24e75-5283-413f-ab91-37c50e1096a4","user_id":"","langgraph_step":3,"langgraph_node":"chatbot","langgraph_triggers":["branch:to:chatbot"],"langgraph_path":["__pregel_pull","chatbot"],"langgraph_checkpoint_ns":"chatbot:06887461-c51c-2408-1eb8-45cebc9278d1","checkpoint_ns":"chatbot:06887461-c51c-2408-1eb8-45cebc9278d1","ls_provider":"openai","ls_model_name":"qwen-plus","ls_model_type":"chat","ls_temperature":null}]
id: 1757671736206-0

event: messages
data: [{"content":"金正恩。","additional_kwargs":{},"response_metadata":{},"type":"AIMessageChunk","name":null,"id":"run--2cb7c2b5-9217-4145-90c9-8029462f4dbf","example":false,"tool_calls":[],"invalid_tool_calls":[],"usage_metadata":null,"tool_call_chunks":[]},{"created_by":"system","graph_id":"agent","assistant_id":"fe096781-5601-53d2-b2f6-0d3403f7e9ca","run_attempt":1,"langgraph_version":"0.6.7","langgraph_api_version":"0.4.15","langgraph_plan":"developer","langgraph_host":"self-hosted","langgraph_api_url":"http://0.0.0.0:2024","langgraph_auth_user_id":"","langgraph_request_id":"e51c74fe-ccb0-43fc-a9ac-a9bebd0ed480","run_id":"01993d66-30c2-73c6-aa95-80e787711830","thread_id":"6ba24e75-5283-413f-ab91-37c50e1096a4","user_id":"","langgraph_step":3,"langgraph_node":"chatbot","langgraph_triggers":["branch:to:chatbot"],"langgraph_path":["__pregel_pull","chatbot"],"langgraph_checkpoint_ns":"chatbot:06887461-c51c-2408-1eb8-45cebc9278d1","checkpoint_ns":"chatbot:06887461-c51c-2408-1eb8-45cebc9278d1","ls_provider":"openai","ls_model_name":"qwen-plus","ls_model_type":"chat","ls_temperature":null}]
id: 1757671736365-0

: heartbeat

event: messages
data: [{"content":"","additional_kwargs":{},"response_metadata":{"finish_reason":"stop","model_name":"qwen-plus"},"type":"AIMessageChunk","name":null,"id":"run--2cb7c2b5-9217-4145-90c9-8029462f4dbf","example":false,"tool_calls":[],"invalid_tool_calls":[],"usage_metadata":null,"tool_call_chunks":[]},{"created_by":"system","graph_id":"agent","assistant_id":"fe096781-5601-53d2-b2f6-0d3403f7e9ca","run_attempt":1,"langgraph_version":"0.6.7","langgraph_api_version":"0.4.15","langgraph_plan":"developer","langgraph_host":"self-hosted","langgraph_api_url":"http://0.0.0.0:2024","langgraph_auth_user_id":"","langgraph_request_id":"e51c74fe-ccb0-43fc-a9ac-a9bebd0ed480","run_id":"01993d66-30c2-73c6-aa95-80e787711830","thread_id":"6ba24e75-5283-413f-ab91-37c50e1096a4","user_id":"","langgraph_step":3,"langgraph_node":"chatbot","langgraph_triggers":["branch:to:chatbot"],"langgraph_path":["__pregel_pull","chatbot"],"langgraph_checkpoint_ns":"chatbot:06887461-c51c-2408-1eb8-45cebc9278d1","checkpoint_ns":"chatbot:06887461-c51c-2408-1eb8-45cebc9278d1","ls_provider":"openai","ls_model_name":"qwen-plus","ls_model_type":"chat","ls_temperature":null}]
id: 1757671736500-0

event: values
data: {"messages":[{"content":[{"type":"text","text":"朝鲜劳动党现任总书记是谁?"}],"additional_kwargs":{},"response_metadata":{},"type":"human","name":null,"id":"b92d5ae7-0bc4-463f-ab49-45e4cbe56f26","example":false},{"content":"","additional_kwargs":{"tool_calls":[{"index":0,"id":"call_aa235e37f2854788a2e9bb","function":{"arguments":"{\"query\": \"朝鲜劳动党现任总书记是谁?\"}","name":"tavily_search"},"type":"function"}]},"response_metadata":{"finish_reason":"tool_calls","model_name":"qwen-plus"},"type":"ai","name":null,"id":"run--b9aec800-2046-43ca-975b-400372de3d31","example":false,"tool_calls":[{"name":"tavily_search","args":{"query":"朝鲜劳动党现任总书记是谁?"},"id":"call_aa235e37f2854788a2e9bb","type":"tool_call"}],"invalid_tool_calls":[],"usage_metadata":null},{"content":"{\"query\": \"朝鲜劳动党现任总书记是谁?\", \"follow_up_questions\": null, \"answer\": null, \"images\": [], \"results\": [{\"url\": \"https://zh.wikipedia.org/zh-hans/%E6%9C%9D%E9%AE%AE%E5%8B%9E%E5%8B%95%E9%BB%A8%E7%B8%BD%E6%9B%B8%E8%A8%98\", \"title\": \"朝鲜劳动党总书记 - 维基百科\", \"content\": \"2021年1月10日,朝鲜劳动党第八次代表大会决议恢复总书记的头衔,并推举金正恩为总书记。\", \"score\": 0.8588255, \"raw_content\": null}, {\"url\": \"https://baike.baidu.com/item/%E6%9C%9D%E9%B2%9C%E5%8A%B3%E5%8A%A8%E5%85%9A%E6%80%BB%E4%B9%A6%E8%AE%B0/3895680\", \"title\": \"朝鲜劳动党总书记\", \"content\": \"中文名. 朝鲜劳动党总书记 · 外文名. 조선로동당총비서 · 别名. 朝鲜劳动党中央委员会总书记 · 实际地位. 朝鲜党和国家的最高领导人 · 现任领导. 金正恩.\", \"score\": 0.8585411, \"raw_content\": null}], \"response_time\": 1.29, \"request_id\": \"255b4a09-2b3f-41fd-a83d-6ac0588511b8\"}","additional_kwargs":{},"response_metadata":{},"type":"tool","name":"tavily_search","id":"23151eca-3d44-47e1-a0d0-67e59ed1f5de","tool_call_id":"call_aa235e37f2854788a2e9bb","artifact":null,"status":"success"},{"content":"朝鲜劳动党现任总书记是金正恩。","additional_kwargs":{},"response_metadata":{"finish_reason":"stop","model_name":"qwen-plus"},"type":"ai","name":null,"id":"run--2cb7c2b5-9217-4145-90c9-8029462f4dbf","example":false,"tool_calls":[],"invalid_tool_calls":[],"usage_metadata":null}]}
id: 1757671736512-0

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐