? ? ? ? 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_conditionos.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_writeros.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-0event: 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-0event: 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-0event: 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-0event: 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-0event: 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-0event: 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-0event: 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-0event: 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-0event: 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-0event: 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-0event: 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-0event: 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-0event: 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-0event: 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