{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%%capture --no-stderr\n", "%pip install -U tavily-python langchain_community" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os\n", "from dotenv import load_dotenv\n", "\n", "load_dotenv()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n", "model = os.getenv(\"OPENAI_MODEL\", \"gpt-4o\")\n", "temperature = float(os.getenv(\"OPENAI_TEMPERATURE\", 0))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from langchain_community.tools.tavily_search import TavilySearchResults\n", "\n", "tool = TavilySearchResults(max_results=2)\n", "tools = [tool]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from typing import Annotated\n", "from langchain_openai import ChatOpenAI as Chat\n", "\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", "from typing_extensions import TypedDict\n", "\n", "from langgraph.checkpoint.memory import MemorySaver\n", "from langgraph.graph import StateGraph, START\n", "from langgraph.graph.message import add_messages\n", "from langgraph.prebuilt import ToolNode, tools_condition\n", "\n", "memory = MemorySaver()\n", "\n", "\n", "class State(TypedDict):\n", " messages: Annotated[list, add_messages]\n", "\n", "\n", "graph_builder = StateGraph(State)\n", "\n", "\n", "tool = TavilySearchResults(max_results=2)\n", "tools = [tool]\n", "llm = Chat(\n", " openai_api_key=openai_api_key,\n", " model=model,\n", " temperature=temperature\n", ")\n", "llm_with_tools = llm.bind_tools(tools)\n", "\n", "\n", "def chatbot(state: State):\n", " return {\"messages\": [llm_with_tools.invoke(state[\"messages\"])]}\n", "\n", "\n", "graph_builder.add_node(\"chatbot\", chatbot)\n", "\n", "tool_node = ToolNode(tools=[tool])\n", "graph_builder.add_node(\"tools\", tool_node)\n", "\n", "graph_builder.add_conditional_edges(\n", " \"chatbot\",\n", " tools_condition,\n", ")\n", "graph_builder.add_edge(\"tools\", \"chatbot\")\n", "graph_builder.add_edge(START, \"chatbot\")\n", "memory = MemorySaver()\n", "graph = graph_builder.compile(\n", " checkpointer=memory,\n", " # This is new!\n", " interrupt_before=[\"tools\"],\n", " # Note: can also interrupt **after** actions, if desired.\n", " # interrupt_after=[\"tools\"]\n", ")\n", "\n", "user_input = \"I'm learning LangGraph. Could you do some research on it for me?\"\n", "config = {\"configurable\": {\"thread_id\": \"1\"}}\n", "# The config is the **second positional argument** to stream() or invoke()!\n", "events = graph.stream({\"messages\": [(\"user\", user_input)]}, config)\n", "for event in events:\n", " if \"messages\" in event:\n", " event[\"messages\"][-1].pretty_print()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "==================================\u001b[1m Ai Message \u001b[0m==================================\n", "Tool Calls:\n", " tavily_search_results_json (call_LGHiKqqnTR0xCTMQj4iKZ2Uy)\n", " Call ID: call_LGHiKqqnTR0xCTMQj4iKZ2Uy\n", " Args:\n", " query: LangGraph programming language\n" ] } ], "source": [ "snapshot = graph.get_state(config)\n", "existing_message = snapshot.values[\"messages\"][-1]\n", "existing_message.pretty_print()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "==================================\u001b[1m Ai Message \u001b[0m==================================\n", "\n", "LangGraph is a library for building stateful, multi-actor applications with LLMs.\n", "\n", "\n", "Last 2 messages;\n", "[ToolMessage(content='LangGraph is a library for building stateful, multi-actor applications with LLMs.', id='c7ee8f20-19bc-430f-a7ad-4219c25fc6ec', tool_call_id='call_LGHiKqqnTR0xCTMQj4iKZ2Uy'), AIMessage(content='LangGraph is a library for building stateful, multi-actor applications with LLMs.', additional_kwargs={}, response_metadata={}, id='a7973d5c-e60b-4c97-8876-d32b9e280acf')]\n" ] } ], "source": [ "from langchain_core.messages import AIMessage, ToolMessage\n", "\n", "answer = (\n", " \"LangGraph is a library for building stateful, multi-actor applications with LLMs.\"\n", ")\n", "new_messages = [\n", " # The LLM API expects some ToolMessage to match its tool call. We'll satisfy that here.\n", " ToolMessage(content=answer,\n", " tool_call_id=existing_message.tool_calls[0][\"id\"]),\n", " # And then directly \"put words in the LLM's mouth\" by populating its response.\n", " AIMessage(content=answer),\n", "]\n", "\n", "new_messages[-1].pretty_print()\n", "graph.update_state(\n", " # Which state to update\n", " config,\n", " # The updated values to provide. The messages in our `State` are \"append-only\", meaning this will be appended\n", " # to the existing state. We will review how to update existing messages in the next section!\n", " {\"messages\": new_messages},\n", ")\n", "\n", "print(\"\\n\\nLast 2 messages;\")\n", "print(graph.get_state(config).values[\"messages\"][-2:])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "class State(TypedDict):\n", " messages: Annotated[list, add_messages]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'configurable': {'thread_id': '1',\n", " 'checkpoint_ns': '',\n", " 'checkpoint_id': '1ef97736-c6dd-6e14-8003-b885bde8bfbe'}}" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "graph.update_state(\n", " config,\n", " {\"messages\": [AIMessage(content=\"I'm an AI expert!\")]},\n", " # Which node for this function to act as. It will automatically continue\n", " # processing as if this node just ran.\n", " as_node=\"chatbot\",\n", ")" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/4gHYSUNDX1BST0ZJTEUAAQEAAAHIAAAAAAQwAABtbnRyUkdCIFhZWiAH4AABAAEAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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q==", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from IPython.display import Image, display\n", "\n", "try:\n", " display(Image(graph.get_graph().draw_mermaid_png()))\n", "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ToolMessage(content='LangGraph is a library for building stateful, multi-actor applications with LLMs.', id='c7ee8f20-19bc-430f-a7ad-4219c25fc6ec', tool_call_id='call_LGHiKqqnTR0xCTMQj4iKZ2Uy'), AIMessage(content='LangGraph is a library for building stateful, multi-actor applications with LLMs.', additional_kwargs={}, response_metadata={}, id='a7973d5c-e60b-4c97-8876-d32b9e280acf'), AIMessage(content=\"I'm an AI expert!\", additional_kwargs={}, response_metadata={}, id='e70e7694-b6f1-44bb-bb03-01e6140d8452')]\n", "()\n" ] } ], "source": [ "snapshot = graph.get_state(config)\n", "print(snapshot.values[\"messages\"][-3:])\n", "print(snapshot.next)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "================================\u001b[1m Human Message \u001b[0m=================================\n", "\n", "I'm learning LangGraph. Could you do some research on it for me?\n", "==================================\u001b[1m Ai Message \u001b[0m==================================\n", "Tool Calls:\n", " tavily_search_results_json (call_a5QcJ8OTfJ81NCohydwHBe43)\n", " Call ID: call_a5QcJ8OTfJ81NCohydwHBe43\n", " Args:\n", " query: LangGraph programming language\n" ] } ], "source": [ "user_input = \"I'm learning LangGraph. Could you do some research on it for me?\"\n", "config = {\"configurable\": {\"thread_id\": \"2\"}} # we'll use thread_id = 2 here\n", "events = graph.stream(\n", " {\"messages\": [(\"user\", user_input)]}, config, stream_mode=\"values\"\n", ")\n", "for event in events:\n", " if \"messages\" in event:\n", " event[\"messages\"][-1].pretty_print()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original\n", "Message ID run-2f27ab36-7466-40c5-adcf-18972d1bc9fa-0\n", "{'name': 'tavily_search_results_json', 'args': {'query': 'LangGraph programming language'}, 'id': 'call_a5QcJ8OTfJ81NCohydwHBe43', 'type': 'tool_call'}\n", "Updated\n", "{'name': 'tavily_search_results_json', 'args': {'query': 'LangGraph human-in-the-loop workflow'}, 'id': 'call_a5QcJ8OTfJ81NCohydwHBe43', 'type': 'tool_call'}\n", "Message ID run-2f27ab36-7466-40c5-adcf-18972d1bc9fa-0\n", "\n", "\n", "Tool calls\n" ] }, { "data": { "text/plain": [ "[{'name': 'tavily_search_results_json',\n", " 'args': {'query': 'LangGraph human-in-the-loop workflow'},\n", " 'id': 'call_a5QcJ8OTfJ81NCohydwHBe43',\n", " 'type': 'tool_call'}]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from langchain_core.messages import AIMessage\n", "\n", "snapshot = graph.get_state(config)\n", "existing_message = snapshot.values[\"messages\"][-1]\n", "print(\"Original\")\n", "print(\"Message ID\", existing_message.id)\n", "print(existing_message.tool_calls[0])\n", "new_tool_call = existing_message.tool_calls[0].copy()\n", "new_tool_call[\"args\"][\"query\"] = \"LangGraph human-in-the-loop workflow\"\n", "new_message = AIMessage(\n", " content=existing_message.content,\n", " tool_calls=[new_tool_call],\n", " # Important! The ID is how LangGraph knows to REPLACE the message in the state rather than APPEND this messages\n", " id=existing_message.id,\n", ")\n", "\n", "print(\"Updated\")\n", "print(new_message.tool_calls[0])\n", "print(\"Message ID\", new_message.id)\n", "graph.update_state(config, {\"messages\": [new_message]})\n", "\n", "print(\"\\n\\nTool calls\")\n", "graph.get_state(config).values[\"messages\"][-1].tool_calls" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "==================================\u001b[1m Ai Message \u001b[0m==================================\n", "Tool Calls:\n", " tavily_search_results_json (call_a5QcJ8OTfJ81NCohydwHBe43)\n", " Call ID: call_a5QcJ8OTfJ81NCohydwHBe43\n", " Args:\n", " query: LangGraph human-in-the-loop workflow\n", "=================================\u001b[1m Tool Message \u001b[0m=================================\n", "Name: tavily_search_results_json\n", "\n", "[{\"url\": \"https://www.youtube.com/watch?v=9BPCV5TYPmg\", \"content\": \"In this video, I'll show you how to handle persistence with LangGraph, enabling a unique Human-in-the-Loop workflow. This approach allows a human to grant an\"}, {\"url\": \"https://medium.com/@kbdhunga/implementing-human-in-the-loop-with-langgraph-ccfde023385c\", \"content\": \"Implementing a Human-in-the-Loop (HIL) framework in LangGraph with the Streamlit app provides a robust mechanism for user engagement and decision-making. By incorporating breakpoints and\"}]\n", "==================================\u001b[1m Ai Message \u001b[0m==================================\n", "\n", "LangGraph is a tool that supports a Human-in-the-Loop (HIL) workflow, which is a process that allows human intervention in automated systems to improve decision-making and outcomes. Here are some resources that might help you understand LangGraph better:\n", "\n", "1. **YouTube Video**: [Handling Persistence with LangGraph](https://www.youtube.com/watch?v=9BPCV5TYPmg) - This video demonstrates how to manage persistence in LangGraph, enabling a unique Human-in-the-Loop workflow. It shows how a human can intervene in the process to grant permissions or make decisions.\n", "\n", "2. **Medium Article**: [Implementing Human-in-the-Loop with LangGraph](https://medium.com/@kbdhunga/implementing-human-in-the-loop-with-langgraph-ccfde023385c) - This article discusses implementing a Human-in-the-Loop framework in LangGraph using a Streamlit app. It provides insights into creating a robust mechanism for user engagement and decision-making by incorporating breakpoints and other interactive elements.\n", "\n", "These resources should give you a good starting point to understand how LangGraph can be used to integrate human decision-making into automated workflows.\n" ] } ], "source": [ "events = graph.stream(None, config, stream_mode=\"values\")\n", "for event in events:\n", " if \"messages\" in event:\n", " event[\"messages\"][-1].pretty_print()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "================================\u001b[1m Human Message \u001b[0m=================================\n", "\n", "Remember what I'm learning about?\n", "==================================\u001b[1m Ai Message \u001b[0m==================================\n", "\n", "You're learning about LangGraph, specifically focusing on its Human-in-the-Loop workflow capabilities.\n" ] } ], "source": [ "events = graph.stream(\n", " {\n", " \"messages\": (\n", " \"user\",\n", " \"Remember what I'm learning about?\",\n", " )\n", " },\n", " config,\n", " stream_mode=\"values\",\n", ")\n", "for event in events:\n", " if \"messages\" in event:\n", " event[\"messages\"][-1].pretty_print()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.7" } }, "nbformat": 4, "nbformat_minor": 2 }