Merge pull request #259 from langchain-ai/rlm/clean-rag-ntbks

Clean and normalize all langgraph RAG ntbks
This commit is contained in:
Lance Martin
2024-04-01 14:59:37 -07:00
committed by GitHub
8 changed files with 3141 additions and 2181 deletions
@@ -22,7 +22,9 @@
"* Single-shot RAG\n",
"* Iterative RAG\n",
"\n",
"Let's build on this to perform query analysis to route across some more interesting cases:\n",
"Let's build on this using LangGraph. \n",
"\n",
"In our implementation, we will route between:\n",
"\n",
"* Web search: for questions related to recent events\n",
"* Self-corrective RAG: for questions related to our index\n",
@@ -45,7 +47,7 @@
"metadata": {},
"outputs": [],
"source": [
"! pip install langchain_community tiktoken langchain-openai langchain-cohere langchainhub chromadb langchain langgraph"
"! pip install langchain_community tiktoken langchain-openai langchain-cohere langchainhub chromadb langchain langgraph tavily-python"
]
},
{
@@ -58,7 +60,18 @@
"### LLMs\n",
"import os\n",
"os.environ['OPENAI_API_KEY'] = <your-api-key>\n",
"os.environ['COHERE_API_KEY'] = <your-api-key>"
"os.environ['COHERE_API_KEY'] = <your-api-key>\n",
"os.environ['TAVILY_API_KEY'] = <your-api-key>"
]
},
{
"cell_type": "markdown",
"id": "018c9e9f-8199-4f33-b5b4-7adfb66d6219",
"metadata": {},
"source": [
"### Tracing\n",
"\n",
"* Optionally, use [LangSmith](https://docs.smith.langchain.com/) for tracing (shown at bottom) by setting: "
]
},
{
@@ -157,7 +170,6 @@
"from langchain_core.prompts import ChatPromptTemplate\n",
"from langchain_core.pydantic_v1 import BaseModel, Field\n",
"from langchain_openai import ChatOpenAI\n",
"### from langchain_cohere import ChatCohere\n",
"\n",
"# Data model\n",
"class RouteQuery(BaseModel):\n",
@@ -218,6 +230,7 @@
"# Prompt \n",
"system = \"\"\"You are a grader assessing relevance of a retrieved document to a user question. \\n \n",
" If the document contains keyword(s) or semantic meaning related to the user question, grade it as relevant. \\n\n",
" It does not need to be a stringent test. The goal is to filter out erroneous retrievals. \\n\n",
" Give a binary score 'yes' or 'no' score to indicate whether the document is relevant to the question.\"\"\"\n",
"grade_prompt = ChatPromptTemplate.from_messages(\n",
" [\n",
@@ -440,7 +453,7 @@
"from typing import List\n",
"\n",
"class GraphState(TypedDict):\n",
" \"\"\"|\n",
" \"\"\"\n",
" Represents the state of our graph.\n",
"\n",
" Attributes:\n",
@@ -673,8 +686,6 @@
"metadata": {},
"outputs": [],
"source": [
"from pprint import pprint\n",
"\n",
"from langgraph.graph import END, StateGraph\n",
"\n",
"workflow = StateGraph(GraphState)\n",
@@ -751,6 +762,8 @@
}
],
"source": [
"from pprint import pprint\n",
"\n",
"# Run \n",
"inputs = {\"question\": \"What player at the Bears expected to draft first in the 2024 NFL draft?\"}\n",
"for output in app.stream(inputs):\n",
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long