Merge pull request #90 from langchain-ai/rlm/crag_mistral

Add Mistral CRAG, update formatting / documentation
This commit is contained in:
Lance Martin
2024-02-07 16:49:16 -08:00
committed by GitHub
3 changed files with 976 additions and 109 deletions
+145 -63
View File
@@ -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",
"![Screenshot 2024-02-04 at 2.50.32 PM.png](attachment:5bfa38a2-78a1-4e99-80a2-d98c8a440ea2.png)\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": [],
@@ -132,12 +133,10 @@
"\n",
"class GraphState(TypedDict):\n",
" \"\"\"\n",
" Represents the state of an agent in the conversation.\n",
" Represents the state of our graph.\n",
"\n",
" Attributes:\n",
" keys: A dictionary where each key is a string and the value is expected to be a list or another structure\n",
" that supports addition with `operator.add`. This could be used, for instance, to accumulate messages\n",
" or other pieces of data throughout the graph.\n",
" keys: A dictionary where each key is a string.\n",
" \"\"\"\n",
"\n",
" keys: Dict[str, any]"
@@ -159,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",
"![Screenshot 2024-02-04 at 1.32.52 PM.png](attachment:3b65f495-5fc4-497b-83e2-73844a97f6cc.png)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "efd639c5-82e2-45e6-a94a-6a4039646ef5",
"metadata": {},
"outputs": [],
@@ -176,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",
@@ -188,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",
@@ -198,10 +202,10 @@
" Retrieve documents\n",
"\n",
" Args:\n",
" state (dict): The current state of the agent, including all keys.\n",
" state (dict): The current graph state\n",
"\n",
" Returns:\n",
" dict: New key added to state, documents, that contains documents.\n",
" state (dict): New key added to state, documents, that contains retrieved documents\n",
" \"\"\"\n",
" print(\"---RETRIEVE---\")\n",
" state_dict = state[\"keys\"]\n",
@@ -215,10 +219,10 @@
" Generate answer\n",
"\n",
" Args:\n",
" state (dict): The current state of the agent, including all keys.\n",
" state (dict): The current graph state\n",
"\n",
" Returns:\n",
" dict: New key added to state, generation, that contains generation.\n",
" state (dict): New key added to state, generation, that contains LLM generation\n",
" \"\"\"\n",
" print(\"---GENERATE---\")\n",
" state_dict = state[\"keys\"]\n",
@@ -250,10 +254,10 @@
" Determines whether the retrieved documents are relevant to the question.\n",
"\n",
" Args:\n",
" state (dict): The current state of the agent, including all keys.\n",
" state (dict): The current graph state\n",
"\n",
" Returns:\n",
" dict: New key added to state, filtered_documents, that contains relevant documents.\n",
" state (dict): Updates documents key with relevant documents\n",
" \"\"\"\n",
"\n",
" print(\"---CHECK RELEVANCE---\")\n",
@@ -323,10 +327,10 @@
" Transform the query to produce a better question.\n",
"\n",
" Args:\n",
" state (dict): The current state of the agent, including all keys.\n",
" state (dict): The current graph state\n",
"\n",
" Returns:\n",
" dict: New value saved to question.\n",
" state (dict): Updates question key with a re-phrased question\n",
" \"\"\"\n",
"\n",
" print(\"---TRANSFORM QUERY---\")\n",
@@ -358,13 +362,13 @@
"\n",
"def web_search(state):\n",
" \"\"\"\n",
" Web search using Tavily.\n",
" Web search based on the re-phrased question using Tavily API.\n",
"\n",
" Args:\n",
" state (dict): The current state of the agent, including all keys.\n",
" state (dict): The current graph state\n",
"\n",
" Returns:\n",
" state (dict): Web results appended to documents.\n",
" state (dict): Updates documents key with appended web results\n",
" \"\"\"\n",
"\n",
" print(\"---WEB SEARCH---\")\n",
@@ -386,13 +390,13 @@
"\n",
"def decide_to_generate(state):\n",
" \"\"\"\n",
" Determines whether to generate an answer, or re-generate a question.\n",
" Determines whether to generate an answer or re-generate a question for web search.\n",
"\n",
" Args:\n",
" state (dict): The current state of the agent, including all keys.\n",
"\n",
" Returns:\n",
" dict: New key added to state, filtered_documents, that contains relevant documents.\n",
" str: Next node to call\n",
" \"\"\"\n",
"\n",
" print(\"---DECIDE TO GENERATE---\")\n",
@@ -412,9 +416,19 @@
" return \"generate\""
]
},
{
"cell_type": "markdown",
"id": "fa076e90-7132-4fcf-8507-db5990314c4f",
"metadata": {},
"source": [
"## 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": [],
@@ -453,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'])"
]
},
{
@@ -490,18 +578,12 @@
"id": "a7e44593-1959-4abf-8405-5e23aa9398f5",
"metadata": {},
"source": [
"Traces -\n",
"LangSmith Traces - \n",
" \n",
"[Trace](https://smith.langchain.com/public/7e0b9569-abfe-4337-b34b-842b1f93df63/r) and [Trace](https://smith.langchain.com/public/b40c5813-7caf-4cc8-b279-ee66060b2040/r)"
"* https://smith.langchain.com/public/7e0b9569-abfe-4337-b34b-842b1f93df63/r\n",
"\n",
"* https://smith.langchain.com/public/b40c5813-7caf-4cc8-b279-ee66060b2040/r"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "69eddb3e-57f4-4eea-8e40-4822fc50c729",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
File diff suppressed because one or more lines are too long
+138 -46
View File
@@ -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",
"![Screenshot 2024-02-02 at 1.36.44 PM.png](attachment:ea6a57d2-f2ec-4061-840a-98deb3207248.png)\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": [],
@@ -141,12 +149,10 @@
"\n",
"class GraphState(TypedDict):\n",
" \"\"\"\n",
" Represents the state of an agent in the conversation.\n",
" Represents the state of our graph.\n",
"\n",
" Attributes:\n",
" keys: A dictionary where each key is a string and the value is expected to be a list or another structure\n",
" that supports addition with `operator.add`. This could be used, for instance, to accumulate messages\n",
" or other pieces of data throughout the graph.\n",
" keys: A dictionary where each key is a string.\n",
" \"\"\"\n",
"\n",
" keys: Dict[str, any]"
@@ -168,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",
"![Screenshot 2024-02-02 at 9.01.01 PM.png](attachment:e61fbd0c-e667-4160-a96c-82f95a560b44.png)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "add509d8-6682-4127-8d95-13dd37d79702",
"metadata": {},
"outputs": [],
@@ -185,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",
@@ -195,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",
@@ -205,10 +211,10 @@
" Retrieve documents\n",
"\n",
" Args:\n",
" state (dict): The current state of the agent, including all keys.\n",
" state (dict): The current graph state\n",
"\n",
" Returns:\n",
" dict: New key added to state, documents, that contains documents.\n",
" state (dict): New key added to state, documents, that contains retrieved documents\n",
" \"\"\"\n",
" print(\"---RETRIEVE---\")\n",
" state_dict = state[\"keys\"]\n",
@@ -222,10 +228,10 @@
" Generate answer\n",
"\n",
" Args:\n",
" state (dict): The current state of the agent, including all keys.\n",
" state (dict): The current graph state\n",
"\n",
" Returns:\n",
" dict: New key added to state, generation, that contains generation.\n",
" state (dict): New key added to state, generation, that contains LLM generation\n",
" \"\"\"\n",
" print(\"---GENERATE---\")\n",
" state_dict = state[\"keys\"]\n",
@@ -257,10 +263,10 @@
" Determines whether the retrieved documents are relevant to the question.\n",
"\n",
" Args:\n",
" state (dict): The current state of the agent, including all keys.\n",
" state (dict): The current graph state\n",
"\n",
" Returns:\n",
" dict: New key added to state, filtered_documents, that contains relevant documents.\n",
" state (dict): Updates documents key with relevant documents\n",
" \"\"\"\n",
"\n",
" print(\"---CHECK RELEVANCE---\")\n",
@@ -322,10 +328,10 @@
" Transform the query to produce a better question.\n",
"\n",
" Args:\n",
" state (dict): The current state of the agent, including all keys.\n",
" state (dict): The current graph state\n",
"\n",
" Returns:\n",
" dict: New value saved to question.\n",
" state (dict): Updates question key with a re-phrased question\n",
" \"\"\"\n",
"\n",
" print(\"---TRANSFORM QUERY---\")\n",
@@ -357,13 +363,13 @@
"\n",
"def prepare_for_final_grade(state):\n",
" \"\"\"\n",
" Stage for final grade, passthrough state.\n",
" Passthrough state for final grade.\n",
"\n",
" Args:\n",
" state (dict): The current state of the agent, including all keys.\n",
" state (dict): The current graph state\n",
"\n",
" Returns:\n",
" state (dict): The current state of the agent, including all keys.\n",
" state (dict): The current graph state\n",
" \"\"\"\n",
"\n",
" print(\"---FINAL GRADE---\")\n",
@@ -388,7 +394,7 @@
" state (dict): The current state of the agent, including all keys.\n",
"\n",
" Returns:\n",
" dict: New key added to state, filtered_documents, that contains relevant documents.\n",
" str: Next node to call\n",
" \"\"\"\n",
"\n",
" print(\"---DECIDE TO GENERATE---\")\n",
@@ -415,7 +421,7 @@
" state (dict): The current state of the agent, including all keys.\n",
"\n",
" Returns:\n",
" str: Binary decision score.\n",
" str: Binary decision\n",
" \"\"\"\n",
"\n",
" print(\"---GRADE GENERATION vs DOCUMENTS---\")\n",
@@ -479,7 +485,7 @@
" state (dict): The current state of the agent, including all keys.\n",
"\n",
" Returns:\n",
" str: Binary decision score.\n",
" str: Binary decision\n",
" \"\"\"\n",
"\n",
" print(\"---GRADE GENERATION vs QUESTION---\")\n",
@@ -540,12 +546,14 @@
"id": "61cd5797-1782-4d78-a277-8196d13f3e1b",
"metadata": {},
"source": [
"## 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": [],
@@ -598,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'])"
]
},
{
@@ -634,9 +725,10 @@
"id": "548f1c5b-4108-4aae-8abb-ec171b511b92",
"metadata": {},
"source": [
"Trace - \n",
"LangSmith Traces - \n",
" \n",
"* https://smith.langchain.com/public/55d6180f-aab8-42bc-8799-dadce6247d9b/r\n",
"\n",
"* https://smith.langchain.com/public/f85ebc95-81d9-47fc-91c6-b54e5b78f359/r"
]
}