diff --git a/README.md b/README.md index 177003c74..2f806256e 100644 --- a/README.md +++ b/README.md @@ -417,7 +417,6 @@ If you need cycles. Langchain Expression Language allows you to easily define chains (DAGs) but does not have a good mechanism for adding in cycles. `langgraph` adds that syntax. - ## How-to Guides These guides show how to use LangGraph in particular ways. @@ -448,6 +447,12 @@ Agents you create with LangGraph can be complex. In order to make it easier to u This can create both ascii art as well as pngs. For a walkthrough on how to do that, see [this documentation](https://github.com/langchain-ai/langgraph/blob/main/examples/visualization.ipynb) +### "Time Travel" + +With "time travel" functionality you can jump to any point in the graph execution, modify the state, and rerun from there. +This is useful for both debugging workflows, as well as end user-facing workflows to allow them to correct the state. +For a walkthrough on how to do that, see [this documentation](https://github.com/langchain-ai/langgraph/blob/main/examples/time-travel.ipynb) + ## Examples @@ -486,7 +491,6 @@ We also have a lot of examples highlighting how to slightly modify the base chat - [Force calling a tool first](https://github.com/langchain-ai/langgraph/blob/main/examples/agent_executor/force-calling-a-tool-first.ipynb): How to always call a specific tool first - [Managing agent steps](https://github.com/langchain-ai/langgraph/blob/main/examples/agent_executor/managing-agent-steps.ipynb): How to more explicitly manage intermediate steps that an agent takes - ### Planning Agent Examples The following notebooks implement agent architectures prototypical of the "plan-and-execute" style, where an LLM planner decomposes a user request into a program, an executor executes the program, and an LLM synthesizes a response (and/or dynamically replans) based on the program outputs. @@ -495,7 +499,6 @@ The following notebooks implement agent architectures prototypical of the "plan- - [Reasoning without Observation](https://github.com/langchain-ai/langgraph/blob/main/examples/rewoo/rewoo.ipynb): planner generates a task list whose observations are saved as **variables**. Variables can be used in subsequent tasks to reduce the need for further re-planning. Based on the [ReWOO](https://arxiv.org/abs/2305.18323) paper by Xu, et. al. - [LLMCompiler](https://github.com/langchain-ai/langgraph/blob/main/examples/llm-compiler/LLMCompiler.ipynb): planner generates a **DAG** of tasks with variable responses. Tasks are **streamed** and executed eagerly to minimize tool execution runtime. Based on the [paper](https://arxiv.org/abs/2312.04511) by Kim, et. al. - ### Reflection / Self-Critique When output quality is a major concern, it's common to incorporate some combination of self-critique or reflection and external validation to refine your system's outputs. The following examples demonstrate research that implement this type of design. @@ -529,7 +532,6 @@ It can often be tough to evaluation chat bots in multi-turn situations. One way [Chain of Table](https://arxiv.org/abs/2401.04398) is a framework that elicits SOTA performance when answering questions over tabular data. [This implementation](https://github.com/CYQIQ/MultiCoT) by Github user [CYQIQ](https://github.com/CYQIQ) uses LangGraph to control the flow. - ## Documentation There are only a few new APIs to use. @@ -679,7 +681,6 @@ What this means is that when the graph is called, it will call the `condition` C - `condition`: A function to call to decide what to do next. The input will be the input to the graph. It should return a string that is present in `conditional_edge_mapping` and represents the edge to take. - `conditional_edge_mapping`: A mapping of string to string. The keys should be strings that may be returned by `condition`. The values should be the downstream node to call if that condition is returned. - #### `.set_finish_point` ```python diff --git a/examples/chat_agent_executor_with_function_calling/high-level.ipynb b/examples/chat_agent_executor_with_function_calling/high-level.ipynb index f4fc724fc..a377367eb 100644 --- a/examples/chat_agent_executor_with_function_calling/high-level.ipynb +++ b/examples/chat_agent_executor_with_function_calling/high-level.ipynb @@ -85,13 +85,13 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})]}\n", + "{'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"weather in San Francisco\"}', 'name': 'tavily_search_results_json'}})]}\n", "----\n", - "{'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')]}\n", + "{'messages': [FunctionMessage(content=\"[{'url': 'https://www.accuweather.com/en/us/san-francisco/94103/weather-forecast/347629', 'content': 'Get the current and future weather conditions for San Francisco, CA, including temperature, precipitation, wind, air quality and more. See the hourly and 10-day outlook, radar maps, alerts and allergy information.'}]\", name='tavily_search_results_json')]}\n", "----\n", - "{'messages': [AIMessage(content=\"I couldn't find the current weather in San Francisco. However, you can check historical weather data for January 2024 [here](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).\")]}\n", + "{'messages': [AIMessage(content='You can check the current and future weather conditions for San Francisco, CA on [AccuWeather](https://www.accuweather.com/en/us/san-francisco/94103/weather-forecast/347629).')]}\n", "----\n", - "{'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 check historical weather data for January 2024 [here](https://weatherspark.com/h/m/557/2024/1/Historical-Weather-in-January-2024-in-San-Francisco-California-United-States).\")]}\n", + "{'messages': [HumanMessage(content='what is the weather in sf'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"weather in San Francisco\"}', 'name': 'tavily_search_results_json'}}), FunctionMessage(content=\"[{'url': 'https://www.accuweather.com/en/us/san-francisco/94103/weather-forecast/347629', 'content': 'Get the current and future weather conditions for San Francisco, CA, including temperature, precipitation, wind, air quality and more. See the hourly and 10-day outlook, radar maps, alerts and allergy information.'}]\", name='tavily_search_results_json'), AIMessage(content='You can check the current and future weather conditions for San Francisco, CA on [AccuWeather](https://www.accuweather.com/en/us/san-francisco/94103/weather-forecast/347629).')]}\n", "----\n" ] } diff --git a/examples/human-in-the-loop.ipynb b/examples/human-in-the-loop.ipynb index aaeef496e..fef9e0737 100644 --- a/examples/human-in-the-loop.ipynb +++ b/examples/human-in-the-loop.ipynb @@ -340,7 +340,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 7, "id": "6845ed6a-d155-4105-9160-28849877248b", "metadata": {}, "outputs": [], @@ -362,7 +362,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 8, "id": "79d29875-8aa8-434c-9f20-1c58346a6249", "metadata": {}, "outputs": [], @@ -385,7 +385,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 9, "id": "cfd140f0-a5a6-4697-8115-322242f197b5", "metadata": {}, "outputs": [ @@ -409,7 +409,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 10, "id": "08ae8246-11d5-40e1-8567-361e5bef8917", "metadata": {}, "outputs": [ @@ -417,7 +417,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "content='Your name is Bob.'\n" + "content='Your name is Bob! How can I help you, Bob?'\n" ] } ], @@ -431,7 +431,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 11, "id": "273d56a8-f40f-4a51-a27f-7c6bb2bda0ba", "metadata": {}, "outputs": [ @@ -439,7 +439,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "content='' additional_kwargs={'function_call': {'arguments': '{\\n \"query\": \"weather in San Francisco now\"\\n}', 'name': 'tavily_search_results_json'}}\n" + "content='' additional_kwargs={'function_call': {'arguments': '{\"query\":\"current weather in San Francisco\"}', 'name': 'tavily_search_results_json'}}\n" ] } ], @@ -463,7 +463,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 12, "id": "51923913-20f7-4ee1-b9ba-d01f5fb2869b", "metadata": {}, "outputs": [ @@ -471,8 +471,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "content='[{\\'url\\': \\'https://www.sfexaminer.com/news/climate/san-francisco-weather-forecast-calls-for-strongest-2024-rain/article_75347810-bfc3-11ee-abf6-e74c528e0583.html\\', \\'content\\': \"San Francisco is projected to receive 2.5 and 3 inches of rain, Clouser said, as well as gusts of wind up to 45 mph. Bay Area starting Wednesday at 4 a.m. and a 24-hour wind advisory in San Francisco starting at the same time. One of winter\\'s \\'stronger\\' storms to douse San Francisco On the heels of record-breaking heat to open the week, heavy rain is slated to pound the Bay Area on Wednesday.A series of historic storms last winter led to one of the wettest water years (Oct. 1 to Sept. 30) in The City\\'s history, highlighted by a 10-day stretch last January in which San Francisco...\"}]' name='tavily_search_results_json'\n", - "content=\"Currently, I couldn't retrieve the exact weather information for San Francisco. However, there is a forecast of heavy rain and gusts of wind up to 45 mph in San Francisco starting Wednesday at 4 a.m. You may want to check a reliable weather website or app for the most up-to-date weather conditions.\"\n" + "content='[{\\'url\\': \\'https://weather.com/weather/hourbyhour/l/USCA0987:1:US\\', \\'content\\': \"recents\\\\nSpecialty Forecasts\\\\nHourly Weather-San Francisco, CA\\\\nBeach Hazard Statement\\\\nSaturday, November 25\\\\n5 pm\\\\nClear\\\\n6 pm\\\\nClear\\\\n7 pm\\\\nClear\\\\n8 pm\\\\nMostly Clear\\\\n9 pm\\\\nPartly Cloudy\\\\n10 pm\\\\nPartly Cloudy\\\\n11 pm\\\\nPartly Cloudy\\\\nSunday, November 26\\\\n12 am\\\\nPartly Cloudy\\\\n1 am\\\\nMostly Cloudy\\\\n2 am\\\\nMostly Cloudy\\\\n3 am\\\\nMostly Cloudy\\\\n4 am\\\\nCloudy\\\\n5 am\\\\nCloudy\\\\n6 am\\\\nMostly Cloudy\\\\n7 am\\\\nMostly Cloudy\\\\n8 am\\\\nMostly Cloudy\\\\n9 am\\\\nMostly Cloudy\\\\n10 am\\\\nPartly Cloudy\\\\n11 am\\\\nPartly Cloudy\\\\n12 pm\\\\nPartly Cloudy\\\\n1 pm\\\\nPartly Cloudy\\\\n2 pm\\\\nPartly Cloudy\\\\n3 pm\\\\nPartly Cloudy\\\\n4 pm\\\\nPartly Cloudy\\\\n5 pm\\\\nPartly Cloudy\\\\n6 pm\\\\nPartly Cloudy\\\\n7 pm\\\\nPartly Cloudy\\\\n8 pm\\\\nPartly Cloudy\\\\n9 pm\\\\nMostly Clear\\\\n10 pm\\\\nMostly Clear\\\\n11 pm\\\\nMostly Clear\\\\nMonday, November 27\\\\n12 am\\\\nClear\\\\n1 am\\\\nMostly Clear\\\\n2 am\\\\nMostly Clear\\\\n3 am\\\\nPartly Cloudy\\\\n4 am\\\\nMostly Clear\\\\n5 am\\\\nMostly Clear\\\\n6 am\\\\nClear\\\\n7 am\\\\nClear\\\\n8 am\\\\nSunny\\\\n9 am\\\\nSunny\\\\n10 am\\\\nSunny\\\\n11 am\\\\nSunny\\\\n12 pm\\\\nMostly Sunny\\\\n1 pm\\\\nPartly Cloudy\\\\n2 pm\\\\nMostly Sunny\\\\n3 pm\\\\nMostly Sunny\\\\n4 pm\\\\nPartly Cloudy\\\\nRadar\\\\nSafety First!\\\\n Don\\'t Miss\\\\nIrresistible\\\\nWeather Wonders\\\\nOur Amazing World\\\\nCelestial Symphony\\\\nFried Turkey Fail\\\\nLook At That!\\\\n Health & Activities\\\\nSeasonal Allergies and Pollen Count Forecast\\\\nNo pollen detected in your area\\\\nCold & Flu Forecast\\\\nFlu risk is low in your area\\\\nWe recognize our responsibility to use data and technology for good. Changes For Critters\\\\nHurricane Tracker\\\\nStay Safe\\\\nAir Quality Index\\\\nAir quality is considered satisfactory, and air pollution poses little or no risk.\\\\n Take control of your data.\\\\n\"}]' name='tavily_search_results_json'\n", + "content='The current weather in San Francisco is partly cloudy. The temperature is expected to be pleasant. If you need more detailed information, you can visit [this link](https://weather.com/weather/hourbyhour/l/USCA0987:1:US). Is there anything else you would like to know, Bob?'\n" ] } ], @@ -500,7 +500,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.1" + "version": "3.11.8" } }, "nbformat": 4, diff --git a/examples/time-travel.ipynb b/examples/time-travel.ipynb new file mode 100644 index 000000000..7a9c1dc84 --- /dev/null +++ b/examples/time-travel.ipynb @@ -0,0 +1,730 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53", + "metadata": {}, + "source": [ + "# Get/Update State\n", + "\n", + "When running LangGraph agents, you can easily get or update the state of the agent at any point in time. This allows for several things. Firstly, it allows you to inspect the state and take actions accordingly. Second, it allows you to modify the state - this can be useful for changing or correcting potential actions.\n", + "\n", + "**Note:** this requires passing in a checkpointer." + ] + }, + { + "cell_type": "markdown", + "id": "7cbd446a-808f-4394-be92-d45ab818953c", + "metadata": {}, + "source": [ + "## Setup\n", + "\n", + "First we need to install the packages required" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "af4ce0ba-7596-4e5f-8bf8-0b0bd6e62833", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.2\u001b[0m\n", + "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n" + ] + } + ], + "source": [ + "!pip install --quiet -U langchain langchain_openai tavily-python" + ] + }, + { + "cell_type": "markdown", + "id": "0abe11f4-62ed-4dc4-8875-3db21e260d1d", + "metadata": {}, + "source": [ + "Next, we need to set API keys for OpenAI (the LLM we will use) and Tavily (the search tool we will use)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "c903a1cf-2977-4e2d-ad7d-8b3946821d89", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "OpenAI API Key: ········\n", + "Tavily API Key: ········\n" + ] + } + ], + "source": [ + "import os\n", + "import getpass\n", + "\n", + "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", + "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" + ] + }, + { + "cell_type": "markdown", + "id": "f0ed46a8-effe-4596-b0e1-a6a29ee16f5c", + "metadata": {}, + "source": [ + "Optionally, we can set API key for [LangSmith tracing](https://smith.langchain.com/), which will give us best-in-class observability." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "95e25aec-7c9f-4a63-b143-225d0e9a79c3", + "metadata": {}, + "outputs": [], + "source": [ + "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"LangSmith API Key:\")" + ] + }, + { + "cell_type": "markdown", + "id": "21ac643b-cb06-4724-a80c-2862ba4773f1", + "metadata": {}, + "source": [ + "## Set up the tools\n", + "\n", + "We will first define the tools we want to use.\n", + "For this simple example, we will use a built-in search tool via Tavily.\n", + "However, it is really easy to create your own tools - see documentation [here](https://python.langchain.com/docs/modules/agents/tools/custom_tools) on how to do that.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "d7ef57dd-5d6e-4ad3-9377-a92201c1310e", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_community.tools.tavily_search import TavilySearchResults\n", + "\n", + "tools = [TavilySearchResults(max_results=1)]" + ] + }, + { + "cell_type": "markdown", + "id": "01885785-b71a-44d1-b1d6-7b5b14d53b58", + "metadata": {}, + "source": [ + "We can now wrap these tools in a simple ToolExecutor.\n", + "This is a real simple class that takes in a ToolInvocation and calls that tool, returning the output.\n", + "A ToolInvocation is any class with `tool` and `tool_input` attribute.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "5cf3331e-ccb3-41c8-aeb9-a840a94d41e7", + "metadata": {}, + "outputs": [], + "source": [ + "from langgraph.prebuilt import ToolExecutor\n", + "\n", + "tool_executor = ToolExecutor(tools)" + ] + }, + { + "cell_type": "markdown", + "id": "5497ed70-fce3-47f1-9cad-46f912bad6a5", + "metadata": {}, + "source": [ + "## Set up the model\n", + "\n", + "Now we need to load the chat model we want to use.\n", + "Importantly, this should satisfy two criteria:\n", + "\n", + "1. It should work with messages. We will represent all agent state in the form of messages, so it needs to be able to work well with them.\n", + "2. It should work with OpenAI function calling. This means it should either be an OpenAI model or a model that exposes a similar interface.\n", + "\n", + "Note: these model requirements are not requirements for using LangGraph - they are just requirements for this one example." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "892b54b9-75f0-4804-9ed0-88b5e5532989", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_openai import ChatOpenAI\n", + "\n", + "model = ChatOpenAI(temperature=0)" + ] + }, + { + "cell_type": "markdown", + "id": "a77995c0-bae2-4cee-a036-8688a90f05b9", + "metadata": {}, + "source": [ + "\n", + "After we've done this, we should make sure the model knows that it has these tools available to call.\n", + "We can do this by converting the LangChain tools into the format for OpenAI function calling, and then bind them to the model class.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "cd3cbae5-d92c-4559-a4aa-44721b80d107", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_core.utils.function_calling import convert_to_openai_function\n", + "\n", + "functions = [convert_to_openai_function(t) for t in tools]\n", + "model = model.bind_functions(functions)" + ] + }, + { + "cell_type": "markdown", + "id": "e03c5094-9297-4d19-a04e-3eedc75cefb4", + "metadata": {}, + "source": [ + "## Define the nodes\n", + "\n", + "We now need to define a few different nodes in our graph.\n", + "In `langgraph`, a node can be either a function or a [runnable](https://python.langchain.com/docs/expression_language/).\n", + "There are two main nodes we need for this:\n", + "\n", + "1. The agent: responsible for deciding what (if any) actions to take.\n", + "2. A function to invoke tools: if the agent decides to take an action, this node will then execute that action.\n", + "\n", + "We will also need to define some edges.\n", + "Some of these edges may be conditional.\n", + "The reason they are conditional is that based on the output of a node, one of several paths may be taken.\n", + "The path that is taken is not known until that node is run (the LLM decides).\n", + "\n", + "1. Conditional Edge: after the agent is called, we should either:\n", + " a. If the agent said to take an action, then the function to invoke tools should be called\n", + " b. If the agent said that it was finished, then it should finish\n", + "2. Normal Edge: after the tools are invoked, it should always go back to the agent to decide what to do next\n", + "\n", + "Let's define the nodes, as well as a function to decide how what conditional edge to take." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "3b541bb9-900c-40d0-964d-7b5dfee30667", + "metadata": {}, + "outputs": [], + "source": [ + "from langgraph.prebuilt import ToolInvocation\n", + "import json\n", + "from langchain_core.messages import FunctionMessage\n", + "\n", + "\n", + "# Define the function that determines whether to continue or not\n", + "def should_continue(messages):\n", + " last_message = messages[-1]\n", + " # If there is no function call, then we finish\n", + " if \"function_call\" not in last_message.additional_kwargs:\n", + " return \"end\"\n", + " # Otherwise if there is, we continue\n", + " else:\n", + " return \"continue\"\n", + "\n", + "\n", + "# Define the function that calls the model\n", + "def call_model(messages):\n", + " response = model.invoke(messages)\n", + " # We return a list, because this will get added to the existing list\n", + " return response\n", + "\n", + "\n", + "# Define the function to execute tools\n", + "def call_tool(messages):\n", + " # Based on the continue condition\n", + " # we know the last message involves a function call\n", + " last_message = messages[-1]\n", + " # We construct an ToolInvocation from the function_call\n", + " action = ToolInvocation(\n", + " tool=last_message.additional_kwargs[\"function_call\"][\"name\"],\n", + " tool_input=json.loads(\n", + " last_message.additional_kwargs[\"function_call\"][\"arguments\"]\n", + " ),\n", + " )\n", + " # We call the tool_executor and get back a response\n", + " response = tool_executor.invoke(action)\n", + " # We use the response to create a FunctionMessage\n", + " function_message = FunctionMessage(content=str(response), name=action.tool)\n", + " # We return a list, because this will get added to the existing list\n", + " return function_message" + ] + }, + { + "cell_type": "markdown", + "id": "ffd6e892-946c-4899-8cc0-7c9291c1f73b", + "metadata": {}, + "source": [ + "## Define the graph\n", + "\n", + "We can now put it all together and define the graph!" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "812b4e70-4956-4415-8880-db48b3dcbad2", + "metadata": {}, + "outputs": [], + "source": [ + "from langgraph.graph import MessageGraph, END\n", + "\n", + "# Define a new graph\n", + "workflow = MessageGraph()\n", + "\n", + "# Define the two nodes we will cycle between\n", + "workflow.add_node(\"agent\", call_model)\n", + "workflow.add_node(\"action\", call_tool)\n", + "\n", + "# Set the entrypoint as `agent`\n", + "# This means that this node is the first one called\n", + "workflow.set_entry_point(\"agent\")\n", + "\n", + "# We now add a conditional edge\n", + "workflow.add_conditional_edges(\n", + " # First, we define the start node. We use `agent`.\n", + " # This means these are the edges taken after the `agent` node is called.\n", + " \"agent\",\n", + " # Next, we pass in the function that will determine which node is called next.\n", + " should_continue,\n", + " # Finally we pass in a mapping.\n", + " # The keys are strings, and the values are other nodes.\n", + " # END is a special node marking that the graph should finish.\n", + " # What will happen is we will call `should_continue`, and then the output of that\n", + " # will be matched against the keys in this mapping.\n", + " # Based on which one it matches, that node will then be called.\n", + " {\n", + " # If `tools`, then we call the tool node.\n", + " \"continue\": \"action\",\n", + " # Otherwise we finish.\n", + " \"end\": END,\n", + " },\n", + ")\n", + "\n", + "# We now add a normal edge from `tools` to `agent`.\n", + "# This means that after `tools` is called, `agent` node is called next.\n", + "workflow.add_edge(\"action\", \"agent\")" + ] + }, + { + "cell_type": "markdown", + "id": "bc9c8536-f90b-44fa-958d-5df016c66d8f", + "metadata": {}, + "source": [ + "**Persistence**\n", + "\n", + "To add in persistence, we pass in a checkpoint when compiling the graph" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "6845ed6a-d155-4105-9160-28849877248b", + "metadata": {}, + "outputs": [], + "source": [ + "from langgraph.checkpoint.sqlite import SqliteSaver\n", + "\n", + "memory = SqliteSaver.from_conn_string(\":memory:\")" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "79d29875-8aa8-434c-9f20-1c58346a6249", + "metadata": {}, + "outputs": [], + "source": [ + "# Finally, we compile it!\n", + "# This compiles it into a LangChain Runnable,\n", + "# meaning you can use it as you would any other runnable\n", + "app = workflow.compile(checkpointer=memory, interrupt_before=[\"action\"])" + ] + }, + { + "cell_type": "markdown", + "id": "2a1b56c5-bd61-4192-8bdb-458a1e9f0159", + "metadata": {}, + "source": [ + "## Interacting with the Agent\n", + "\n", + "We can now interact with the agent. Between interactions you can get and update state.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "cfd140f0-a5a6-4697-8115-322242f197b5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "content='Hello Bob! How can I assist you today?' response_metadata={'finish_reason': 'stop', 'logprobs': None} id='9467300a-f8ea-47db-9ade-3d9130c9f6d1'\n" + ] + } + ], + "source": [ + "from langchain_core.messages import HumanMessage\n", + "\n", + "thread = {\"configurable\": {\"thread_id\": '3'}}\n", + "inputs = [HumanMessage(content=\"hi! I'm bob\")]\n", + "for event in app.stream(inputs, thread):\n", + " for k, v in event.items():\n", + " if k != \"__end__\":\n", + " print(v)" + ] + }, + { + "cell_type": "markdown", + "id": "c161d511-0dc3-4824-b89e-0dbcf9a7a759", + "metadata": {}, + "source": [ + "Let's now get the current state" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "23433643-d35d-4df4-80fe-a3002323cd4f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "StateSnapshot(values=[HumanMessage(content=\"hi! I'm bob\", id='5e326da4-44ee-4334-b2e6-869dd6d3218d'), AIMessage(content='Hello Bob! How can I assist you today?', response_metadata={'finish_reason': 'stop', 'logprobs': None}, id='9467300a-f8ea-47db-9ade-3d9130c9f6d1')], next=(), config={'configurable': {'thread_id': '3', 'thread_ts': '2024-03-15T21:24:10.780714+00:00'}})" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "app.get_state(thread)" + ] + }, + { + "cell_type": "markdown", + "id": "06aaa787-32fe-404b-abeb-5b2ed94309fd", + "metadata": {}, + "source": [ + "Let's now show how we can update the state. This is very useful when you want to have a human-in-the-loop to validate (and potentially change) the action to take. If you notice above, we added `interrupt_before=[\"action\"]` - this means that before any actions are taken we pause. This is a great moment to allow the user to correct and update the state!" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "08ae8246-11d5-40e1-8567-361e5bef8917", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "content='' additional_kwargs={'function_call': {'arguments': '{\"query\":\"current weather in San Francisco\"}', 'name': 'tavily_search_results_json'}} response_metadata={'finish_reason': 'function_call', 'logprobs': None} id='af48af47-01bc-4099-a7d6-14cc2df5709e'\n" + ] + } + ], + "source": [ + "inputs = [HumanMessage(content=\"what is the weather in sf currently\")]\n", + "for event in app.stream(inputs, thread):\n", + " for k, v in event.items():\n", + " if k != \"__end__\":\n", + " print(v)" + ] + }, + { + "cell_type": "markdown", + "id": "7d9fa4f0-fb7d-47a7-b77e-464e8b619273", + "metadata": {}, + "source": [ + "This is the function call the model produced" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "5a53df78-6c25-4176-b049-02a02a713771", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'function_call': {'arguments': '{\"query\":\"current weather in San Francisco\"}',\n", + " 'name': 'tavily_search_results_json'}}" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "current_values = app.get_state(thread)\n", + "current_values.values[-1].additional_kwargs" + ] + }, + { + "cell_type": "markdown", + "id": "b2b7af00-035c-44e6-a76a-a4b83340b32b", + "metadata": {}, + "source": [ + "Let's update the search string before proceeding" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "060e2e33-1f6a-40ef-850e-161b308986fb", + "metadata": {}, + "outputs": [], + "source": [ + "current_values.values[-1].additional_kwargs = {'function_call': {'arguments': '{\"query\":\"weather in San Francisco today\"}',\n", + " 'name': 'tavily_search_results_json'}}" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "3210d003-e20e-47fa-b4f4-402fed7409f1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'configurable': {'thread_id': '3',\n", + " 'thread_ts': '2024-03-15T21:24:37.208417+00:00'}}" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "app.update_state(thread, current_values.values)" + ] + }, + { + "cell_type": "markdown", + "id": "3613f445-cf46-46a0-b7c1-e83a02aae96f", + "metadata": {}, + "source": [ + "The current state now reflects our updated search query!" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "4a7dfaab-bfa7-47c7-8b93-2b06b3560809", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "StateSnapshot(values=[HumanMessage(content=\"hi! I'm bob\", id='5e326da4-44ee-4334-b2e6-869dd6d3218d'), AIMessage(content='Hello Bob! How can I assist you today?', response_metadata={'finish_reason': 'stop', 'logprobs': None}, id='9467300a-f8ea-47db-9ade-3d9130c9f6d1'), HumanMessage(content='what is the weather in sf currently', id='bc16849a-fa20-4f07-815c-b254c6d77f63'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"weather in San Francisco today\"}', 'name': 'tavily_search_results_json'}}, response_metadata={'finish_reason': 'function_call', 'logprobs': None}, id='af48af47-01bc-4099-a7d6-14cc2df5709e')], next=('agent:edges',), config={'configurable': {'thread_id': '3', 'thread_ts': '2024-03-15T21:24:37.208417+00:00'}})" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "app.get_state(thread)" + ] + }, + { + "cell_type": "markdown", + "id": "27304085-4e44-48bc-afa5-d7ab6da4bb8c", + "metadata": {}, + "source": [ + "If we start the agent again it will pick up from the state we updated." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "273d56a8-f40f-4a51-a27f-7c6bb2bda0ba", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "content='[{\\'url\\': \\'https://weather.com/weather/today/l/San+Francisco+CA+USCA0987:1:US\\', \\'content\\': \"Today\\'s and tonight\\'s San Francisco, CA weather forecast, weather conditions and Doppler radar from The Weather Channel and Weather.com\"}]' name='tavily_search_results_json' id='dbba7582-c1b3-4d6a-8727-9421c431f7dc'\n", + "content='You can check the current weather in San Francisco by visiting [The Weather Channel](https://weather.com/weather/today/l/San+Francisco+CA+USCA0987:1:US).' response_metadata={'finish_reason': 'stop', 'logprobs': None} id='4a32c94c-63bc-4f65-b9dd-bbaeaff3c54a'\n" + ] + } + ], + "source": [ + "for event in app.stream(None, thread):\n", + " for k, v in event.items():\n", + " if k != \"__end__\":\n", + " print(v)" + ] + }, + { + "cell_type": "markdown", + "id": "1c38c505-6cee-427f-9dcd-493a2ade7ebb", + "metadata": {}, + "source": [ + "## Checking history\n", + "\n", + "Let's browse the history of this thread, from newest to oldest." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "8578a66d-6489-4e03-8c23-fd0530278455", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "StateSnapshot(values=[HumanMessage(content=\"hi! I'm bob\", id='5e326da4-44ee-4334-b2e6-869dd6d3218d'), AIMessage(content='Hello Bob! How can I assist you today?', response_metadata={'finish_reason': 'stop', 'logprobs': None}, id='9467300a-f8ea-47db-9ade-3d9130c9f6d1'), HumanMessage(content='what is the weather in sf currently', id='bc16849a-fa20-4f07-815c-b254c6d77f63'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"weather in San Francisco today\"}', 'name': 'tavily_search_results_json'}}, response_metadata={'finish_reason': 'function_call', 'logprobs': None}, id='af48af47-01bc-4099-a7d6-14cc2df5709e'), FunctionMessage(content='[{\\'url\\': \\'https://weather.com/weather/today/l/San+Francisco+CA+USCA0987:1:US\\', \\'content\\': \"Today\\'s and tonight\\'s San Francisco, CA weather forecast, weather conditions and Doppler radar from The Weather Channel and Weather.com\"}]', name='tavily_search_results_json', id='dbba7582-c1b3-4d6a-8727-9421c431f7dc'), AIMessage(content='You can check the current weather in San Francisco by visiting [The Weather Channel](https://weather.com/weather/today/l/San+Francisco+CA+USCA0987:1:US).', response_metadata={'finish_reason': 'stop', 'logprobs': None}, id='4a32c94c-63bc-4f65-b9dd-bbaeaff3c54a')], next=(), config={'configurable': {'thread_id': '3', 'thread_ts': '2024-03-15T21:24:59.635105+00:00'}})\n", + "--\n", + "StateSnapshot(values=[HumanMessage(content=\"hi! I'm bob\", id='5e326da4-44ee-4334-b2e6-869dd6d3218d'), AIMessage(content='Hello Bob! How can I assist you today?', response_metadata={'finish_reason': 'stop', 'logprobs': None}, id='9467300a-f8ea-47db-9ade-3d9130c9f6d1'), HumanMessage(content='what is the weather in sf currently', id='bc16849a-fa20-4f07-815c-b254c6d77f63'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"weather in San Francisco today\"}', 'name': 'tavily_search_results_json'}}, response_metadata={'finish_reason': 'function_call', 'logprobs': None}, id='af48af47-01bc-4099-a7d6-14cc2df5709e'), FunctionMessage(content='[{\\'url\\': \\'https://weather.com/weather/today/l/San+Francisco+CA+USCA0987:1:US\\', \\'content\\': \"Today\\'s and tonight\\'s San Francisco, CA weather forecast, weather conditions and Doppler radar from The Weather Channel and Weather.com\"}]', name='tavily_search_results_json', id='dbba7582-c1b3-4d6a-8727-9421c431f7dc'), AIMessage(content='You can check the current weather in San Francisco by visiting [The Weather Channel](https://weather.com/weather/today/l/San+Francisco+CA+USCA0987:1:US).', response_metadata={'finish_reason': 'stop', 'logprobs': None}, id='4a32c94c-63bc-4f65-b9dd-bbaeaff3c54a')], next=('agent:edges',), config={'configurable': {'thread_id': '3', 'thread_ts': '2024-03-15T21:24:59.629900+00:00'}})\n", + "--\n", + "StateSnapshot(values=[HumanMessage(content=\"hi! I'm bob\", id='5e326da4-44ee-4334-b2e6-869dd6d3218d'), AIMessage(content='Hello Bob! How can I assist you today?', response_metadata={'finish_reason': 'stop', 'logprobs': None}, id='9467300a-f8ea-47db-9ade-3d9130c9f6d1'), HumanMessage(content='what is the weather in sf currently', id='bc16849a-fa20-4f07-815c-b254c6d77f63'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"weather in San Francisco today\"}', 'name': 'tavily_search_results_json'}}, response_metadata={'finish_reason': 'function_call', 'logprobs': None}, id='af48af47-01bc-4099-a7d6-14cc2df5709e'), FunctionMessage(content='[{\\'url\\': \\'https://weather.com/weather/today/l/San+Francisco+CA+USCA0987:1:US\\', \\'content\\': \"Today\\'s and tonight\\'s San Francisco, CA weather forecast, weather conditions and Doppler radar from The Weather Channel and Weather.com\"}]', name='tavily_search_results_json', id='dbba7582-c1b3-4d6a-8727-9421c431f7dc')], next=('agent',), config={'configurable': {'thread_id': '3', 'thread_ts': '2024-03-15T21:24:58.501730+00:00'}})\n", + "--\n", + "StateSnapshot(values=[HumanMessage(content=\"hi! I'm bob\", id='5e326da4-44ee-4334-b2e6-869dd6d3218d'), AIMessage(content='Hello Bob! How can I assist you today?', response_metadata={'finish_reason': 'stop', 'logprobs': None}, id='9467300a-f8ea-47db-9ade-3d9130c9f6d1'), HumanMessage(content='what is the weather in sf currently', id='bc16849a-fa20-4f07-815c-b254c6d77f63'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"weather in San Francisco today\"}', 'name': 'tavily_search_results_json'}}, response_metadata={'finish_reason': 'function_call', 'logprobs': None}, id='af48af47-01bc-4099-a7d6-14cc2df5709e'), FunctionMessage(content='[{\\'url\\': \\'https://weather.com/weather/today/l/San+Francisco+CA+USCA0987:1:US\\', \\'content\\': \"Today\\'s and tonight\\'s San Francisco, CA weather forecast, weather conditions and Doppler radar from The Weather Channel and Weather.com\"}]', name='tavily_search_results_json', id='dbba7582-c1b3-4d6a-8727-9421c431f7dc')], next=('action:edges',), config={'configurable': {'thread_id': '3', 'thread_ts': '2024-03-15T21:24:58.498596+00:00'}})\n", + "--\n", + "StateSnapshot(values=[HumanMessage(content=\"hi! I'm bob\", id='5e326da4-44ee-4334-b2e6-869dd6d3218d'), AIMessage(content='Hello Bob! How can I assist you today?', response_metadata={'finish_reason': 'stop', 'logprobs': None}, id='9467300a-f8ea-47db-9ade-3d9130c9f6d1'), HumanMessage(content='what is the weather in sf currently', id='bc16849a-fa20-4f07-815c-b254c6d77f63'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"weather in San Francisco today\"}', 'name': 'tavily_search_results_json'}}, response_metadata={'finish_reason': 'function_call', 'logprobs': None}, id='af48af47-01bc-4099-a7d6-14cc2df5709e')], next=('action',), config={'configurable': {'thread_id': '3', 'thread_ts': '2024-03-15T21:24:57.092121+00:00'}})\n", + "--\n", + "StateSnapshot(values=[HumanMessage(content=\"hi! I'm bob\", id='5e326da4-44ee-4334-b2e6-869dd6d3218d'), AIMessage(content='Hello Bob! How can I assist you today?', response_metadata={'finish_reason': 'stop', 'logprobs': None}, id='9467300a-f8ea-47db-9ade-3d9130c9f6d1'), HumanMessage(content='what is the weather in sf currently', id='bc16849a-fa20-4f07-815c-b254c6d77f63'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"weather in San Francisco today\"}', 'name': 'tavily_search_results_json'}}, response_metadata={'finish_reason': 'function_call', 'logprobs': None}, id='af48af47-01bc-4099-a7d6-14cc2df5709e')], next=('agent:edges',), config={'configurable': {'thread_id': '3', 'thread_ts': '2024-03-15T21:24:37.208417+00:00'}})\n", + "--\n", + "StateSnapshot(values=[HumanMessage(content=\"hi! I'm bob\", id='5e326da4-44ee-4334-b2e6-869dd6d3218d'), AIMessage(content='Hello Bob! How can I assist you today?', response_metadata={'finish_reason': 'stop', 'logprobs': None}, id='9467300a-f8ea-47db-9ade-3d9130c9f6d1'), HumanMessage(content='what is the weather in sf currently', id='bc16849a-fa20-4f07-815c-b254c6d77f63'), AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"query\":\"current weather in San Francisco\"}', 'name': 'tavily_search_results_json'}}, response_metadata={'finish_reason': 'function_call', 'logprobs': None}, id='af48af47-01bc-4099-a7d6-14cc2df5709e')], next=('agent:edges',), config={'configurable': {'thread_id': '3', 'thread_ts': '2024-03-15T21:24:20.413423+00:00'}})\n", + "--\n", + "StateSnapshot(values=[HumanMessage(content=\"hi! I'm bob\", id='5e326da4-44ee-4334-b2e6-869dd6d3218d'), AIMessage(content='Hello Bob! How can I assist you today?', response_metadata={'finish_reason': 'stop', 'logprobs': None}, id='9467300a-f8ea-47db-9ade-3d9130c9f6d1'), HumanMessage(content='what is the weather in sf currently', id='bc16849a-fa20-4f07-815c-b254c6d77f63')], next=('agent',), config={'configurable': {'thread_id': '3', 'thread_ts': '2024-03-15T21:24:19.721216+00:00'}})\n", + "--\n", + "StateSnapshot(values=[HumanMessage(content=\"hi! I'm bob\", id='5e326da4-44ee-4334-b2e6-869dd6d3218d'), AIMessage(content='Hello Bob! How can I assist you today?', response_metadata={'finish_reason': 'stop', 'logprobs': None}, id='9467300a-f8ea-47db-9ade-3d9130c9f6d1'), HumanMessage(content='what is the weather in sf currently', id='bc16849a-fa20-4f07-815c-b254c6d77f63')], next=('__start__:edges',), config={'configurable': {'thread_id': '3', 'thread_ts': '2024-03-15T21:24:19.717873+00:00'}})\n", + "--\n", + "StateSnapshot(values=[HumanMessage(content=\"hi! I'm bob\", id='5e326da4-44ee-4334-b2e6-869dd6d3218d'), AIMessage(content='Hello Bob! How can I assist you today?', response_metadata={'finish_reason': 'stop', 'logprobs': None}, id='9467300a-f8ea-47db-9ade-3d9130c9f6d1')], next=(), config={'configurable': {'thread_id': '3', 'thread_ts': '2024-03-15T21:24:10.780714+00:00'}})\n", + "--\n", + "StateSnapshot(values=[HumanMessage(content=\"hi! I'm bob\", id='5e326da4-44ee-4334-b2e6-869dd6d3218d'), AIMessage(content='Hello Bob! How can I assist you today?', response_metadata={'finish_reason': 'stop', 'logprobs': None}, id='9467300a-f8ea-47db-9ade-3d9130c9f6d1')], next=('agent:edges',), config={'configurable': {'thread_id': '3', 'thread_ts': '2024-03-15T21:24:10.773938+00:00'}})\n", + "--\n", + "StateSnapshot(values=[HumanMessage(content=\"hi! I'm bob\", id='5e326da4-44ee-4334-b2e6-869dd6d3218d')], next=('agent',), config={'configurable': {'thread_id': '3', 'thread_ts': '2024-03-15T21:24:10.163843+00:00'}})\n", + "--\n", + "StateSnapshot(values=[HumanMessage(content=\"hi! I'm bob\", id='5e326da4-44ee-4334-b2e6-869dd6d3218d')], next=('__start__:edges',), config={'configurable': {'thread_id': '3', 'thread_ts': '2024-03-15T21:24:10.162581+00:00'}})\n", + "--\n" + ] + } + ], + "source": [ + "for state in app.get_state_history(thread):\n", + " print(state)\n", + " print('--')\n", + " if len(state.values) == 3:\n", + " to_replay = state" + ] + }, + { + "cell_type": "markdown", + "id": "0ec41c37-7c09-4cc7-8475-bf373fe66584", + "metadata": {}, + "source": [ + "We can go back to any of these states and restart the agent from there!" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "21e7fc18-6fd9-4e11-a84b-e0325c9640c8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "StateSnapshot(values=[HumanMessage(content=\"hi! I'm bob\", id='5e326da4-44ee-4334-b2e6-869dd6d3218d'), AIMessage(content='Hello Bob! How can I assist you today?', response_metadata={'finish_reason': 'stop', 'logprobs': None}, id='9467300a-f8ea-47db-9ade-3d9130c9f6d1'), HumanMessage(content='what is the weather in sf currently', id='bc16849a-fa20-4f07-815c-b254c6d77f63')], next=('__start__:edges',), config={'configurable': {'thread_id': '3', 'thread_ts': '2024-03-15T21:24:19.717873+00:00'}})" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "to_replay" + ] + }, + { + "cell_type": "markdown", + "id": "29da43ea-9295-43e2-b164-0eb28d96749c", + "metadata": {}, + "source": [ + "To replay from this place we just need to pass its config back to the agent." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "e986f94f-706f-4b6f-b3c4-f95483b9e9b8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "content='' additional_kwargs={'function_call': {'arguments': '{\"query\":\"current weather in San Francisco\"}', 'name': 'tavily_search_results_json'}} response_metadata={'finish_reason': 'function_call', 'logprobs': None} id='3a853de3-bdd9-4186-9f10-56bd151f63da'\n" + ] + } + ], + "source": [ + "for event in app.stream(None, to_replay.config):\n", + " for k, v in event.items():\n", + " if k != \"__end__\":\n", + " print(v)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b084f141-5800-487b-b115-d2e58421b963", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.1" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}