赞
踩
LangChain系列文章
LangGraph支持几种不同类型的流媒体。
使用LangGraph的好处之一是很容易将每个节点产生的输出作为流媒体进行传输。
inputs = {"messages": [HumanMessage(content="what is the weather in sf")]}
for output in app.stream(inputs):
# stream() yields dictionaries with output keyed by node name
for key, value in output.items():
print(f"Output from node '{key}':")
print("---")
print(value)
print("\n---\n")
Output from node 'agent': --- {'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\n "query": "weather in San Francisco"\n}', 'name': 'tavily_search_results_json'}})]} --- Output from node 'action': --- {'messages': [FunctionMessage(content="[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]", name='tavily_search_results_json')]} --- Output from node 'agent': --- {'messages': [AIMessage(content="I couldn't find the current weather in San Francisco. However, you can visit [WeatherSpark](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States) to check the historical weather data for January 2024 in San Francisco.")]} --- Output from node '__end__': --- {'messages': [HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\n "query": "weather in San Francisco"\n}', 'name': 'tavily_search_results_json'}}), FunctionMessage(content="[{'url': 'https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States', 'content': 'January 2024 Weather History in San Francisco California, United States Daily Precipitation in January 2024 in San Francisco Observed Weather in January 2024 in San Francisco San Francisco Temperature History January 2024 Hourly Temperature in January 2024 in San Francisco Hours of Daylight and Twilight in January 2024 in San FranciscoThis report shows the past weather for San Francisco, providing a weather history for January 2024. It features all historical weather data series we have available, including the San Francisco temperature history for January 2024. You can drill down from year to month and even day level reports by clicking on the graphs.'}]", name='tavily_search_results_json'), AIMessage(content="I couldn't find the current weather in San Francisco. However, you can visit [WeatherSpark](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States) to check the historical weather data for January 2024 in San Francisco.")]} ---
您也可以在每个节点生成LLM令牌时访问这些LLM令牌。在这种情况下,只有"agent"节点会生成LLM令牌。为了使其正常工作,您必须使用一个支持流式传输的LLM,并在构建LLM时设置它(例如ChatOpenAI(model="gpt-3.5-turbo-1106", streaming=True)
)。
inputs = {"messages": [HumanMessage(content="what is the weather in sf")]}
async for output in app.astream_log(inputs, include_types=["llm"]):
# astream_log() yields the requested logs (here LLMs) in JSONPatch format
for op in output.ops:
if op["path"] == "/streamed_output/-":
# this is the output from .stream()
...
elif op["path"].startswith("/logs/") and op["path"].endswith(
"/streamed_output/-"
):
# because we chose to only include LLMs, these are LLM tokens
print(op["value"])
content='' additional_kwargs={'function_call': {'arguments': '', 'name': 'tavily_search_results_json'}} content='' additional_kwargs={'function_call': {'arguments': '{\n', 'name': ''}} content='' additional_kwargs={'function_call': {'arguments': ' ', 'name': ''}} content='' additional_kwargs={'function_call': {'arguments': ' "', 'name': ''}} content='' additional_kwargs={'function_call': {'arguments': 'query', 'name': ''}} content='' additional_kwargs={'function_call': {'arguments': '":', 'name': ''}} content='' additional_kwargs={'function_call': {'arguments': ' "', 'name': ''}} content='' additional_kwargs={'function_call': {'arguments': 'weather', 'name': ''}} content='' additional_kwargs={'function_call': {'arguments': ' in', 'name': ''}} content='' additional_kwargs={'function_call': {'arguments': ' San', 'name': ''}} content='' additional_kwargs={'function_call': {'arguments': ' Francisco', 'name': ''}} content='' additional_kwargs={'function_call': {'arguments': '"\n', 'name': ''}} content='' additional_kwargs={'function_call': {'arguments': '}', 'name': ''}} content='' content='' content='I' content="'m" content=' sorry' content=',' content=' but' content=' I' content=' couldn' content="'t" content=' find' content=' the' content=' current' content=' weather' content=' in' content=' San' content=' Francisco' content='.' content=' However' content=',' content=' you' content=' can' content=' check' content=' the' content=' historical' content=' weather' content=' data' content=' for' content=' January' content=' ' content='202' content='4' content=' in' content=' San' content=' Francisco' content=' [' content='here' content='](' content='https' content='://' content='we' content='athers' content='park' content='.com' content='/h' content='/m' content='/' content='557' content='/' content='202' content='4' content='/' content='1' content='/H' content='istorical' content='-' content='Weather' content='-in' content='-Jan' content='uary' content='-' content='202' content='4' content='-in' content='-S' content='an' content='-F' content='r' content='anc' content='isco' content='-Cal' content='ifornia' content='-' content='United' content='-' content='States' content=').' content=''
何时应该使用这个而不是LangChain表达语言?
如果你需要循环。
Langchain表达语言允许您轻松定义链 (DAGs),但没有很好的机制来添加循环。langgraph
添加了这个语法。
https://github.com/zgpeace/pets-name-langchain/tree/develop
https://python.langchain.com/docs/langsmith/walkthrough
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。