LLMs are trained on a large but fixed corpus of data, limiting their ability to reason about private or recent information. Fine-tuning is one way to mitigate this, but is often not well-suited for factual recall and can be costly.
Retrieval Augmented Generation (RAG) has emerged as a popular and powerful mechanism to expand an LLM's knowledge base, using documents retrieved from an external data source to ground the LLM generation via in-context learning.
This repository contains comprehensive notebooks that build up an understanding of RAG from scratch, covering the complete RAG landscape including indexing, retrieval, query transformations, routing, and advanced techniques.
This repository accompanies the excellent "RAG From Scratch" video series by LangChain:
Foundation & Core Concepts
This notebook introduces the fundamental building blocks of RAG systems:
- Part 1: Overview & Environment Setup
- Part 2: Indexing - Document loading, text splitting, and vector embeddings
- Part 3: Retrieval - Semantic search and document retrieval from vector stores
- Part 4: Generation - Combining retrieved context with LLM prompts to generate answers
Key Topics: Document indexing, vector embeddings, ChromaDB, basic RAG pipeline, retrieval-augmented generation
Advanced Query Processing Techniques
Explore sophisticated methods for transforming and enhancing user queries:
- Part 5: Multi-Query - Generate multiple perspectives of a single query for better retrieval
- Part 6: RAG-Fusion - Combine results from multiple query variations with reciprocal rank fusion
- Part 7: Decomposition - Break complex questions into simpler sub-questions
- Part 8: Step-Back Prompting - Generate broader queries to retrieve higher-level context
- Part 9: HyDE (Hypothetical Document Embeddings) - Generate hypothetical answers to improve retrieval
Key Topics: Query rewriting, multi-query retrieval, query decomposition, RAG-fusion, step-back prompting, HyDE
Intelligent Query Routing
Learn how to route queries intelligently to different data sources or processing pipelines:
- Part 10: Logical Routing - Use function-calling to classify and route queries logically
- Part 11: Semantic Routing - Route queries based on semantic similarity to data sources
Key Topics: Query routing, logical routing, semantic routing, function calling, multi-index routing
Advanced Indexing Strategies
Deep dive into sophisticated indexing techniques for better retrieval:
- Part 12: Multi-Representation Indexing - Index document summaries while retrieving full documents
- Part 13: RAPTOR - Recursively cluster and summarize documents for hierarchical retrieval
- Part 14: ColBERT - Late interaction models for token-level similarity matching
Key Topics: Multi-representation indexing, document summarization, hierarchical indexing, RAPTOR, ColBERT, late interaction retrieval
Advanced Retrieval Methods
Master advanced retrieval techniques to improve answer quality:
- Part 15: Re-Ranking - Use cross-encoders to re-rank retrieved documents (e.g., Cohere Rerank)
- Part 16: Compression - Filter and compress retrieved context to focus on relevant information
- Part 17: Contextual Compression - Combine compression with base retriever
- Part 18: Fusion Retrieval - Merge results from multiple retrieval strategies
Key Topics: Re-ranking, cross-encoders, contextual compression, result fusion, Cohere rerank
pip install langchain langchain_community langchain-openai
pip install tiktoken chromadb langchainhub
pip install youtube-transcript-api pytube cohereYou'll need to set up the following:
-
LangSmith (optional, for tracing):
- Sign up at smith.langchain.com
- Set environment variables:
LANGCHAIN_TRACING_V2,LANGCHAIN_ENDPOINT,LANGCHAIN_API_KEY
-
API Keys:
- OpenAI API key:
OPENAI_API_KEY - Cohere API key (for Parts 15-18):
COHERE_API_KEY
- OpenAI API key:
Recommended Order:
- Start with Parts 1-4 to understand RAG fundamentals
- Move to Parts 5-9 to learn query transformation techniques
- Study Parts 10-11 for intelligent routing strategies
- Explore Parts 12-14 for advanced indexing methods
- Complete with Parts 15-18 to master retrieval optimization
Feel free to open issues or submit pull requests for improvements!
This project follows the original repository's licensing.
- LangChain team for the excellent video series and educational content
- Original repository: langchain-ai/rag-from-scratch
