whileTrue: user_input = input("User: ") if user_input.lower() in ["quit", "exit", "q"]: print("Goodbye!") break for event in graph.stream({"messages": ("user", user_input)}): for value in event.values(): print("Assistant:", value["messages"][-1].content)
whileTrue: user_input = input("User: ") if user_input.lower() in ["quit", "exit", "q"]: print("Goodbye!") break for event in graph.stream({"messages": [("user", user_input)]}): for value in event.values(): print(value)
from typing import Annotated from typing_extensions import TypedDict
from langchain_community.tools.tavily_search import TavilySearchResults from langchain_openai import ChatOpenAI from langgraph.graph import StateGraph from langgraph.graph.message import add_messages from langgraph.prebuilt import ToolNode from langgraph.checkpoint.sqlite import SqliteSaver
last message: content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_xRK1M26mLAGIB0oKJvrivudr', 'function': {'arguments': '{"query":"LangGraph"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}]} response_metadata={'finish_reason': 'tool_calls'} id='run-bdc06d2d-021f-4922-8de4-0a7713c10aef-0' tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'LangGraph'}, 'id': 'call_xRK1M26mLAGIB0oKJvrivudr'}] value: {'messages': [AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_xRK1M26mLAGIB0oKJvrivudr', 'function': {'arguments': '{"query":"LangGraph"}', 'name': 'tavily_search_results_json'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls'}, id='run-bdc06d2d-021f-4922-8de4-0a7713c10aef-0', tool_calls=[{'name': 'tavily_search_results_json', 'args': {'query': 'LangGraph'}, 'id': 'call_xRK1M26mLAGIB0oKJvrivudr'}])]} value: {'messages': [ToolMessage(content='[{"url": "https://github.com/langchain-ai/langgraph/blob/main/README.md", "content": "Define the nodes\\nWe now need to define a few different nodes in our graph.\\\\nIn langgraph, a node can be either a function or a runnable.\\\\nThere are two main nodes we need for this:\\nWe will also need to define some edges.\\\\nSome of these edges may be conditional.\\\\nThe reason they are conditional is that based on the output of a node, one of several paths may be taken.\\\\nThe path that is taken is not known until that node is run (the LLM decides).\\n LangChain.\\\\nIt extends the LangChain Expression Language with the ability to coordinate multiple chains (or actors) across multiple steps of computation in a cyclic manner.\\\\nIt is inspired by Pregel and Apache Beam.\\\\nThe current interface exposed is one inspired by NetworkX.\\nThe main use is for adding cycles to your LLM application.\\\\nCrucially, this is NOT a DAG framework.\\\\nIf you want to build a DAG, you should use just use LangChain Expression Language.\\n This is a special node representing the end of the graph.\\\\nThis means that anything passed to this node will be the final output of the graph.\\\\nIt can be used in two places:\\nWhen to Use\\nWhen should you use this versus LangChain Expression Language?\\n This method adds a node to the graph.\\\\nIt takes two arguments:\\n.add_edge\\nCreates an edge from one node to the next.\\\\nThis means that output of the first node will be passed to the next node.\\\\nIt takes two arguments.\\n Assuming you have done the above Quick Start, you can build off it like:\\nHere, we manually define the first tool call that we will make.\\\\nNotice that it does that same thing as agent would have done (adds the agent_outcome key).\\\\nThis is so that we can easily plug it in.\\n"}, {"url": "https://blog.langchain.dev/langgraph-multi-agent-workflows/", "content": "As a part of the launch, we highlighted two simple runtimes: one that is the equivalent of the AgentExecutor in langchain, and a second that was a version of that aimed at message passing and chat models.\\n It\'s important to note that these three examples are only a few of the possible examples we could highlight - there are almost assuredly other examples out there and we look forward to seeing what the community comes up with!\\n LangGraph: Multi-Agent Workflows\\nLinks\\nLast week we highlighted LangGraph - a new package (available in both Python and JS) to better enable creation of LLM workflows containing cycles, which are a critical component of most agent runtimes. \\"\\nAnother key difference between Autogen and LangGraph is that LangGraph is fully integrated into the LangChain ecosystem, meaning you take fully advantage of all the LangChain integrations and LangSmith observability.\\n As part of this launch, we\'re also excited to highlight a few applications built on top of LangGraph that utilize the concept of multiple agents.\\n"}, {"url": "https://python.langchain.com/docs/langgraph/", "content": "LangGraph is a library for building stateful, multi-actor applications with LLMs, built on top of (and intended to be used with) LangChain . It extends the LangChain Expression Language with the ability to coordinate multiple chains (or actors) across multiple steps of computation in a cyclic manner. It is inspired by Pregel and Apache Beam ."}]', name='tavily_search_results_json', id='b75137fb-86fa-477c-b4c7-79fec35a80be', tool_call_id='call_xRK1M26mLAGIB0oKJvrivudr')]} last message: content='I found some information about LangGraph:\n\n1. LangGraph is a library for building stateful, multi-actor applications with LLMs, built on top of LangChain. It extends the LangChain Expression Language with the ability to coordinate multiple chains (or actors) across multiple steps of computation in a cyclic manner. It is inspired by Pregel and Apache Beam. [Source](https://python.langchain.com/docs/langgraph/)\n\n2. LangGraph allows the creation of LLM workflows containing cycles, which are essential for most agent runtimes. It is available in both Python and JS and is fully integrated into the LangChain ecosystem, providing advantages in terms of integrations and observability. [Source](https://blog.langchain.dev/langgraph-multi-agent-workflows/)\n\n3. In LangGraph, nodes can be functions or runnables, and edges can be conditional based on the output of a node. LangGraph is used for adding cycles to LLM applications and is not a DAG framework. [Source](https://github.com/langchain-ai/langgraph/blob/main/README.md)\n\nThese sources provide more detailed information about LangGraph and its capabilities.' response_metadata={'finish_reason': 'stop'} id='run-4426b55f-4c9f-4fac-b9a9-ce0f98b6a73a-0' value: {'messages': [AIMessage(content='I found some information about LangGraph:\n\n1. LangGraph is a library for building stateful, multi-actor applications with LLMs, built on top of LangChain. It extends the LangChain Expression Language with the ability to coordinate multiple chains (or actors) across multiple steps of computation in a cyclic manner. It is inspired by Pregel and Apache Beam. [Source](https://python.langchain.com/docs/langgraph/)\n\n2. LangGraph allows the creation of LLM workflows containing cycles, which are essential for most agent runtimes. It is available in both Python and JS and is fully integrated into the LangChain ecosystem, providing advantages in terms of integrations and observability. [Source](https://blog.langchain.dev/langgraph-multi-agent-workflows/)\n\n3. In LangGraph, nodes can be functions or runnables, and edges can be conditional based on the output of a node. LangGraph is used for adding cycles to LLM applications and is not a DAG framework. [Source](https://github.com/langchain-ai/langgraph/blob/main/README.md)\n\nThese sources provide more detailed information about LangGraph and its capabilities.', response_metadata={'finish_reason': 'stop'}, id='run-4426b55f-4c9f-4fac-b9a9-ce0f98b6a73a-0')]}
events = graph.stream(None, config) for event in events: for value in event.values(): print(f"value: {value}")
最后执行的输出结果是:
1 2 3
value: {'messages': [ToolMessage(content='[{"url": "https://python.langchain.com/docs/langgraph/", "content": "LangGraph is a library for building stateful, multi-actor applications with LLMs, built on top of (and intended to be used with) LangChain . It extends the LangChain Expression Language with the ability to coordinate multiple chains (or actors) across multiple steps of computation in a cyclic manner. It is inspired by Pregel and Apache Beam ."}, {"url": "https://github.com/langchain-ai/langgraph/blob/main/README.md", "content": "Define the nodes\\nWe now need to define a few different nodes in our graph.\\\\nIn langgraph, a node can be either a function or a runnable.\\\\nThere are two main nodes we need for this:\\nWe will also need to define some edges.\\\\nSome of these edges may be conditional.\\\\nThe reason they are conditional is that based on the output of a node, one of several paths may be taken.\\\\nThe path that is taken is not known until that node is run (the LLM decides).\\n LangChain.\\\\nIt extends the LangChain Expression Language with the ability to coordinate multiple chains (or actors) across multiple steps of computation in a cyclic manner.\\\\nIt is inspired by Pregel and Apache Beam.\\\\nThe current interface exposed is one inspired by NetworkX.\\nThe main use is for adding cycles to your LLM application.\\\\nCrucially, this is NOT a DAG framework.\\\\nIf you want to build a DAG, you should use just use LangChain Expression Language.\\n This is a special node representing the end of the graph.\\\\nThis means that anything passed to this node will be the final output of the graph.\\\\nIt can be used in two places:\\nWhen to Use\\nWhen should you use this versus LangChain Expression Language?\\n This method adds a node to the graph.\\\\nIt takes two arguments:\\n.add_edge\\nCreates an edge from one node to the next.\\\\nThis means that output of the first node will be passed to the next node.\\\\nIt takes two arguments.\\n Assuming you have done the above Quick Start, you can build off it like:\\nHere, we manually define the first tool call that we will make.\\\\nNotice that it does that same thing as agent would have done (adds the agent_outcome key).\\\\nThis is so that we can easily plug it in.\\n"}, {"url": "https://blog.langchain.dev/langgraph-multi-agent-workflows/", "content": "As a part of the launch, we highlighted two simple runtimes: one that is the equivalent of the AgentExecutor in langchain, and a second that was a version of that aimed at message passing and chat models.\\n It\'s important to note that these three examples are only a few of the possible examples we could highlight - there are almost assuredly other examples out there and we look forward to seeing what the community comes up with!\\n LangGraph: Multi-Agent Workflows\\nLinks\\nLast week we highlighted LangGraph - a new package (available in both Python and JS) to better enable creation of LLM workflows containing cycles, which are a critical component of most agent runtimes. \\"\\nAnother key difference between Autogen and LangGraph is that LangGraph is fully integrated into the LangChain ecosystem, meaning you take fully advantage of all the LangChain integrations and LangSmith observability.\\n As part of this launch, we\'re also excited to highlight a few applications built on top of LangGraph that utilize the concept of multiple agents.\\n"}]', name='tavily_search_results_json', id='b1f6c679-eecd-44d8-8610-17f1dd5c14d4', tool_call_id='call_FytVzAxqElqPgkAVeKFVfeJq')]} last message: content='LangGraph is a library for building stateful, multi-actor applications with LLMs, built on top of LangChain. It extends the LangChain Expression Language with the ability to coordinate multiple chains (or actors) across multiple steps of computation in a cyclic manner. LangGraph is inspired by Pregel and Apache Beam.\n\nYou can find more information about LangGraph on the following links:\n1. [LangGraph Documentation](https://python.langchain.com/docs/langgraph/): Provides details on how LangGraph works and its features.\n2. [LangGraph GitHub Repository](https://github.com/langchain-ai/langgraph/blob/main/README.md): Explains how to define nodes and edges in LangGraph and provides insights into its usage.\n3. [LangGraph Multi-Agent Workflows Blog Post](https://blog.langchain.dev/langgraph-multi-agent-workflows/): Discusses the use of LangGraph for multi-agent workflows and highlights applications built on top of LangGraph.\n\nLangGraph is designed to enable the creation of LLM workflows containing cycles, which are essential for most agent runtimes. It offers a way to coordinate multiple actors in a cyclic manner within LangChain applications.' response_metadata={'finish_reason': 'stop'} id='run-342ae745-57ae-43ff-a274-c8942e55e95a-0' value: {'messages': [AIMessage(content='LangGraph is a library for building stateful, multi-actor applications with LLMs, built on top of LangChain. It extends the LangChain Expression Language with the ability to coordinate multiple chains (or actors) across multiple steps of computation in a cyclic manner. LangGraph is inspired by Pregel and Apache Beam.\n\nYou can find more information about LangGraph on the following links:\n1. [LangGraph Documentation](https://python.langchain.com/docs/langgraph/): Provides details on how LangGraph works and its features.\n2. [LangGraph GitHub Repository](https://github.com/langchain-ai/langgraph/blob/main/README.md): Explains how to define nodes and edges in LangGraph and provides insights into its usage.\n3. [LangGraph Multi-Agent Workflows Blog Post](https://blog.langchain.dev/langgraph-multi-agent-workflows/): Discusses the use of LangGraph for multi-agent workflows and highlights applications built on top of LangGraph.\n\nLangGraph is designed to enable the creation of LLM workflows containing cycles, which are essential for most agent runtimes. It offers a way to coordinate multiple actors in a cyclic manner within LangChain applications.', response_metadata={'finish_reason': 'stop'}, id='run-342ae745-57ae-43ff-a274-c8942e55e95a-0')]}