mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-09-23 18:15:08 +02:00
Minor updates
This commit is contained in:
@@ -20,9 +20,23 @@
|
||||
"id": "8889a307-fa3f-4d38-9127-d41e4686ae47",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# CRAG\n",
|
||||
"# Corrective RAG (CRAG)\n",
|
||||
"\n",
|
||||
"Corrective-RAG is a recent paper that introduces an interesting approach for active RAG. \n",
|
||||
"Self-reflection can enhance RAG, enabling correction of poor quality retrieval or generations.\n",
|
||||
"\n",
|
||||
"Several recent papers focus on this theme, but implementing the ideas can be tricky.\n",
|
||||
"\n",
|
||||
"Here we show how to implement ideas from the `Corrective RAG (CRAG)` paper [here](https://arxiv.org/pdf/2401.15884.pdf) using LangGraph.\n",
|
||||
"\n",
|
||||
"## Dependencies\n",
|
||||
"\n",
|
||||
"Set `OPENAI_API_KEY`\n",
|
||||
"\n",
|
||||
"Set `TAVILY_API_KEY` to enable web search [here](https://app.tavily.com/sign-in)\n",
|
||||
"\n",
|
||||
"## CRAG Detail\n",
|
||||
"\n",
|
||||
"Corrective-RAG (CRAG) is a recent paper that introduces an interesting approach for self-reflective RAG. \n",
|
||||
"\n",
|
||||
"The framework grades retrieved documents relative to the question:\n",
|
||||
"\n",
|
||||
@@ -41,22 +55,9 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Paper -\n",
|
||||
"\n",
|
||||
"https://arxiv.org/pdf/2401.15884.pdf\n",
|
||||
"\n",
|
||||
"---\n",
|
||||
"\n",
|
||||
"Let's implement this from scratch using [LangGraph](https://python.langchain.com/docs/langgraph).\n",
|
||||
"\n",
|
||||
"We can make some simplifications:\n",
|
||||
"\n",
|
||||
"* Let's skip the knowledge refinement phase as a first pass. This can be added back as a node, if desired. \n",
|
||||
"* If *any* document is irrelevant, let's opt to supplement retrieval with web search. \n",
|
||||
"* We'll use [Tavily Search](https://python.langchain.com/docs/integrations/tools/tavily_search) for web search.\n",
|
||||
"* Let's use query re-writing to optimize the query for web search.\n",
|
||||
"\n",
|
||||
"Set the `TAVILY_API_KEY`."
|
||||
"Let's implement some of these ideas from scratch using [LangGraph](https://python.langchain.com/docs/langgraph)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -71,7 +72,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 4,
|
||||
"id": "3a566a30-cf0e-4330-ad4d-9bf994bdfa86",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -120,7 +121,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 5,
|
||||
"id": "94b3945f-ef0f-458d-a443-f763903550b0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -157,14 +158,21 @@
|
||||
"\n",
|
||||
"Each `edge` will choose which `node` to call next.\n",
|
||||
"\n",
|
||||
"It will follow the graph diagram shown above.\n",
|
||||
"We can make some simplifications from the paper:\n",
|
||||
"\n",
|
||||
"* Let's skip the knowledge refinement phase as a first pass. This can be added back as a node, if desired. \n",
|
||||
"* If *any* document is irrelevant, let's opt to supplement retrieval with web search. \n",
|
||||
"* We'll use [Tavily Search](https://python.langchain.com/docs/integrations/tools/tavily_search) for web search.\n",
|
||||
"* Let's use query re-writing to optimize the query for web search.\n",
|
||||
"\n",
|
||||
"Here is our graph flow:\n",
|
||||
"\n",
|
||||
""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 7,
|
||||
"id": "efd639c5-82e2-45e6-a94a-6a4039646ef5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -174,7 +182,6 @@
|
||||
"from typing import Annotated, Sequence, TypedDict\n",
|
||||
"\n",
|
||||
"from langchain import hub\n",
|
||||
"from langchain.output_parsers import PydanticOutputParser\n",
|
||||
"from langchain.output_parsers.openai_tools import PydanticToolsParser\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain.schema import Document\n",
|
||||
@@ -186,7 +193,6 @@
|
||||
"from langchain_core.runnables import RunnablePassthrough\n",
|
||||
"from langchain_core.utils.function_calling import convert_to_openai_tool\n",
|
||||
"from langchain_openai import ChatOpenAI, OpenAIEmbeddings\n",
|
||||
"from langgraph.prebuilt import ToolInvocation\n",
|
||||
"\n",
|
||||
"### Nodes ###\n",
|
||||
"\n",
|
||||
@@ -415,12 +421,14 @@
|
||||
"id": "fa076e90-7132-4fcf-8507-db5990314c4f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Build Graph"
|
||||
"## Build Graph\n",
|
||||
"\n",
|
||||
"The just follows the flow we outlined in the figure above."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 8,
|
||||
"id": "dedae17a-98c6-474d-90a7-9234b7c8cea0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -459,36 +467,110 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 9,
|
||||
"id": "f5b7c2fe-1fc7-4b76-bf93-ba701a40aa6b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"---RETRIEVE---\n",
|
||||
"\"Node 'retrieve':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---CHECK RELEVANCE---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"\"Node 'grade_documents':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---DECIDE TO GENERATE---\n",
|
||||
"---DECISION: GENERATE---\n",
|
||||
"---GENERATE---\n",
|
||||
"\"Node 'generate':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"\"Node '__end__':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"('There are several types of memory in human brains, including sensory memory, '\n",
|
||||
" 'which retains impressions of sensory information for a few seconds after the '\n",
|
||||
" 'original stimuli have ended. Short-term memory is utilized for in-context '\n",
|
||||
" 'learning, while long-term memory allows the agent to retain and recall '\n",
|
||||
" 'information over extended periods by leveraging an external vector store and '\n",
|
||||
" 'fast retrieval. Additionally, agents can use tool use to call external APIs '\n",
|
||||
" 'for extra information that is missing from the model weights.')\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Run\n",
|
||||
"inputs = {\"keys\": {\"question\": \"Explain how the different types of agent memory work?\"}}\n",
|
||||
"for output in app.stream(inputs):\n",
|
||||
" for key, value in output.items():\n",
|
||||
" pprint.pprint(f\"Output from node '{key}':\")\n",
|
||||
" pprint.pprint(\"---\")\n",
|
||||
" pprint.pprint(value[\"keys\"], indent=2, width=80, depth=None)\n",
|
||||
" pprint.pprint(\"\\n---\\n\")"
|
||||
" # Node\n",
|
||||
" pprint.pprint(f\"Node '{key}':\")\n",
|
||||
" # Optional: print full state at each node\n",
|
||||
" # pprint.pprint(value[\"keys\"], indent=2, width=80, depth=None)\n",
|
||||
" pprint.pprint(\"\\n---\\n\")\n",
|
||||
"\n",
|
||||
"# Final generation\n",
|
||||
"pprint.pprint(value['keys']['generation'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 11,
|
||||
"id": "2bee03de-a32c-4bbe-b37a-a13bb825e4cb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"---RETRIEVE---\n",
|
||||
"\"Node 'retrieve':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---CHECK RELEVANCE---\n",
|
||||
"---GRADE: DOCUMENT NOT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT NOT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT NOT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT NOT RELEVANT---\n",
|
||||
"\"Node 'grade_documents':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---DECIDE TO GENERATE---\n",
|
||||
"---DECISION: TRANSFORM QUERY and RUN WEB SEARCH---\n",
|
||||
"---TRANSFORM QUERY---\n",
|
||||
"\"Node 'transform_query':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---WEB SEARCH---\n",
|
||||
"\"Node 'web_search':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---GENERATE---\n",
|
||||
"\"Node 'generate':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"\"Node '__end__':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"('The AlphaCodium paper uses a test-based, iterative approach for code '\n",
|
||||
" 'generation. It employs a multi-stage, code-oriented flow that addresses the '\n",
|
||||
" 'specific challenges of coding problems. Unlike traditional models, '\n",
|
||||
" 'AlphaCodium actively engages in problem self-reflection, reasoning, and '\n",
|
||||
" 'iterative code solution generation.')\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Correction for question not present in context\n",
|
||||
"inputs = {\"keys\": {\"question\": \"What is the approach taken in the AlphaCodium paper?\"}}\n",
|
||||
"inputs = {\"keys\": {\"question\": \"What is the approach for code generation taken in the AlphaCodium paper?\"}}\n",
|
||||
"for output in app.stream(inputs):\n",
|
||||
" for key, value in output.items():\n",
|
||||
" pprint.pprint(f\"Output from node '{key}':\")\n",
|
||||
" pprint.pprint(\"---\")\n",
|
||||
" pprint.pprint(value[\"keys\"], indent=2, width=80, depth=None)\n",
|
||||
" pprint.pprint(\"\\n---\\n\")"
|
||||
" # Node\n",
|
||||
" pprint.pprint(f\"Node '{key}':\")\n",
|
||||
" # Optional: print full state \n",
|
||||
" # pprint.pprint(value[\"keys\"], indent=2, width=80, depth=None)\n",
|
||||
" pprint.pprint(\"\\n---\\n\")\n",
|
||||
"\n",
|
||||
"# Final generation\n",
|
||||
"pprint.pprint(value['keys']['generation'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
"id": "92ddc4f4-f7bf-4e0e-b5a5-5abd8a008b21",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Self-Reflective RAG\n",
|
||||
"# Corrective RAG\n",
|
||||
"\n",
|
||||
"Self-reflection can enhance RAG, enabling correction of poor quality retrieval or generations.\n",
|
||||
"\n",
|
||||
@@ -37,7 +37,7 @@
|
||||
"\n",
|
||||
"Here we show how to implement self-reflective RAG using `Mistral` and `LangGraph`.\n",
|
||||
"\n",
|
||||
"In particular, we'll focus on the approach from one paper focused on Corrective RAG (CRAG) [here](https://arxiv.org/pdf/2401.15884.pdf).\n",
|
||||
"We'll focus on ideas from one paper, `Corrective RAG (CRAG)` [here](https://arxiv.org/pdf/2401.15884.pdf).\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
@@ -187,7 +187,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 53,
|
||||
"execution_count": 5,
|
||||
"id": "447d1333-082d-479a-a6fa-0ac0df78bb9d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -461,12 +461,14 @@
|
||||
"id": "6096626d-dfa5-48e0-8a24-3747b298bc67",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Build Graph"
|
||||
"## Build Graph\n",
|
||||
"\n",
|
||||
"The just follows the flow we outlined in the figure above."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 54,
|
||||
"execution_count": 6,
|
||||
"id": "0a63776c-f9cd-46ce-b8cf-95c066dc5b06",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -515,12 +517,48 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 13,
|
||||
"id": "3ab1d8df-a74e-4b48-a30b-e39bbfd5925a",
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
"outputs": [],
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"---RETRIEVE---\n",
|
||||
"\"Node 'retrieve':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---CHECK RELEVANCE---\n",
|
||||
"---GRADE: DOCUMENT NOT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"\"Node 'grade_documents':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---DECIDE TO GENERATE---\n",
|
||||
"---DECISION: TRANSFORM QUERY and RUN WEB SEARCH---\n",
|
||||
"---TRANSFORM QUERY---\n",
|
||||
"\"Node 'transform_query':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---WEB SEARCH---\n",
|
||||
"\"Node 'web_search':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---GENERATE---\n",
|
||||
"\"Node 'generate':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"\"Node '__end__':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"('Episodic memory stores specific events or experiences, making them unique to '\n",
|
||||
" 'each individual. Semantic memory, on the other hand, involves general '\n",
|
||||
" 'knowledge and facts that are not tied to personal experiences. Procedural '\n",
|
||||
" 'memory is responsible for learning and remembering sequences of actions, '\n",
|
||||
" \"such as riding a bike. These memory types contribute to an agent's learning \"\n",
|
||||
" 'and decision-making processes by allowing it to recall past experiences '\n",
|
||||
" '(episodic), understand and use information (semantic), and perform tasks '\n",
|
||||
" '(procedural).')\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Run\n",
|
||||
"inputs = {\n",
|
||||
@@ -531,10 +569,14 @@
|
||||
"}\n",
|
||||
"for output in app.stream(inputs):\n",
|
||||
" for key, value in output.items():\n",
|
||||
" pprint.pprint(f\"Output from node '{key}':\")\n",
|
||||
" pprint.pprint(\"---\")\n",
|
||||
" pprint.pprint(value[\"keys\"], indent=2, width=80, depth=None)\n",
|
||||
" pprint.pprint(\"\\n---\\n\")"
|
||||
" # Node\n",
|
||||
" pprint.pprint(f\"Node '{key}':\")\n",
|
||||
" # Optional: print full state at each node\n",
|
||||
" # pprint.pprint(value[\"keys\"], indent=2, width=80, depth=None)\n",
|
||||
" pprint.pprint(\"\\n---\\n\")\n",
|
||||
"\n",
|
||||
"# Final generation\n",
|
||||
"pprint.pprint(value['keys']['generation'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -547,10 +589,49 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 14,
|
||||
"id": "16ea2032-59c7-433d-aca4-2828a1239074",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"---RETRIEVE---\n",
|
||||
"\"Node 'retrieve':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---CHECK RELEVANCE---\n",
|
||||
"---GRADE: DOCUMENT NOT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT NOT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"\"Node 'grade_documents':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---DECIDE TO GENERATE---\n",
|
||||
"---DECISION: TRANSFORM QUERY and RUN WEB SEARCH---\n",
|
||||
"---TRANSFORM QUERY---\n",
|
||||
"\"Node 'transform_query':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---WEB SEARCH---\n",
|
||||
"\"Node 'web_search':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---GENERATE---\n",
|
||||
"\"Node 'generate':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"\"Node '__end__':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"('There are three types of agent memory in artificial intelligence systems: '\n",
|
||||
" 'sensory memory, short-term memory, and long-term memory. Sensory memory is '\n",
|
||||
" 'the learning embedding representations for raw inputs such as text, image or '\n",
|
||||
" 'other modalities. Short-term memory is in-context learning that is short and '\n",
|
||||
" 'finite, restricted by the finite context window length of Transformer. '\n",
|
||||
" 'Long-term memory is an external vector store that the agent can attend to at '\n",
|
||||
" 'query time, accessible via fast retrieval. The external memory can alleviate '\n",
|
||||
" 'the restriction of finite attention span by using approximate nearest '\n",
|
||||
" 'neighbors (ANN) algorithms such as maximum inner product search (MIPS).')\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Run\n",
|
||||
"inputs = {\n",
|
||||
@@ -561,10 +642,14 @@
|
||||
"}\n",
|
||||
"for output in app.stream(inputs):\n",
|
||||
" for key, value in output.items():\n",
|
||||
" pprint.pprint(f\"Output from node '{key}':\")\n",
|
||||
" pprint.pprint(\"---\")\n",
|
||||
" pprint.pprint(value[\"keys\"], indent=2, width=80, depth=None)\n",
|
||||
" pprint.pprint(\"\\n---\\n\")"
|
||||
" # Node\n",
|
||||
" pprint.pprint(f\"Node '{key}':\")\n",
|
||||
" # Optional: print full state at each node\n",
|
||||
" # pprint.pprint(value[\"keys\"], indent=2, width=80, depth=None)\n",
|
||||
" pprint.pprint(\"\\n---\\n\")\n",
|
||||
"\n",
|
||||
"# Final generation\n",
|
||||
"pprint.pprint(value['keys']['generation'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -22,9 +22,21 @@
|
||||
"source": [
|
||||
"# Self-RAG\n",
|
||||
"\n",
|
||||
"Self-RAG is a recent paper that introduces an interesting approach for active RAG. \n",
|
||||
"Self-reflection can enhance RAG, enabling correction of poor quality retrieval or generations.\n",
|
||||
"\n",
|
||||
"The framework trains a single arbitrary LM (LLaMA2-7b, 13b) to generate tokens that govern the RAG process:\n",
|
||||
"Several recent papers focus on this theme, but implementing the ideas can be tricky.\n",
|
||||
"\n",
|
||||
"Here we show how to implement ideas from the `Self RAG` paper [here](https://arxiv.org/abs/2310.11511) using LangGraph.\n",
|
||||
"\n",
|
||||
"## Dependencies\n",
|
||||
"\n",
|
||||
"Set `OPENAI_API_KEY`\n",
|
||||
"\n",
|
||||
"## Self-RAG Detail\n",
|
||||
"\n",
|
||||
"Self-RAG is a recent paper that introduces an interesting approach for self-reflective RAG. \n",
|
||||
"\n",
|
||||
"The framework trains an LLM (e.g., LLaMA2-7b or 13b) to generate tokens that govern the RAG process in a few ways:\n",
|
||||
"\n",
|
||||
"1. Should I retrieve from retriever, `R` -\n",
|
||||
"\n",
|
||||
@@ -59,13 +71,9 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"Paper -\n",
|
||||
"\n",
|
||||
"https://arxiv.org/abs/2310.11511\n",
|
||||
"\n",
|
||||
"---\n",
|
||||
"\n",
|
||||
"Let's implement this from scratch using [LangGraph](https://python.langchain.com/docs/langgraph)."
|
||||
"Let's implement some of these ideas from scratch using [LangGraph](https://python.langchain.com/docs/langgraph)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -80,7 +88,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"id": "565a6d44-2c9f-4fff-b1ec-eea05df9350d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -129,7 +137,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 2,
|
||||
"id": "f1617e9e-66a8-4c1a-a1fe-cc936284c085",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -166,14 +174,16 @@
|
||||
"\n",
|
||||
"Each `edge` will choose which `node` to call next.\n",
|
||||
"\n",
|
||||
"We can lay out `self-RAG` as a graph:\n",
|
||||
"We can lay out `self-RAG` as a graph.\n",
|
||||
"\n",
|
||||
"Here is our graph flow:\n",
|
||||
"\n",
|
||||
""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 3,
|
||||
"id": "add509d8-6682-4127-8d95-13dd37d79702",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -183,7 +193,6 @@
|
||||
"from typing import Annotated, Sequence, TypedDict\n",
|
||||
"\n",
|
||||
"from langchain import hub\n",
|
||||
"from langchain.output_parsers import PydanticOutputParser\n",
|
||||
"from langchain.output_parsers.openai_tools import PydanticToolsParser\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain_community.vectorstores import Chroma\n",
|
||||
@@ -193,7 +202,6 @@
|
||||
"from langchain_core.runnables import RunnablePassthrough\n",
|
||||
"from langchain_core.utils.function_calling import convert_to_openai_tool\n",
|
||||
"from langchain_openai import ChatOpenAI, OpenAIEmbeddings\n",
|
||||
"from langgraph.prebuilt import ToolInvocation\n",
|
||||
"\n",
|
||||
"### Nodes ###\n",
|
||||
"\n",
|
||||
@@ -538,12 +546,14 @@
|
||||
"id": "61cd5797-1782-4d78-a277-8196d13f3e1b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Build Graph"
|
||||
"## Build Graph\n",
|
||||
"\n",
|
||||
"The just follows the flow we outlined in the figure above."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 5,
|
||||
"id": "0e09ca9f-e36d-4ef4-a0d5-79fdbada9fe0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -596,35 +606,118 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 6,
|
||||
"id": "fb69dbb9-91ee-4868-8c3c-93af3cd885be",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"---RETRIEVE---\n",
|
||||
"\"Node 'retrieve':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---CHECK RELEVANCE---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"\"Node 'grade_documents':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---DECIDE TO GENERATE---\n",
|
||||
"---DECISION: GENERATE---\n",
|
||||
"---GENERATE---\n",
|
||||
"\"Node 'generate':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---GRADE GENERATION vs DOCUMENTS---\n",
|
||||
"---DECISION: SUPPORTED, MOVE TO FINAL GRADE---\n",
|
||||
"---FINAL GRADE---\n",
|
||||
"\"Node 'prepare_for_final_grade':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---GRADE GENERATION vs QUESTION---\n",
|
||||
"---DECISION: USEFUL---\n",
|
||||
"\"Node '__end__':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"('Short-term memory is the stage of memory that stores information that we are '\n",
|
||||
" 'currently aware of and needed to carry out complex cognitive tasks. It has a '\n",
|
||||
" 'limited capacity and lasts for a short duration. Long-term memory, on the '\n",
|
||||
" 'other hand, can store information for a long time and has unlimited storage '\n",
|
||||
" 'capacity. It includes explicit/declarative memory for facts and events, and '\n",
|
||||
" 'implicit/procedural memory for unconscious skills and routines.')\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Run\n",
|
||||
"inputs = {\"keys\": {\"question\": \"Explain how the different types of agent memory work?\"}}\n",
|
||||
"for output in app.stream(inputs):\n",
|
||||
" for key, value in output.items():\n",
|
||||
" pprint.pprint(f\"Output from node '{key}':\")\n",
|
||||
" pprint.pprint(\"---\")\n",
|
||||
" pprint.pprint(value[\"keys\"], indent=2, width=80, depth=None)\n",
|
||||
" pprint.pprint(\"\\n---\\n\")"
|
||||
" # Node\n",
|
||||
" pprint.pprint(f\"Node '{key}':\")\n",
|
||||
" # Optional: print full state at each node\n",
|
||||
" # pprint.pprint(value[\"keys\"], indent=2, width=80, depth=None)\n",
|
||||
" pprint.pprint(\"\\n---\\n\")\n",
|
||||
"\n",
|
||||
"# Final generation\n",
|
||||
"pprint.pprint(value['keys']['generation'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 7,
|
||||
"id": "4138bc51-8c84-4b8a-8d24-f7f470721f6f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"---RETRIEVE---\n",
|
||||
"\"Node 'retrieve':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---CHECK RELEVANCE---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"\"Node 'grade_documents':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---DECIDE TO GENERATE---\n",
|
||||
"---DECISION: GENERATE---\n",
|
||||
"---GENERATE---\n",
|
||||
"\"Node 'generate':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---GRADE GENERATION vs DOCUMENTS---\n",
|
||||
"---DECISION: SUPPORTED, MOVE TO FINAL GRADE---\n",
|
||||
"---FINAL GRADE---\n",
|
||||
"\"Node 'prepare_for_final_grade':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---GRADE GENERATION vs QUESTION---\n",
|
||||
"---DECISION: USEFUL---\n",
|
||||
"\"Node '__end__':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"('Chain of thought prompting involves guiding the behavior of autoregressive '\n",
|
||||
" 'language models by providing prompts or demonstrations that contain '\n",
|
||||
" 'high-quality reasoning chains. This can be done through methods such as '\n",
|
||||
" 'self-asking, interleaving retrieval with chain-of-thought reasoning, and '\n",
|
||||
" 'complexity-based prompting for multi-step reasoning. These techniques aim to '\n",
|
||||
" \"improve the model's ability to generate coherent and logical responses \"\n",
|
||||
" 'without updating its weights.')\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"inputs = {\"keys\": {\"question\": \"Explain how chain of thought prompting works?\"}}\n",
|
||||
"for output in app.stream(inputs):\n",
|
||||
" for key, value in output.items():\n",
|
||||
" pprint.pprint(f\"Output from node '{key}':\")\n",
|
||||
" pprint.pprint(\"---\")\n",
|
||||
" pprint.pprint(value[\"keys\"], indent=2, width=80, depth=None)\n",
|
||||
" pprint.pprint(\"\\n---\\n\")"
|
||||
" # Node\n",
|
||||
" pprint.pprint(f\"Node '{key}':\")\n",
|
||||
" # Optional: print full state at each node\n",
|
||||
" # pprint.pprint(value[\"keys\"], indent=2, width=80, depth=None)\n",
|
||||
" pprint.pprint(\"\\n---\\n\")\n",
|
||||
"\n",
|
||||
"# Final generation\n",
|
||||
"pprint.pprint(value['keys']['generation'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user