diff --git a/docs/docs/how_to/graph_semantic.ipynb b/docs/docs/how_to/graph_semantic.ipynb index 4cd717d861297..89250edc155a2 100644 --- a/docs/docs/how_to/graph_semantic.ipynb +++ b/docs/docs/how_to/graph_semantic.ipynb @@ -31,20 +31,12 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "ffdd48f6-bd05-4e5c-b846-d41183398a55", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Note: you may need to restart the kernel to use updated packages.\n" - ] - } - ], + "outputs": [], "source": [ - "%pip install --upgrade --quiet langchain langchain-neo4j langchain-openai neo4j" + "%pip install --upgrade --quiet langchain langchain-neo4j langchain-openai" ] }, { @@ -62,7 +54,7 @@ "metadata": {}, "outputs": [ { - "name": "stdout", + "name": "stdin", "output_type": "stream", "text": [ " ········\n" @@ -129,7 +121,7 @@ "source": [ "from langchain_neo4j import Neo4jGraph\n", "\n", - "graph = Neo4jGraph()\n", + "graph = Neo4jGraph(refresh_schema=False)\n", "\n", "# Import movie information\n", "\n", @@ -175,21 +167,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Optional, Type\n", - "\n", - "from langchain_core.callbacks import (\n", - " AsyncCallbackManagerForToolRun,\n", - " CallbackManagerForToolRun,\n", - ")\n", - "from langchain_core.tools import BaseTool\n", - "\n", - "# Import things that are needed generically\n", - "from pydantic import BaseModel, Field\n", - "\n", "description_query = \"\"\"\n", "MATCH (m:Movie|Person)\n", "WHERE m.title CONTAINS $candidate OR m.name CONTAINS $candidate\n", - "MATCH (m)-[r:ACTED_IN|HAS_GENRE]-(t)\n", + "MATCH (m)-[r:ACTED_IN|IN_GENRE]-(t)\n", "WITH m, type(r) as type, collect(coalesce(t.name, t.title)) as names\n", "WITH m, type+\": \"+reduce(s=\"\", n IN names | s + n + \", \") as types\n", "WITH m, collect(types) as contexts\n", @@ -225,18 +206,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Optional, Type\n", - "\n", - "from langchain_core.callbacks import (\n", - " AsyncCallbackManagerForToolRun,\n", - " CallbackManagerForToolRun,\n", - ")\n", + "from typing import Type, Optional\n", "from langchain_core.tools import BaseTool\n", - "\n", - "# Import things that are needed generically\n", "from pydantic import BaseModel, Field\n", "\n", - "\n", "class InformationInput(BaseModel):\n", " entity: str = Field(description=\"movie or a person mentioned in the question\")\n", "\n", @@ -251,7 +224,6 @@ " def _run(\n", " self,\n", " entity: str,\n", - " run_manager: Optional[CallbackManagerForToolRun] = None,\n", " ) -> str:\n", " \"\"\"Use the tool.\"\"\"\n", " return get_information(entity)\n", @@ -259,7 +231,6 @@ " async def _arun(\n", " self,\n", " entity: str,\n", - " run_manager: Optional[AsyncCallbackManagerForToolRun] = None,\n", " ) -> str:\n", " \"\"\"Use the tool asynchronously.\"\"\"\n", " return get_information(entity)" @@ -270,9 +241,15 @@ "id": "ff4820aa-2b57-4558-901f-6d984b326738", "metadata": {}, "source": [ - "## OpenAI Agent\n", + "## LangGraph Agent\n", + "\n", + "We will implement a straightforward ReAct agent using LangGraph.\n", + "\n", + "The agent consists of an LLM and tools step. As we interact with the agent, we will first call the LLM to decide if we should use tools. Then we will run a loop:\n", "\n", - "LangChain expression language makes it very convenient to define an agent to interact with a graph database over the semantic layer." + "If the agent said to take an action (i.e. call tool), we’ll run the tools and pass the results back to the agent\n", + "If the agent did not ask to run tools, we will finish (respond to the user)\n", + "The code implementation is as straightforward as it gets. First we bind the tools to the LLM and define the assistant step." ] }, { @@ -282,67 +259,90 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import List, Tuple\n", - "\n", - "from langchain.agents import AgentExecutor\n", - "from langchain.agents.format_scratchpad import format_to_openai_function_messages\n", - "from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser\n", - "from langchain_core.messages import AIMessage, HumanMessage\n", - "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n", - "from langchain_core.utils.function_calling import convert_to_openai_function\n", "from langchain_openai import ChatOpenAI\n", + "from langchain_core.messages import SystemMessage, HumanMessage\n", + "from langgraph.graph import MessagesState\n", "\n", - "llm = ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=0)\n", - "tools = [InformationTool()]\n", + "llm = ChatOpenAI(model='gpt-4o')\n", "\n", - "llm_with_tools = llm.bind(functions=[convert_to_openai_function(t) for t in tools])\n", + "tools = [InformationTool()]\n", + "llm_with_tools = llm.bind_tools(tools)\n", "\n", - "prompt = ChatPromptTemplate.from_messages(\n", - " [\n", - " (\n", - " \"system\",\n", - " \"You are a helpful assistant that finds information about movies \"\n", - " \" and recommends them. If tools require follow up questions, \"\n", - " \"make sure to ask the user for clarification. Make sure to include any \"\n", - " \"available options that need to be clarified in the follow up questions \"\n", - " \"Do only the things the user specifically requested. \",\n", - " ),\n", - " MessagesPlaceholder(variable_name=\"chat_history\"),\n", - " (\"user\", \"{input}\"),\n", - " MessagesPlaceholder(variable_name=\"agent_scratchpad\"),\n", - " ]\n", + "# System message\n", + "sys_msg = SystemMessage(\n", + " content=\"You are a helpful assistant tasked with finding and explaining relevant information about movies.\"\n", ")\n", "\n", - "\n", - "def _format_chat_history(chat_history: List[Tuple[str, str]]):\n", - " buffer = []\n", - " for human, ai in chat_history:\n", - " buffer.append(HumanMessage(content=human))\n", - " buffer.append(AIMessage(content=ai))\n", - " return buffer\n", - "\n", - "\n", - "agent = (\n", - " {\n", - " \"input\": lambda x: x[\"input\"],\n", - " \"chat_history\": lambda x: _format_chat_history(x[\"chat_history\"])\n", - " if x.get(\"chat_history\")\n", - " else [],\n", - " \"agent_scratchpad\": lambda x: format_to_openai_function_messages(\n", - " x[\"intermediate_steps\"]\n", - " ),\n", - " }\n", - " | prompt\n", - " | llm_with_tools\n", - " | OpenAIFunctionsAgentOutputParser()\n", + "# Node\n", + "def assistant(state: MessagesState):\n", + " return {\"messages\": [llm_with_tools.invoke([sys_msg] + state[\"messages\"])]}" + ] + }, + { + "cell_type": "markdown", + "id": "087e745b-6ce6-4f1f-9a52-a6de597971bf", + "metadata": {}, + "source": [ + "Next we define the LangGraph flow." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "6763e87b-a6d2-44ed-9000-7440b51b325f", + "metadata": {}, + "outputs": [ + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAANYAAAD5CAIAAADUe1yaAAAAAXNSR0IArs4c6QAAIABJREFUeJztnWlcU8fex+ckIWSHJOwgm+yKKyqtuFWolaoXqCtaa1tvq7V6W9cutLWL1i5a6r19alute8UVFYuCe5W6YaUKqAXZRAiEBBISsuc8L+KH0hgQNefMCZnvxxdylvn/En7MmZkz8x8Mx3GAQMCDBlsAwtlBFkRABlkQARlkQQRkkAURkEEWRECGAVvA46CUG5QyQ5vSpG41GvWOMazEcMHoDIzDp3MEDLEvk8Whw1ZEFTDH+AUCAACQ3tPe+VNdWaLmChgmI84R0Ll8BpNNA47wCRiumKrZ2NZqalMa1QoT140e0pcbPoDHE7rAlgYZx7CgQmb4/XAT3QUTejFD+nA9/F1hK3pS7t3RVBar5RKduyfz6YlihovztogcwIKXjspuF7Y+PckjrD8Pthb78+dvLb/nyEakevR92g22FjhQ3YL7vq3tO1wQFSeALYRYLufJW+WGsTO8YQuBAHUtiOP4j+9WTHrdzzeEDVsLGZReUlaVqJNf8YUthGyoa8Hvl5fPzgjmChyyz/543LqiLP5dOfk/AbCFkApFLbgvs3Z4itg32Cnqv47cKFDI6nSjp3jBFkIeVOyIXcyVxY4QOKH/AACxw904fPrNy0rYQsiDchZsbtSXF6kiB/fw/kcXDBorPLNXClsFeVDOgr/nyJ6eKIatAiYMF9rgROGlozLYQkiCWhaUVGld2bTQ2B44/vdIDB0nklRpDXozbCFkQC0L3rmuEvkwSQtXXFys0+lg3d41LC69slhNUOGUgloWrCxRh/ThkhMrJydnzpw5Go0Gyu0PJaQvF1mQbJob9QIRQ+hNUi342BWYZRiLuPrPQmgsVyEzEBqCIlDIgoomA4ZhRJRcXV09b968hISE5OTk1atXm83mnJycNWvWAAASExPj4uJycnIAAEVFRW+++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uX/SLh5uhC2EAJhT2z/lvhCF2m1Y+dFqLyuEvv2hBVPPR7bFvTq5UrJnCfdpUWqD+7DYbigatAB6LQW9A9j/bZfQroe+3ByZ118MhV3IEc8SFf7EZdcUJQVqfqPEgu9mZ1NbqMUGpVR0WT4bZ/khYX+7t14NYSgAg/ZEruyRF10tkVSqaUzqP5gFvm6KqT60L6coePFXAHq6TsMD7FgOzoN1bekw3HA4jhAVY2worsWRCAIAlUbCMggCyIggyyIgAyyIAIyyIIIyCALIiDz/x8c2UhUcKGwAAAAAElFTkSuQmCC\n", + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "from langgraph.graph import StateGraph, START, END\n", + "from langgraph.prebuilt import tools_condition\n", + "from langgraph.prebuilt import ToolNode\n", + "\n", + "from IPython.display import Image, display\n", + "\n", + "# Graph\n", + "builder = StateGraph(MessagesState)\n", + "\n", + "# Define nodes: these do the work\n", + "builder.add_node(\"assistant\", assistant)\n", + "builder.add_node(\"tools\", ToolNode(tools))\n", + "\n", + "# Define edges: these determine how the control flow moves\n", + "builder.add_edge(START, \"assistant\")\n", + "builder.add_conditional_edges(\n", + " \"assistant\",\n", + " # If the latest message (result) from assistant is a tool call -> tools_condition routes to tools\n", + " # If the latest message (result) from assistant is a not a tool call -> tools_condition routes to END\n", + " tools_condition,\n", ")\n", + "builder.add_edge(\"tools\", \"assistant\")\n", + "react_graph = builder.compile()\n", "\n", - "agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)" + "# Show\n", + "display(Image(react_graph.get_graph(xray=True).draw_mermaid_png()))" + ] + }, + { + "cell_type": "markdown", + "id": "c038c048-5a1a-4d6a-a904-387a85e3e549", + "metadata": {}, + "source": [ + "Let's test the workflow now with an example question." ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "id": "b0459833-fe84-4ebc-9823-a3a3ffd929e9", "metadata": {}, "outputs": [ @@ -350,45 +350,47 @@ "name": "stdout", "output_type": "stream", "text": [ + "================================\u001b[1m Human Message \u001b[0m=================================\n", "\n", + "Who played in the Casino?\n", + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "Tool Calls:\n", + " Information (call_oF5NAq2jERlhcnDB4ElKeeI8)\n", + " Call ID: call_oF5NAq2jERlhcnDB4ElKeeI8\n", + " Args:\n", + " entity: Casino\n", + "=================================\u001b[1m Tool Message \u001b[0m=================================\n", + "Name: Information\n", "\n", - "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n", - "\u001b[32;1m\u001b[1;3m\n", - "Invoking: `Information` with `{'entity': 'Casino'}`\n", - "\n", - "\n", - "\u001b[0m\u001b[36;1m\u001b[1;3mtype:Movie\n", + "type:Movie\n", "title: Casino\n", "year: 1995-11-22\n", - "ACTED_IN: Joe Pesci, Robert De Niro, Sharon Stone, James Woods\n", - "\u001b[0m\u001b[32;1m\u001b[1;3mThe movie \"Casino\" starred Joe Pesci, Robert De Niro, Sharon Stone, and James Woods.\u001b[0m\n", + "ACTED_IN: Robert De Niro, Joe Pesci, Sharon Stone, James Woods\n", + "IN_GENRE: Drama, Crime\n", + "\n", + "==================================\u001b[1m Ai Message \u001b[0m==================================\n", + "\n", + "The movie \"Casino,\" released on November 22, 1995, features the following actors:\n", + "\n", + "- Robert De Niro\n", + "- Joe Pesci\n", + "- Sharon Stone\n", + "- James Woods\n", "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" + "The film belongs to the Drama and Crime genres.\n" ] - }, - { - "data": { - "text/plain": [ - "{'input': 'Who played in Casino?',\n", - " 'output': 'The movie \"Casino\" starred Joe Pesci, Robert De Niro, Sharon Stone, and James Woods.'}" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" } ], "source": [ - "agent_executor.invoke({\"input\": \"Who played in Casino?\"})" + "messages = [\n", + " HumanMessage(\n", + " content=\"Who played in the Casino?\"\n", + " )\n", + "]\n", + "messages = react_graph.invoke({\"messages\": messages})\n", + "for m in messages[\"messages\"]:\n", + " m.pretty_print()" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "c2759973-de8a-4624-8930-c90a21d6caa3", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -407,7 +409,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.1" + "version": "3.11.5" } }, "nbformat": 4,