diff --git a/examples/create-react-agent-hitl.ipynb b/examples/create-react-agent-hitl.ipynb index 7a14da497..235f2b057 100644 --- a/examples/create-react-agent-hitl.ipynb +++ b/examples/create-react-agent-hitl.ipynb @@ -73,7 +73,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "id": "7a154152-973e-4b5d-aa13-48c617744a4c", "metadata": {}, "outputs": [], @@ -92,14 +92,14 @@ "\n", "\n", "@tool\n", - "def get_weather(city: Literal[\"nyc\", \"sf\"]):\n", - " \"\"\"Use this to get weather information.\"\"\"\n", - " if city == \"nyc\":\n", + "def get_weather(location: str):\n", + " \"\"\"Use this to get weather information from a given location.\"\"\"\n", + " if location.lower() in [\"nyc\", \"new york\"]:\n", " return \"It might be cloudy in nyc\"\n", - " elif city == \"sf\":\n", + " elif location.lower() in [\"sf\", \"san francisco\"]:\n", " return \"It's always sunny in sf\"\n", " else:\n", - " raise AssertionError(\"Unknown city\")\n", + " raise AssertionError(\"Unknown Location\")\n", "\n", "\n", "tools = [get_weather]\n", @@ -144,7 +144,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 3, "id": "9ffff6c3-a4f5-47c9-b51d-97caaee85cd6", "metadata": {}, "outputs": [ @@ -154,26 +154,35 @@ "text": [ "================================\u001b[1m Human Message \u001b[0m=================================\n", "\n", - "What's the weather in SF?\n", + "what is the weather in SF?\n", "==================================\u001b[1m Ai Message \u001b[0m==================================\n", "Tool Calls:\n", - " get_weather (call_0OMmuTLec9t8kxMVkllZCSxo)\n", - " Call ID: call_0OMmuTLec9t8kxMVkllZCSxo\n", + " get_weather (call_TcDfLuoCKLmQ7eG71SedxLZ6)\n", + " Call ID: call_TcDfLuoCKLmQ7eG71SedxLZ6\n", " Args:\n", - " city: sf\n" + " location: San Francisco, CA\n" ] } ], "source": [ + "from langchain_core.messages import HumanMessage\n", "config = {\"configurable\": {\"thread_id\": \"42\"}}\n", - "inputs = {\"messages\": [(\"user\", \"What's the weather in SF?\")]}\n", + "inputs = {\"messages\": [(\"user\", \"what is the weather in SF?\")]}\n", "\n", "print_stream(graph.stream(inputs, config, stream_mode=\"values\"))" ] }, + { + "cell_type": "markdown", + "id": "ca40a719", + "metadata": {}, + "source": [ + "We can verify that our graph stopped at the right place:" + ] + }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "3decf001-7228-4ed5-8779-2b9ed98a74ea", "metadata": {}, "outputs": [ @@ -190,9 +199,87 @@ "print(\"Next step: \", snapshot.next)" ] }, + { + "cell_type": "markdown", + "id": "7de6ca78", + "metadata": {}, + "source": [ + "Now we can either approve or edit the tool call before proceeding to the next node. If we wanted to approve the tool call, we would simply continue streaming the graph with `None` input. If we wanted to edit the tool call we need to update the state to have the correct tool call, and then after the update has been applied we can continue.\n", + "\n", + "We can try resuming and we will see an error arise:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "740bbaeb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "=================================\u001b[1m Tool Message \u001b[0m=================================\n", + "Name: get_weather\n", + "\n", + "Error: AssertionError('Unknown Location')\n", + " Please fix your mistakes.\n", + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "\n", + "It seems there was an issue with the location provided. Let's try specifying \"San Francisco, California\" more clearly.\n", + "Tool Calls:\n", + " get_weather (call_TZm9HCShGNEreglVJcmUdXqG)\n", + " Call ID: call_TZm9HCShGNEreglVJcmUdXqG\n", + " Args:\n", + " location: San Francisco, California\n" + ] + } + ], + "source": [ + "print_stream(graph.stream(None, config, stream_mode=\"values\"))" + ] + }, + { + "cell_type": "markdown", + "id": "c1cf5950", + "metadata": {}, + "source": [ + "This error arose because our tool argument of \"San Francisco, CA\" is not a location our tool recognizes.\n", + "\n", + "Let's show how we would edit the tool call to search for \"San Francisco\" instead of \"San Francisco, CA\" - since our tool as written treats \"San Francisco, CA\" as an unknown location. We will update the state and then resume streaming the graph and should see no errors arise:" + ] + }, { "cell_type": "code", "execution_count": 6, + "id": "1c81ed9f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'configurable': {'thread_id': '42',\n", + " 'checkpoint_ns': '',\n", + " 'checkpoint_id': '1ef66368-9772-67ea-8004-07c779869a0a'}}" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "state = graph.get_state(config)\n", + "\n", + "last_message = state.values['messages'][-1]\n", + "last_message.tool_calls[0]['args'] = {\"location\": \"San Francisco\"}\n", + "\n", + "graph.update_state(config, {\"messages\": [ last_message]})" + ] + }, + { + "cell_type": "code", + "execution_count": 7, "id": "83148e08-63e8-49e5-a08b-02dc907bed1d", "metadata": {}, "outputs": [ @@ -206,13 +293,21 @@ "It's always sunny in sf\n", "==================================\u001b[1m Ai Message \u001b[0m==================================\n", "\n", - "The weather in San Francisco is currently sunny.\n" + "The weather in San Francisco is currently sunny. Enjoy the sunshine!\n" ] } ], "source": [ "print_stream(graph.stream(None, config, stream_mode=\"values\"))" ] + }, + { + "cell_type": "markdown", + "id": "8202a5f9", + "metadata": {}, + "source": [ + "Fantastic! Our graph updated properly to query the weather in San Francisco and got the correct \"It's always sunny in sf\" response from the tool, and then responded to the user accordingly." + ] } ], "metadata": { @@ -231,7 +326,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.2" + "version": "3.11.9" } }, "nbformat": 4,