langgraph astream使用詳解

? ? ? ? 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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/922110.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/922110.shtml
英文地址,請注明出處:http://en.pswp.cn/news/922110.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【C++】模板進階:非類型參數、模板特化與分離編譯

目錄 1. 非類型模板參數 2. 模板的特化 3. 分離編譯 1. 非類型模板參數 模板參數分類類型形參與非類型形參。 類型形參即&#xff1a;出現在模板參數列表中&#xff0c;跟在class或者typename之類的參數類型名稱。 非類型形參&#xff0c;就是用一個常量作為類(函數)模板…

棧-1047.刪除字符串中的所有相鄰重復項-力扣(LeetCode)

一、題目解析 1、反復執行重復項刪除操作 2、s僅由小寫英文字母組成 二、算法原理 該題并不難&#xff0c;難的是能不能想到用棧這個數據結構解題 解法&#xff1a;棧模擬 橫著看起來不好理解&#xff0c;我們把它豎起來&#xff0c;是不是和消消樂很類似&#xff0c;兩兩消…

【每日算法】移除元素 LeetCode

雙指針方法是解決數組或鏈表問題中非常高效的技巧之一&#xff0c;尤其適用于原地修改數組或減少時間復雜度的場景。以下是對雙指針方法的詳細講解。1. 雙指針方法的核心思想雙指針方法通常使用兩個指針&#xff08;或索引&#xff09;在數組或鏈表中協同工作&#xff0c;通過一…

Android 項目:畫圖白板APP開發(六)——分頁展示

本篇將介紹如何為我們的畫板應用添加分頁展示功能&#xff0c;讓用戶可以創建多個畫布并在它們之間輕松切換。這章沒有啥知識點的講解&#xff0c;主要介紹一下每頁保存的數據結構是什么樣的。 一、ListView 多頁數據的管理我們使用ListView。之前有文章講過ListView這里就不多…

智能眼鏡產品成熟度分析框架與評估

引言 當前(2025年9月12日),智能眼鏡(Smart Glasses)市場正處于快速演進階段,從早期的新奇設備向主流消費電子轉型。AI整合、AR顯示和多模態交互的進步推動了這一轉變。根據最新數據,2025年AI眼鏡發貨量預計達686萬臺,同比增長265%,全球市場規模從2024年的約19.3億美元…

(網絡編程)網絡編程套接字 UDP的socket API 代碼解析

網絡編程基礎 為什么需要網絡編程?--豐富的網絡資源 用戶在瀏覽器中,打開在線視頻網站,如優酷看視頻,實質是通過網絡,獲取到網絡上的一個視頻資源。與本地打開視頻文件類似,只是視頻文件這個資源的來源是網絡。 相比本地資源來說,網絡提供了更為豐富的網絡資源:所謂的網絡資源…

【STM32】狀態機(State Machine)

這篇博客介紹 狀態機&#xff08;State Machine&#xff09;&#xff0c;適合用于嵌入式開發、驅動開發、協議解析、按鍵識別等多種場景。 一、什么是狀態機&#xff08;State Machine&#xff09;&#xff1f; 狀態機&#xff08;State Machine&#xff09;是一種用于描述系統…

深度學習在離崗檢測中的應用

離崗檢測技術正逐步成為現代企業精細化管理和安全生產的重要工具。這項基于計算機視覺和人工智能的應用&#xff0c;通過自動化、實時化的監測方式&#xff0c;有效提升了工作紀律性和運營效率&#xff0c;為項目管理者和企業提供了創新的監管解決方案。在許多工作場景中&#…

Spring緩存(二):解決緩存雪崩、擊穿、穿透問題

1. 緩存穿透問題與解決方案 1.1 什么是緩存穿透 緩存穿透是指查詢一個不存在的數據&#xff0c;由于緩存中沒有這個數據&#xff0c;每次請求都會直接打到數據庫。 如果有惡意用戶不斷請求不存在的數據&#xff0c;就會給數據庫帶來巨大壓力。 這種情況下&#xff0c;緩存失去了…

PHP 與 WebAssembly 的 “天然隔閡”

WebAssembly&#xff08;簡稱 WASM&#xff09;是一種低級二進制指令格式&#xff0c;旨在為高級語言提供高性能的編譯目標&#xff0c;尤其在瀏覽器環境中實現接近原生的執行效率。它主要用于前端性能密集型場景&#xff08;如游戲引擎、視頻編解碼、3D 渲染等&#xff09;&am…

unity中通過拖拽,自定義scroll view中子物體順序

1.在每個content的子物體上掛載DragHandler腳本&#xff0c;并且添加Canvs Group組件&#xff0c;設置見圖2.DragHandler腳本內容&#xff1a;using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections.Generic; using System.Coll…

用 Matplotlib 繪制餅圖:從基礎語法到實戰美化,全面掌握分類數據可視化技巧

用 Matplotlib 繪制餅圖:從基礎語法到實戰美化,全面掌握分類數據可視化技巧 在數據分析與可視化的世界里,**“圖勝千言”**早已成為共識。而在眾多圖表類型中,餅圖(Pie Chart)以其直觀的比例展示方式,成為展示分類數據分布的常見選擇。無論是業務報表、用戶畫像,還是市…

基礎算法之二分算法 --- 2

大家好&#xff0c;不同的時間&#xff0c;相同的地點&#xff0c;時隔多日我們又見面了。繼上次的二分算法后&#xff0c;我們這次要來學習的是二分答案了。這個部分相較于前面的二分算法難度有相當的提升&#xff0c;希望大家有所準備。雖然難度增加了&#xff0c;但是博主還…

發揮nano banana的最大能力

1. 概述Nano Banana 簡介&#xff1a;Nano Banana 是 Google DeepMind 開發的 AI 圖像生成與編輯模型&#xff0c;集成在 Google Gemini 平臺中&#xff08;具體為 Gemini 2.5 Flash 版本&#xff09;。它以高效的圖像編輯能力聞名&#xff0c;尤其在角色一致性、光影理解和快速…

leetcode 面試題01.02判定是否互為字符重排

一、問題描述二、解題思路解法一&#xff1a;對s1和s2進行sort排序&#xff0c;返回s1是否等于s2&#xff1b;解法二&#xff1a;用哈希表分別來記錄s1和s2中字符出現的次數&#xff0c;統計完后&#xff0c;判斷兩個哈希表是否相等;三、代碼實現解法一&#xff1a;時間復雜度&…

Python Yolo8 物體識別

支持單張圖片/圖片目錄批量預標注 默認使用cuda GPU .env HTTP_PROXYhttp://192.168.2.109:10808 HTTPS_PROXYhttp://192.168.2.109:10808pyproject.toml [project] name "yolo-test" version "0.1.0" description "Add your description here&quo…

LeetCode100-234回文鏈表

本文基于各個大佬的文章上點關注下點贊&#xff0c;明天一定更燦爛&#xff01;前言Python基礎好像會了又好像沒會&#xff0c;所有我直接開始刷leetcode一邊抄樣例代碼一邊學習吧。本系列文章用來記錄學習中的思考&#xff0c;寫給自己看的&#xff0c;也歡迎大家在評論區指導…

BUG排查流程

引言簡述Bug排查的重要性分享個人或團隊在Bug排查中的常見挑戰引出日記形式記錄的價值日記格式設計時間戳&#xff1a;記錄問題發現和解決的時間節點問題描述&#xff1a;清晰定義Bug的現象和影響范圍環境信息&#xff1a;操作系統、版本號、依賴庫等關鍵配置復現步驟&#xff…

汽車功能安全 Functional Safety ISO 26262 測試之一

汽車電子電氣系統的日益復雜使得功能安全成為保障車輛可靠性和駕乘安全的關鍵。 本文將圍繞ISO 26262標準的核心內容展開&#xff0c;幫助大家理解如何通過系統化的方法控制風險&#xff0c;進行測試&#xff0c;確保產品安全。 01 什么是功能安全&#xff1f; 首先&#xff0c…

人形機器人賽道的隱形勝負手:低延遲視頻鏈路如何決定機器人未來

一、引言&#xff1a;爆發前夜的人形機器人賽道 2025 年&#xff0c;被業內稱為“人形機器人量產元年”。政策與資本的合力&#xff0c;讓這條原本還帶著科幻色彩的產業賽道&#xff0c;驟然進入現實加速期。國家層面&#xff0c;《“機器人”行動計劃》明確提出要推動人形機器…