{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53",
   "metadata": {},
   "source": [
    "# How to manage agent steps\n",
    "\n",
    "In this example we will build a ReAct Agent that explicitly manages intermediate steps.\n",
    "\n",
    "The previous examples just put all messages into the model, but that extra context can distract the agent and add latency to the API calls. In this example we will only include the `N` most recent messages in the chat history. Note that this is meant to be illustrative of general state management."
   ]
  },
  {
   "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": [],
   "source": ["%%capture --no-stderr\n%pip install --quiet -U langgraph langchain_openai"]
  },
  {
   "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": 1,
   "id": "c903a1cf-2977-4e2d-ad7d-8b3946821d89",
   "metadata": {},
   "outputs": [],
   "source": ["import getpass\nimport os\n\n\ndef _set_env(var: str):\n    if not os.environ.get(var):\n        os.environ[var] = getpass.getpass(f\"{var}: \")\n\n\n_set_env(\"OPENAI_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": 2,
   "id": "95e25aec-7c9f-4a63-b143-225d0e9a79c3",
   "metadata": {},
   "outputs": [],
   "source": ["os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n_set_env(\"LANGCHAIN_API_KEY\")"]
  },
  {
   "cell_type": "markdown",
   "id": "5683c276",
   "metadata": {},
   "source": [
    "## Set up the State\n",
    "\n",
    "The main type of graph in `langgraph` is the [StateGraph](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.StateGraph).\n",
    "This graph is parameterized by a `State` object that it passes around to each node.\n",
    "Each node then returns operations the graph uses to `update` that state.\n",
    "These operations can either SET specific attributes on the state (e.g. overwrite the existing values) or ADD to the existing attribute.\n",
    "Whether to set or add is denoted by annotating the `State` object you use to construct the graph.\n",
    "\n",
    "For this example, the state we will track will just be a list of messages.\n",
    "We want each node to just add messages to that list.\n",
    "Therefore, we will use a `TypedDict` with one key (`messages`) and annotate it so that the `messages` attribute is \"append-only\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5f374964",
   "metadata": {},
   "outputs": [],
   "source": ["from typing import Annotated\n\nfrom typing_extensions import TypedDict\n\nfrom langgraph.graph.message import add_messages\n\n# Add messages essentially does this with more\n# robust handling\n# def add_messages(left: list, right: list):\n#     return left + right\n\n\nclass State(TypedDict):\n    messages: Annotated[list, add_messages]"]
  },
  {
   "cell_type": "markdown",
   "id": "6fa717fc",
   "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 create a placeholder search engine.\n",
    "It is really easy to create your own tools - see documentation [here](https://python.langchain.com/v0.2/docs/how_to/custom_tools) on how to do that.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "692cffb0",
   "metadata": {},
   "outputs": [],
   "source": ["from langchain_core.tools import tool\n\n\n@tool\ndef search(query: str):\n    \"\"\"Call to surf the web.\"\"\"\n    # This is a placeholder, but don't tell the LLM that...\n    return [\n        \"Try again in a few seconds! Checking with the weathermen... Call be again next.\"\n    ]\n\n\ntools = [search]"]
  },
  {
   "cell_type": "markdown",
   "id": "b22c13d9",
   "metadata": {},
   "source": [
    "We can now wrap these tools in a simple [ToolNode](https://langchain-ai.github.io/langgraph/reference/prebuilt/#toolnode).\n",
    "This is  a simple class that takes in a list of messages containing an [AIMessages with tool_calls](https://api.python.langchain.com/en/latest/messages/langchain_core.messages.ai.AIMessage.html#langchain_core.messages.ai.AIMessage.tool_calls), runs the tools, and returns the output as [ToolMessage](https://api.python.langchain.com/en/latest/messages/langchain_core.messages.tool.ToolMessage.html#langchain_core.messages.tool.ToolMessage)s.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ae7abc20",
   "metadata": {},
   "outputs": [],
   "source": ["from langgraph.prebuilt import ToolNode\n\ntool_node = ToolNode(tools)"]
  },
  {
   "cell_type": "markdown",
   "id": "9affc5db",
   "metadata": {},
   "source": [
    "## Set up the model\n",
    "\n",
    "Now we need to load the chat model we want to use.\n",
    "This should satisfy two criteria:\n",
    "\n",
    "1. It should work with messages, since our state is primarily a list of messages (chat history).\n",
    "2. It should work with tool calling, since we are using a prebuilt [ToolNode](https://langchain-ai.github.io/langgraph/reference/prebuilt/#toolnode)\n",
    "\n",
    "**Note:** these model requirements are not requirements for using LangGraph - they are just requirements for this particular example.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4ad247ff",
   "metadata": {},
   "outputs": [],
   "source": ["from langchain_openai import ChatOpenAI\n\nmodel = ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=0)"]
  },
  {
   "cell_type": "markdown",
   "id": "ebe87eac",
   "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 function calling, and then bind them to the model class.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "cd3cbae5-d92c-4559-a4aa-44721b80d107",
   "metadata": {},
   "outputs": [],
   "source": ["model = model.bind_tools(tools)"]
  },
  {
   "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/v0.2/docs/concepts/#langchain-expression-language-lcel).\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.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "e718a9c5-6596-457f-ac25-a25d8cb8c259",
   "metadata": {},
   "outputs": [],
   "source": ["from typing import Literal\n\n\n# Define the function that determines whether to continue or not\ndef should_continue(state: State) -> Literal[\"__end__\", \"action\"]:\n    messages = state[\"messages\"]\n    last_message = messages[-1]\n    # If there is no function call, then we finish\n    if not last_message.tool_calls:\n        return \"end\"\n    # Otherwise if there is, we continue\n    else:\n        return \"continue\""]
  },
  {
   "cell_type": "markdown",
   "id": "a763aa63-701c-40fa-a9d3-9d992ebe7e4d",
   "metadata": {},
   "source": [
    "**MODIFICATION**\n",
    "\n",
    "Here we don't pass all messages to the model but rather only pass the `N` most recent. Note that this is a terribly simplistic way to handle messages meant as an illustrtion, and there may be other methods you may want to look into depending on your use case. We also have to make sure we don't truncate the chat history to include the tool message first, as this would cause an API error."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "714e4135-7cb5-4f17-b2ae-46f7e98bde61",
   "metadata": {},
   "outputs": [],
   "source": ["# Define the function that calls the model\ndef call_model(state):\n    messages = []\n    for m in state[\"messages\"][::-1]:\n        messages.append(m)\n        if len(messages) >= 5:\n            if messages[-1].type != \"tool\":\n                break\n    response = model.invoke(messages[::-1])\n    # We return a list, because this will get added to the existing list\n    return {\"messages\": [response]}"]
  },
  {
   "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": 34,
   "id": "813ae66c-3b58-4283-a02a-36da72a2ab90",
   "metadata": {},
   "outputs": [],
   "source": ["from langgraph.graph import END, StateGraph, START\n\n# Define a new graph\nworkflow = StateGraph(State)\n\n# Define the two nodes we will cycle between\nworkflow.add_node(\"agent\", call_model)\nworkflow.add_node(\"action\", tool_node)\n\n# Set the entrypoint as `agent`\n# This means that this node is the first one called\nworkflow.add_edge(START, \"agent\")\n\n# We now add a conditional edge\nworkflow.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.\nworkflow.add_edge(\"action\", \"agent\")\n\n# Finally, we compile it!\n# This compiles it into a LangChain Runnable,\n# meaning you can use it as you would any other runnable\napp = workflow.compile()"]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "1f6af5f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/jpeg": "[encoded data, 12716 characters]",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": ["from IPython.display import Image, display\n\ndisplay(Image(app.get_graph(xray=True).draw_mermaid_png()))"]
  },
  {
   "cell_type": "markdown",
   "id": "547c3931-3dae-4281-ad4e-4b51305594d4",
   "metadata": {},
   "source": [
    "## Use it!\n",
    "\n",
    "We can now use it!\n",
    "This now exposes the [same interface](https://python.langchain.com/v0.2/docs/concepts/#langchain-expression-language-lcel) as all other LangChain runnables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "f544977e-31f7-41f0-88c4-ec9c27b8cecb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "\n",
      "what is the weather in sf? Don't give up! Keep using your tools.\n",
      "\n",
      "---\n",
      "\n",
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "\n",
      "what is the weather in sf? Don't give up! Keep using your tools.\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "Tool Calls:\n",
      "  search (call_IFPJzhP9xj2vHF6nKku2bPkJ)\n",
      " Call ID: call_IFPJzhP9xj2vHF6nKku2bPkJ\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "\n",
      "---\n",
      "\n",
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "\n",
      "what is the weather in sf? Don't give up! Keep using your tools.\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "Tool Calls:\n",
      "  search (call_IFPJzhP9xj2vHF6nKku2bPkJ)\n",
      " Call ID: call_IFPJzhP9xj2vHF6nKku2bPkJ\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
      "Name: search\n",
      "\n",
      "[\"Try again in a few seconds! Checking with the weathermen... Call be again next.\"]\n",
      "\n",
      "---\n",
      "\n",
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "\n",
      "what is the weather in sf? Don't give up! Keep using your tools.\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "Tool Calls:\n",
      "  search (call_IFPJzhP9xj2vHF6nKku2bPkJ)\n",
      " Call ID: call_IFPJzhP9xj2vHF6nKku2bPkJ\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
      "Name: search\n",
      "\n",
      "[\"Try again in a few seconds! Checking with the weathermen... Call be again next.\"]\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "It seems like there was a delay in retrieving the weather information for San Francisco. Let me try again.\n",
      "Tool Calls:\n",
      "  search (call_Qva3ZfINeDVzKd9neRLB3NwF)\n",
      " Call ID: call_Qva3ZfINeDVzKd9neRLB3NwF\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "\n",
      "---\n",
      "\n",
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "\n",
      "what is the weather in sf? Don't give up! Keep using your tools.\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "Tool Calls:\n",
      "  search (call_IFPJzhP9xj2vHF6nKku2bPkJ)\n",
      " Call ID: call_IFPJzhP9xj2vHF6nKku2bPkJ\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
      "Name: search\n",
      "\n",
      "[\"Try again in a few seconds! Checking with the weathermen... Call be again next.\"]\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "It seems like there was a delay in retrieving the weather information for San Francisco. Let me try again.\n",
      "Tool Calls:\n",
      "  search (call_Qva3ZfINeDVzKd9neRLB3NwF)\n",
      " Call ID: call_Qva3ZfINeDVzKd9neRLB3NwF\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
      "Name: search\n",
      "\n",
      "[\"Try again in a few seconds! Checking with the weathermen... Call be again next.\"]\n",
      "\n",
      "---\n",
      "\n",
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "\n",
      "what is the weather in sf? Don't give up! Keep using your tools.\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "Tool Calls:\n",
      "  search (call_IFPJzhP9xj2vHF6nKku2bPkJ)\n",
      " Call ID: call_IFPJzhP9xj2vHF6nKku2bPkJ\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
      "Name: search\n",
      "\n",
      "[\"Try again in a few seconds! Checking with the weathermen... Call be again next.\"]\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "It seems like there was a delay in retrieving the weather information for San Francisco. Let me try again.\n",
      "Tool Calls:\n",
      "  search (call_Qva3ZfINeDVzKd9neRLB3NwF)\n",
      " Call ID: call_Qva3ZfINeDVzKd9neRLB3NwF\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
      "Name: search\n",
      "\n",
      "[\"Try again in a few seconds! Checking with the weathermen... Call be again next.\"]\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "It appears that there is still a delay in retrieving the weather information for San Francisco. Let me try using a different approach to get the weather update.\n",
      "Tool Calls:\n",
      "  search (call_cMGhCmBYGM6NcYvrddgMaY4W)\n",
      " Call ID: call_cMGhCmBYGM6NcYvrddgMaY4W\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "  search (call_DXj0kic4WZfwA61edqGZLWxh)\n",
      " Call ID: call_DXj0kic4WZfwA61edqGZLWxh\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "\n",
      "---\n",
      "\n",
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "\n",
      "what is the weather in sf? Don't give up! Keep using your tools.\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "Tool Calls:\n",
      "  search (call_IFPJzhP9xj2vHF6nKku2bPkJ)\n",
      " Call ID: call_IFPJzhP9xj2vHF6nKku2bPkJ\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
      "Name: search\n",
      "\n",
      "[\"Try again in a few seconds! Checking with the weathermen... Call be again next.\"]\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "It seems like there was a delay in retrieving the weather information for San Francisco. Let me try again.\n",
      "Tool Calls:\n",
      "  search (call_Qva3ZfINeDVzKd9neRLB3NwF)\n",
      " Call ID: call_Qva3ZfINeDVzKd9neRLB3NwF\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
      "Name: search\n",
      "\n",
      "[\"Try again in a few seconds! Checking with the weathermen... Call be again next.\"]\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "It appears that there is still a delay in retrieving the weather information for San Francisco. Let me try using a different approach to get the weather update.\n",
      "Tool Calls:\n",
      "  search (call_cMGhCmBYGM6NcYvrddgMaY4W)\n",
      " Call ID: call_cMGhCmBYGM6NcYvrddgMaY4W\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "  search (call_DXj0kic4WZfwA61edqGZLWxh)\n",
      " Call ID: call_DXj0kic4WZfwA61edqGZLWxh\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
      "Name: search\n",
      "\n",
      "[\"Try again in a few seconds! Checking with the weathermen... Call be again next.\"]\n",
      "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
      "Name: search\n",
      "\n",
      "[\"Try again in a few seconds! Checking with the weathermen... Call be again next.\"]\n",
      "\n",
      "---\n",
      "\n",
      "================================\u001b[1m Human Message \u001b[0m=================================\n",
      "\n",
      "what is the weather in sf? Don't give up! Keep using your tools.\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "Tool Calls:\n",
      "  search (call_IFPJzhP9xj2vHF6nKku2bPkJ)\n",
      " Call ID: call_IFPJzhP9xj2vHF6nKku2bPkJ\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
      "Name: search\n",
      "\n",
      "[\"Try again in a few seconds! Checking with the weathermen... Call be again next.\"]\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "It seems like there was a delay in retrieving the weather information for San Francisco. Let me try again.\n",
      "Tool Calls:\n",
      "  search (call_Qva3ZfINeDVzKd9neRLB3NwF)\n",
      " Call ID: call_Qva3ZfINeDVzKd9neRLB3NwF\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
      "Name: search\n",
      "\n",
      "[\"Try again in a few seconds! Checking with the weathermen... Call be again next.\"]\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "It appears that there is still a delay in retrieving the weather information for San Francisco. Let me try using a different approach to get the weather update.\n",
      "Tool Calls:\n",
      "  search (call_cMGhCmBYGM6NcYvrddgMaY4W)\n",
      " Call ID: call_cMGhCmBYGM6NcYvrddgMaY4W\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "  search (call_DXj0kic4WZfwA61edqGZLWxh)\n",
      " Call ID: call_DXj0kic4WZfwA61edqGZLWxh\n",
      "  Args:\n",
      "    query: weather in San Francisco\n",
      "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
      "Name: search\n",
      "\n",
      "[\"Try again in a few seconds! Checking with the weathermen... Call be again next.\"]\n",
      "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
      "Name: search\n",
      "\n",
      "[\"Try again in a few seconds! Checking with the weathermen... Call be again next.\"]\n",
      "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
      "\n",
      "It seems that there is still a delay in retrieving the weather information for San Francisco. Let's wait a bit longer for the update. Thank you for your patience.\n",
      "\n",
      "---\n",
      "\n"
     ]
    }
   ],
   "source": ["from langchain_core.messages import HumanMessage\n\ninputs = {\n    \"messages\": [\n        HumanMessage(\n            content=\"what is the weather in sf? Don't give up! Keep using your tools.\"\n        )\n    ]\n}\nfor event in app.stream(inputs, stream_mode=\"values\"):\n    # stream() yields dictionaries with output keyed by node name\n    for message in event[\"messages\"]:\n        message.pretty_print()\n    print(\"\\n---\\n\")"]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08ae8246-11d5-40e1-8567-361e5bef8917",
   "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.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
