From cf7f6243d7fc80f29ccb1187b219d1f5e00349bd Mon Sep 17 00:00:00 2001 From: Lance Martin Date: Tue, 26 Mar 2024 16:45:57 -0700 Subject: [PATCH] Add set_conditional_entry_point --- examples/rag/adaptive_rag.ipynb | 96 +++++++++++++-------------------- 1 file changed, 38 insertions(+), 58 deletions(-) diff --git a/examples/rag/adaptive_rag.ipynb b/examples/rag/adaptive_rag.ipynb index 4326d0b3b..2d89fa88d 100644 --- a/examples/rag/adaptive_rag.ipynb +++ b/examples/rag/adaptive_rag.ipynb @@ -16,7 +16,7 @@ "\n", "Adaptive RAG is a strategy for RAG that unites (1) [query analysis](https://blog.langchain.dev/query-construction/) with (2) [active / self-corrective RAG](https://blog.langchain.dev/agentic-rag-with-langgraph/).\n", "\n", - "In the paper, they report query analysis to route across:\n", + "In the [paper](https://arxiv.org/abs/2403.14403), they report query analysis to route across:\n", "\n", "* No Retrieval\n", "* Single-shot RAG\n", @@ -84,7 +84,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 1, "id": "b224e5ba-50ca-495a-a7fa-0f75a080e03c", "metadata": {}, "outputs": [], @@ -136,7 +136,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 3, "id": "4dec9d98-f3dc-4b7f-abc0-9d01c754f2be", "metadata": {}, "outputs": [ @@ -190,7 +190,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 4, "id": "856801cb-f42a-44e7-956f-47845e3664ca", "metadata": {}, "outputs": [ @@ -198,7 +198,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "binary_score='yes'\n" + "binary_score='no'\n" ] } ], @@ -235,7 +235,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 5, "id": "2272333e-50b2-42ab-b472-e1055a3b94a8", "metadata": {}, "outputs": [ @@ -273,7 +273,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 6, "id": "f0c08d14-77a0-4eed-b882-2d636abb22a3", "metadata": {}, "outputs": [ @@ -283,7 +283,7 @@ "GradeHallucinations(binary_score='yes')" ] }, - "execution_count": 33, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -317,7 +317,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 7, "id": "ded99680-437a-4c9d-b860-619c88949d84", "metadata": {}, "outputs": [ @@ -327,7 +327,7 @@ "GradeAnswer(binary_score='yes')" ] }, - "execution_count": 34, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -361,7 +361,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 8, "id": "9d75f1d7-a47a-4577-bb0d-84b504b0867e", "metadata": {}, "outputs": [ @@ -371,7 +371,7 @@ "\"What is the role of memory in an agent's functioning?\"" ] }, - "execution_count": 35, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -406,7 +406,7 @@ }, { "cell_type": "code", - "execution_count": 69, + "execution_count": 9, "id": "01d829bb-1074-4976-b650-ead41dcb9788", "metadata": {}, "outputs": [], @@ -431,7 +431,7 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 10, "id": "e723fcdb-06e6-402d-912e-899795b78408", "metadata": {}, "outputs": [], @@ -463,28 +463,13 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": 11, "id": "b76b5ec3-0720-443d-85b1-c0e79659ca0a", "metadata": {}, "outputs": [], "source": [ "from langchain.schema import Document\n", "\n", - "def start_node(state):\n", - " \"\"\"\n", - " Starting node for graph\n", - "\n", - " Args:\n", - " state (dict): The current graph state\n", - "\n", - " Returns:\n", - " state (dict): The current graph state\n", - " \"\"\"\n", - " print(\"START NODE STATE\")\n", - " print(state)\n", - " question = state[\"question\"]\n", - " return {\"question\": question}\n", - "\n", "def retrieve(state):\n", " \"\"\"\n", " Retrieve documents\n", @@ -683,7 +668,7 @@ }, { "cell_type": "code", - "execution_count": 66, + "execution_count": 12, "id": "67854e07-9293-4c3c-bf9a-bc9a605570ee", "metadata": {}, "outputs": [], @@ -695,7 +680,6 @@ "workflow = StateGraph(GraphState)\n", "\n", "# Define the nodes\n", - "workflow.add_node(\"start\", start_node) # start node\n", "workflow.add_node(\"web_search\", web_search) # web search\n", "workflow.add_node(\"retrieve\", retrieve) # retrieve\n", "workflow.add_node(\"grade_documents\", grade_documents) # grade documents\n", @@ -703,9 +687,7 @@ "workflow.add_node(\"transform_query\", transform_query) # transform_query\n", "\n", "# Build graph\n", - "workflow.set_entry_point(\"start\")\n", - "workflow.add_conditional_edges(\n", - " \"start\",\n", + "workflow.set_conditional_entry_point(\n", " route_question,\n", " {\n", " \"web_search\": \"web_search\",\n", @@ -732,7 +714,6 @@ " \"not useful\": \"transform_query\",\n", " },\n", ")\n", - "workflow.set_finish_point(\"start\")\n", "\n", "# Compile\n", "app = workflow.compile()" @@ -740,7 +721,7 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 13, "id": "29acc541-d726-4b75-84d1-a215845fe88a", "metadata": {}, "outputs": [ @@ -748,14 +729,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "START NODE STATE\n", - "{'question': 'What player at the Bears expected to draft first in the 2024 NFL draft?', 'generation': None, 'documents': None}\n", - "\"Node 'start':\"\n", - "'\\n---\\n'\n", "---ROUTE QUESTION---\n", "---ROUTE QUESTION TO WEB SEARCH---\n", - "\"Node '__end__':\"\n", - "'\\n---\\n'\n", "---WEB SEARCH---\n", "\"Node 'web_search':\"\n", "'\\n---\\n'\n", @@ -768,8 +743,10 @@ "---DECISION: GENERATION ADDRESSES QUESTION---\n", "\"Node '__end__':\"\n", "'\\n---\\n'\n", - "('Several NFL analysts expect the Bears to draft USC quarterback Caleb '\n", - " 'Williams with the No. 1 pick in the 2024 NFL draft.')\n" + "('Several NFL analysts expect the Bears to select USC quarterback Caleb '\n", + " 'Williams at No. 1 in the 2024 NFL draft. The Chicago Bears will have the No. '\n", + " '1 pick in the draft. The draft will take place in Detroit at Campus Martius '\n", + " 'Park and Hart Plaza.')\n" ] } ], @@ -795,12 +772,12 @@ "source": [ "Trace: \n", "\n", - "https://smith.langchain.com/public/a33fa139-89e6-40c9-bc63-63984e9709af/r" + "https://smith.langchain.com/public/7e3aa7e5-c51f-45c2-bc66-b34f17ff2263/r" ] }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 14, "id": "69a985dd-03c6-45af-a67b-b15746a2cb5f", "metadata": {}, "outputs": [ @@ -808,14 +785,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "START NODE STATE\n", - "{'question': 'What are the types of agent memory?', 'generation': None, 'documents': None}\n", - "\"Node 'start':\"\n", - "'\\n---\\n'\n", "---ROUTE QUESTION---\n", "---ROUTE QUESTION TO RAG---\n", - "\"Node '__end__':\"\n", - "'\\n---\\n'\n", "---RETRIEVE---\n", "\"Node 'retrieve':\"\n", "'\\n---\\n'\n", @@ -839,10 +810,11 @@ "'\\n---\\n'\n", "('The types of agent memory are sensory memory, short-term memory, and '\n", " 'long-term memory. Sensory memory involves learning embedding representations '\n", - " 'for raw inputs, short-term memory is limited by the context window length of '\n", - " 'Transformer, and long-term memory is an external vector store accessible at '\n", - " 'query time. The external memory can support fast maximum inner-product '\n", - " 'search (MIPS) to alleviate the restriction of finite attention span.')\n" + " 'for raw inputs, short-term memory is in-context learning with a finite '\n", + " 'context window length, and long-term memory is an external vector store '\n", + " 'accessible via fast retrieval. The external memory can support fast maximum '\n", + " 'inner-product search (MIPS) to alleviate the restriction of finite attention '\n", + " 'span.')\n" ] } ], @@ -868,8 +840,16 @@ "source": [ "Trace: \n", "\n", - "https://smith.langchain.com/public/95e8075d-fdfe-475c-b6cf-a4699f160af7/r" + "https://smith.langchain.com/public/fdf0a180-6d15-4d09-bb92-f84f2105ca51/r" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "19ac1f6f-2d84-488f-8a0e-7ee2a46b0f71", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {