Skip to content

This repository contains a series of lessons demonstrating advanced Retrieval-Augmented Generation (RAG) techniques using LangChain. Each lesson focuses on a specific approach to improve retrieval quality and efficiency.

License

Notifications You must be signed in to change notification settings

extrawest/advanced_rag_techniques

Folders and files

NameName
Last commit message
Last commit date

Latest commit

author
yevhen-ruban-ew
Apr 25, 2025
d852cfe ยท Apr 25, 2025

History

5 Commits
Apr 24, 2025
Apr 24, 2025
Apr 24, 2025
Apr 25, 2025
Apr 24, 2025

Repository files navigation

๐Ÿš€ LangChain RAG Tips and Tricks

Maintenance Maintainer Ask Me Anything ! License GitHub release

This repository contains a series of lessons demonstrating advanced Retrieval-Augmented Generation (RAG) techniques using LangChain. Each lesson focuses on a specific approach to improve retrieval quality and efficiency.

๐Ÿ“š Lessons Overview

Lesson 1: Self-Query Retriever

This lesson demonstrates how to use the SelfQueryRetriever to automatically convert natural language queries into semantic searches and metadata filters.

Key Components:

  • ๐Ÿง  SelfQueryRetriever for intelligent query parsing
  • ๐Ÿท๏ธ Metadata field definitions for structured filtering
  • ๐Ÿ” Vector search with metadata constraints
  • ๐Ÿ”ข Result limiting and pagination

What You'll Learn:

  • Creating and structuring a vector store with metadata
  • Setting up metadata field definitions for intelligent filtering
  • Configuring and using SelfQueryRetriever for semantic search
  • Combining vector search with metadata filtering
  • Limiting query results using the limit parameter

Lesson 2: Parent Document Retriever

This lesson explores the ParentDocumentRetriever which allows searching on small chunks but returning whole documents or larger chunks.

Key Components:

  • ๐Ÿ“„ ParentDocumentRetriever for hierarchical document access
  • ๐Ÿ“ฆ Dual storage for small and large document fragments
  • ๐Ÿ”„ Small-to-large chunk mapping system
  • ๐Ÿค– Integration with LLM for complete QA workflow

What You'll Learn:

  • Creating a system that allows searching documents by small fragments
  • Setting up storage for small and large document fragments
  • Two modes of ParentDocumentRetriever operation:
    • Returning full documents based on small chunk matches
    • Returning larger chunks based on small chunk matches
  • Integrating the retriever with an LLM for a complete QA system

Lesson 3: Hybrid Search (BM25 + Ensemble Retrieval)

This lesson demonstrates creating a hybrid search system that combines sparse (BM25) and dense (FAISS) vector representations using an EnsembleRetriever.

Key Components:

  • ๐Ÿ“š BM25 retriever for keyword-based search
  • ๐Ÿงฎ FAISS vector retriever for semantic search
  • ๐Ÿ”„ EnsembleRetriever for combining search strategies
  • โš–๏ธ Result weighting and score normalization
  • ๐Ÿ“Š Comparative performance analysis

What You'll Learn:

  • Creating a BM25 retriever for efficient keyword search
  • Creating a dense vector FAISS retriever for semantic search
  • Combining both retrievers with EnsembleRetriever
  • Weighting results for optimal hybrid search performance
  • Comparing the effectiveness of different search approaches

Lesson 4: Contextual Compression and Filtering

This lesson focuses on improving the quality of context for LLMs through contextual compression and filtering to retrieve only relevant information.

Key Components:

  • ๐Ÿ“‘ SentenceWindowNodeParser for context-aware text chunking
  • ๐Ÿ”„ MetadataReplacementPostProcessor for enhancing retrieval context
  • ๐Ÿ† SentenceTransformerRerank for improving result relevance
  • ๐Ÿ“Š Comparative analysis of different window sizes

What You'll Learn:

  • How sentence window parsing creates contextual overlaps between chunks
  • How to configure optimal window sizes for different content types
  • How reranking can enhance the quality of retrieved passages
  • How metadata replacement provides more complete context
  • How to compare different window size configurations (1 vs 3)
  • The impact of context window size on RAG performance metrics

Lesson 5: Hypothetical Document Embeddings (HyDE)

This lesson explores the Hypothetical Document Embeddings (HyDE) technique which improves search by creating hypothetical answers to questions.

Key Components:

  • ๐Ÿ’ญ LLM-generated hypothetical documents
  • ๐Ÿ”„ Query-to-document transformation
  • ๐Ÿงฉ Multiple generation variants for robustness
  • ๐Ÿ“Š Comparative evaluation against standard search
  • ๐Ÿง  BGE embedding model integration

What You'll Learn:

  • Creating hypothetical documents using an LLM
  • Embedding these hypothetical documents instead of direct queries
  • Working with different HyDE implementation variants:
    • Basic HyDE with predefined templates
    • HyDE with multiple generations for improved results
    • HyDE with custom prompts for specific tasks
  • Comparing HyDE search with standard vector search
  • Working with BGE embeddings for improved result quality

Lesson 6: RAGFusion

This lesson demonstrates the RAGFusion technique which generates multiple search queries from a single user query and combines the results.

Key Components:

  • ๐Ÿ”„ Query generation for diverse search angles
  • ๐Ÿ“š Multi-query document retrieval system
  • ๐Ÿ“Š Reciprocal Rank Fusion for result combination
  • โšก Asynchronous execution pipeline
  • ๐Ÿง  OpenAI and BGE embedding model support

What You'll Learn:

  • Generating multiple search queries from a single user query
  • Retrieving documents for each generated query
  • Combining results using the Reciprocal Rank Fusion algorithm
  • Setting up and using OpenAI and BGE embeddings
  • Comparing standard RAG and RAGFusion
  • Asynchronous query execution for improved performance
  • Serializing and deserializing documents for efficient ranking

๐Ÿ› ๏ธ Requirements

The code in these lessons uses various components from:

  • langchain_core
  • langchain_community
  • langchain_openai
  • langchain_text_splitters
  • FAISS
  • Chroma
  • HuggingFace models for embeddings

๐Ÿš€ Installation

# Install required packages
pip install -r requirements.txt

# Set up your OpenAI API key
# Either create a .env file with OPENAI_API_KEY=your-api-key
# Or export it in your shell:
export OPENAI_API_KEY=your-api-key

About

This repository contains a series of lessons demonstrating advanced Retrieval-Augmented Generation (RAG) techniques using LangChain. Each lesson focuses on a specific approach to improve retrieval quality and efficiency.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages