{
  "cells": [
    {
      "attachments": {
        "02ae42da-d1a4-4849-984a-6ab0bbf759bd.png": {
          "image/png": "[encoded data, 152892 characters]"
        }
      },
      "cell_type": "markdown",
      "id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53",
      "metadata": {},
      "source": [
        "# How to wait for user input\n",
        "\n",
        "Human-in-the-loop (HIL) interactions are crucial for [agentic systems](https://langchain-ai.github.io/langgraph/concepts/agentic_concepts/#human-in-the-loop). Waiting for human input is a common HIL interaction pattern, allowing the agent to ask the user clarifying questions and await input before proceeding. \n",
        "\n",
        "We can implement this in LangGraph using a [breakpoint](https://langchain-ai.github.io/langgraph/how-tos/human_in_the_loop/breakpoints/): breakpoints allow us to stop graph execution at a specific step. At this breakpoint, we can wait for human input. Once we have input from the user, we can add it to the graph state and proceed.\n",
        "\n",
        "![Screenshot 2024-07-08 at 5.26.26 PM.png](attachment:02ae42da-d1a4-4849-984a-6ab0bbf759bd.png)"
      ]
    },
    {
      "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_anthropic langchain_openai"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "0abe11f4-62ed-4dc4-8875-3db21e260d1d",
      "metadata": {},
      "source": [
        "Next, we need to set API keys for Anthropic and / or OpenAI (the LLM(s) we will use)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "c903a1cf-2977-4e2d-ad7d-8b3946821d89",
      "metadata": {},
      "outputs": [
        {
          "name": "stdin",
          "output_type": "stream",
          "text": [
            "ANTHROPIC_API_KEY:  ········\n"
          ]
        }
      ],
      "source": [
        "import getpass\n",
        "import os\n",
        "\n",
        "\n",
        "def _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\")\n",
        "_set_env(\"ANTHROPIC_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": 3,
      "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": "e6cf1fad-5ab6-49c5-b0c8-15a1b6e8cf21",
      "metadata": {},
      "source": [
        "## Simple Usage\n",
        "\n",
        "Let's look at very basic usage of this. One intuitive approach is simply to create a node, `human_feedback`, that will get user feedback. This allows us to place our feedback gathering at a specific, chosen point in our graph.\n",
        " \n",
        "1) We specify the [breakpoint](https://langchain-ai.github.io/langgraph/concepts/low_level/#breakpoints) using `interrupt_before` our `human_feedback` node.\n",
        "\n",
        "2) We set up a [checkpointer](https://langchain-ai.github.io/langgraph/concepts/low_level/#checkpointer) to save the state of the graph up until this node.\n",
        "\n",
        "3) We use `.update_state` to update the state of the graph with the human response we get.\n",
        "\n",
        "* We [use the `as_node` parameter](https://langchain-ai.github.io/langgraph/concepts/low_level/#update-state) to apply this state update as the specified node, `human_feedback`.\n",
        "* The graph will then resume execution as if the `human_feedback` node just acted."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 61,
      "id": "58eae42d-be32-48da-8d0a-ab64471657d9",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/jpeg": "[encoded data, 13520 characters]",
            "text/plain": [
              "<IPython.core.display.Image object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "from typing import TypedDict\n",
        "from langgraph.graph import StateGraph, START, END\n",
        "from langgraph.checkpoint.memory import MemorySaver\n",
        "from IPython.display import Image, display\n",
        "\n",
        "\n",
        "class State(TypedDict):\n",
        "    input: str\n",
        "    user_feedback: str\n",
        "\n",
        "\n",
        "def step_1(state):\n",
        "    print(\"---Step 1---\")\n",
        "    pass\n",
        "\n",
        "\n",
        "def human_feedback(state):\n",
        "    print(\"---human_feedback---\")\n",
        "    pass\n",
        "\n",
        "\n",
        "def step_3(state):\n",
        "    print(\"---Step 3---\")\n",
        "    pass\n",
        "\n",
        "\n",
        "builder = StateGraph(State)\n",
        "builder.add_node(\"step_1\", step_1)\n",
        "builder.add_node(\"human_feedback\", human_feedback)\n",
        "builder.add_node(\"step_3\", step_3)\n",
        "builder.add_edge(START, \"step_1\")\n",
        "builder.add_edge(\"step_1\", \"human_feedback\")\n",
        "builder.add_edge(\"human_feedback\", \"step_3\")\n",
        "builder.add_edge(\"step_3\", END)\n",
        "\n",
        "# Set up memory\n",
        "memory = MemorySaver()\n",
        "\n",
        "# Add\n",
        "graph = builder.compile(checkpointer=memory, interrupt_before=[\"human_feedback\"])\n",
        "\n",
        "# View\n",
        "display(Image(graph.get_graph().draw_mermaid_png()))"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ce0fe2bc-86fc-465f-956c-729805d50404",
      "metadata": {},
      "source": [
        "Run until our breakpoint at `step_2` - "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 62,
      "id": "eb8e7d47-e7c9-4217-b72c-08394a2c4d3e",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{'input': 'hello world'}\n",
            "---Step 1---\n"
          ]
        }
      ],
      "source": [
        "# Input\n",
        "initial_input = {\"input\": \"hello world\"}\n",
        "\n",
        "# Thread\n",
        "thread = {\"configurable\": {\"thread_id\": \"1\"}}\n",
        "\n",
        "# Run the graph until the first interruption\n",
        "for event in graph.stream(initial_input, thread, stream_mode=\"values\"):\n",
        "    print(event)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "28a7d545-ab19-4800-985b-62837d060809",
      "metadata": {},
      "source": [
        "Now, we can just manually update our graph state with with the user input - "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 63,
      "id": "2165a1bc-1c5b-411f-9e9c-a2b9627e5d56",
      "metadata": {},
      "outputs": [
        {
          "name": "stdin",
          "output_type": "stream",
          "text": [
            "Tell me how you want to update the state:  go to step 3!\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "--State after update--\n",
            "StateSnapshot(values={'input': 'hello world', 'user_feedback': 'go to step 3!'}, next=('step_3',), config={'configurable': {'thread_id': '1', 'thread_ts': '1ef3e216-b8a2-6db4-8002-966ecca671d0'}}, metadata={'source': 'update', 'step': 2, 'writes': {'human_feedback': {'user_feedback': 'go to step 3!'}}}, created_at='2024-07-09T18:31:13.083519+00:00', parent_config=None)\n"
          ]
        },
        {
          "data": {
            "text/plain": [
              "('step_3',)"
            ]
          },
          "execution_count": 63,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Get user input\n",
        "user_input = input(\"Tell me how you want to update the state: \")\n",
        "\n",
        "# We now update the state as if we are the human_feedback node\n",
        "graph.update_state(thread, {\"user_feedback\": user_input}, as_node=\"human_feedback\")\n",
        "\n",
        "# We can check the state\n",
        "print(\"--State after update--\")\n",
        "print(graph.get_state(thread))\n",
        "\n",
        "# We can check the next node, showing that it is node 3 (which follows human_feedback)\n",
        "graph.get_state(thread).next"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ccc4a84a-02f2-4b79-a5a5-22173645526d",
      "metadata": {},
      "source": [
        "We can proceed after our breakpoint - "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 64,
      "id": "3cca588f-e8d8-416b-aba7-0f3ae5e51598",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "---Step 3---\n"
          ]
        }
      ],
      "source": [
        "# Continue the graph execution\n",
        "for event in graph.stream(None, thread, stream_mode=\"values\"):\n",
        "    print(event)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a75a1060-47aa-4cc6-8c41-e6ba2e9d7923",
      "metadata": {},
      "source": [
        "We can see our feedback was added to state - "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 66,
      "id": "2b83e5ca-8497-43ca-bff7-7203e654c4d3",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'input': 'hello world', 'user_feedback': 'go to step 3!'}"
            ]
          },
          "execution_count": 66,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "graph.get_state(thread).values"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e36f89e5",
      "metadata": {},
      "source": [
        "## Agent\n",
        "\n",
        "In the context of agents, waiting for user feedback is useful to ask clarifying questions.\n",
        " \n",
        "To show this, we will build a relatively simple ReAct-style agent that does tool calling. \n",
        "\n",
        "We will use OpenAI and / or Anthropic's models and a fake tool (just for demo purposes)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 47,
      "id": "f5319e01",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "image/jpeg": "[encoded data, 18964 characters]",
            "text/plain": [
              "<IPython.core.display.Image object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "# Set up the state\n",
        "from langgraph.graph import MessagesState, START\n",
        "\n",
        "# Set up the tool\n",
        "# We will have one real tool - a search tool\n",
        "# We'll also have one \"fake\" tool - a \"ask_human\" tool\n",
        "# Here we define any ACTUAL tools\n",
        "from langchain_core.tools import tool\n",
        "from langgraph.prebuilt import ToolNode\n",
        "\n",
        "\n",
        "@tool\n",
        "def search(query: str):\n",
        "    \"\"\"Call to surf the web.\"\"\"\n",
        "    # This is a placeholder for the actual implementation\n",
        "    # Don't let the LLM know this though 😊\n",
        "    return [\n",
        "        f\"I looked up: {query}. Result: It's sunny in San Francisco, but you better look out if you're a Gemini 😈.\"\n",
        "    ]\n",
        "\n",
        "\n",
        "tools = [search]\n",
        "tool_node = ToolNode(tools)\n",
        "\n",
        "# Set up the model\n",
        "from langchain_anthropic import ChatAnthropic\n",
        "from langchain_openai import ChatOpenAI\n",
        "\n",
        "model = ChatAnthropic(model=\"claude-3-5-sonnet-20240620\")\n",
        "model = ChatOpenAI(model=\"gpt-4o\")\n",
        "\n",
        "# We are going \"bind\" all tools to the model\n",
        "# We have the ACTUAL tools from above, but we also need a mock tool to ask a human\n",
        "# Since `bind_tools` takes in tools but also just tool definitions,\n",
        "# We can define a tool definition for `ask_human`\n",
        "\n",
        "from langchain_core.pydantic_v1 import BaseModel\n",
        "\n",
        "\n",
        "class AskHuman(BaseModel):\n",
        "    \"\"\"Ask the human a question\"\"\"\n",
        "\n",
        "    question: str\n",
        "\n",
        "\n",
        "model = model.bind_tools(tools + [AskHuman])\n",
        "\n",
        "# Define nodes and conditional edges\n",
        "\n",
        "from langchain_core.messages import ToolMessage\n",
        "\n",
        "from langgraph.prebuilt import ToolInvocation\n",
        "\n",
        "\n",
        "# Define the function that determines whether to continue or not\n",
        "def should_continue(state):\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",
        "    # If tool call is asking Human, we return that node\n",
        "    # You could also add logic here to let some system know that there's something that requires Human input\n",
        "    # For example, send a slack message, etc\n",
        "    elif last_message.tool_calls[0][\"name\"] == \"AskHuman\":\n",
        "        return \"ask_human\"\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(state):\n",
        "    messages = state[\"messages\"]\n",
        "    response = model.invoke(messages)\n",
        "    # We return a list, because this will get added to the existing list\n",
        "    return {\"messages\": [response]}\n",
        "\n",
        "\n",
        "# We define a fake node to ask the human\n",
        "def ask_human(state):\n",
        "    pass\n",
        "\n",
        "\n",
        "# Build the graph\n",
        "\n",
        "from langgraph.graph import END, StateGraph\n",
        "\n",
        "# Define a new graph\n",
        "workflow = StateGraph(MessagesState)\n",
        "\n",
        "# Define the three nodes we will cycle between\n",
        "workflow.add_node(\"agent\", call_model)\n",
        "workflow.add_node(\"action\", tool_node)\n",
        "workflow.add_node(\"ask_human\", ask_human)\n",
        "\n",
        "# Set the entrypoint as `agent`\n",
        "# This means that this node is the first one called\n",
        "workflow.add_edge(START, \"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",
        "        # We may ask the human\n",
        "        \"ask_human\": \"ask_human\",\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\")\n",
        "\n",
        "# After we get back the human response, we go back to the agent\n",
        "workflow.add_edge(\"ask_human\", \"agent\")\n",
        "\n",
        "# Set up memory\n",
        "from langgraph.checkpoint.memory import MemorySaver\n",
        "\n",
        "memory = MemorySaver()\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\n",
        "# We add a breakpoint BEFORE the `ask_human` node so it never executes\n",
        "app = workflow.compile(checkpointer=memory, interrupt_before=[\"ask_human\"])\n",
        "\n",
        "display(Image(app.get_graph().draw_mermaid_png()))"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "2a1b56c5-bd61-4192-8bdb-458a1e9f0159",
      "metadata": {},
      "source": [
        "## Interacting with the Agent\n",
        "\n",
        "We can now interact with the agent. Let's ask it to ask the user where they are, then tell them the weather. \n",
        "\n",
        "This should make it use the `ask_human` tool first, then use the normal tool."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 48,
      "id": "cfd140f0-a5a6-4697-8115-322242f197b5",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "================================\u001b[1m Human Message \u001b[0m=================================\n",
            "\n",
            "Use the search tool to ask the user where they are, then look up the weather there\n",
            "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
            "Tool Calls:\n",
            "  AskHuman (call_LDo62KBPQKZWxPI5IHxPBF0w)\n",
            " Call ID: call_LDo62KBPQKZWxPI5IHxPBF0w\n",
            "  Args:\n",
            "    question: Can you tell me where you are located?\n"
          ]
        }
      ],
      "source": [
        "from langchain_core.messages import HumanMessage\n",
        "\n",
        "config = {\"configurable\": {\"thread_id\": \"2\"}}\n",
        "input_message = HumanMessage(\n",
        "    content=\"Use the search tool to ask the user where they are, then look up the weather there\"\n",
        ")\n",
        "for event in app.stream({\"messages\": [input_message]}, config, stream_mode=\"values\"):\n",
        "    event[\"messages\"][-1].pretty_print()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cc168c90-a374-4280-a9a6-8bc232dbb006",
      "metadata": {},
      "source": [
        "We now want to update this thread with a response from the user. We then can kick off another run. \n",
        "\n",
        "Because we are treating this as a tool call, we will need to update the state as if it is a response from a tool call. In order to do this, we will need to check the state to get the ID of the tool call."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 50,
      "id": "63598092-d565-4170-9773-e092d345f8c1",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "('agent',)"
            ]
          },
          "execution_count": 50,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "tool_call_id = app.get_state(config).values[\"messages\"][-1].tool_calls[0][\"id\"]\n",
        "\n",
        "# We now create the tool call with the id and the response we want\n",
        "tool_message = [\n",
        "    {\"tool_call_id\": tool_call_id, \"type\": \"tool\", \"content\": \"san francisco\"}\n",
        "]\n",
        "\n",
        "# # This is equivalent to the below, either one works\n",
        "# from langchain_core.messages import ToolMessage\n",
        "# tool_message = [ToolMessage(tool_call_id=tool_call_id, content=\"san francisco\")]\n",
        "\n",
        "# We now update the state\n",
        "# Notice that we are also specifying `as_node=\"ask_human\"`\n",
        "# This will apply this update as this node,\n",
        "# which will make it so that afterwards it continues as normal\n",
        "app.update_state(config, {\"messages\": tool_message}, as_node=\"ask_human\")\n",
        "\n",
        "# We can check the state\n",
        "# We can see that the state currently has the `agent` node next\n",
        "# This is based on how we define our graph,\n",
        "# where after the `ask_human` node goes (which we just triggered)\n",
        "# there is an edge to the `agent` node\n",
        "app.get_state(config).next"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "6a30c9fb-2a40-45cc-87ba-406c11c9f0cf",
      "metadata": {},
      "source": [
        "We can now tell the agent to continue. We can just pass in `None` as the input to the graph, since no additional input is needed"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 51,
      "id": "a9f599b5-1a55-406b-a76b-f52b3ca06975",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
            "Tool Calls:\n",
            "  search (call_LJlkCFfHvAS2taKHTaMmORE5)\n",
            " Call ID: call_LJlkCFfHvAS2taKHTaMmORE5\n",
            "  Args:\n",
            "    query: current weather in San Francisco\n",
            "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
            "Name: search\n",
            "\n",
            "[\"I looked up: current weather in San Francisco. Result: It's sunny in San Francisco, but you better look out if you're a Gemini \\ud83d\\ude08.\"]\n",
            "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
            "\n",
            "The current weather in San Francisco is sunny. Enjoy the good weather! 🌞\n"
          ]
        }
      ],
      "source": [
        "for event in app.stream(None, config, stream_mode=\"values\"):\n",
        "    event[\"messages\"][-1].pretty_print()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f6f972d1-3d99-4fc1-8b33-92b71e74835d",
      "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.8"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
