Skip to content

gianlucamazza/langchain-rag-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

28 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

LangChain RAG Tutorial

Python LangChain License OpenAI Docker Tests

A comprehensive, production-ready tutorial for building Retrieval-Augmented Generation (RAG) systems using LangChain.

🎯 Features: 15 advanced RAG architectures | Multimodal RAG (images + text) | Fine-tuning embeddings | RAGAS evaluation | SQL & Graph support | Docker & production templates | Complete testing & CI/CD

πŸš€ Quick Start

# Clone repository
git clone https://github.com/gianlucamazza/langchain-rag-tutorial.git
cd langchain-rag-tutorial

# Setup environment
python3 -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate
pip install -r requirements.txt

# Configure API key
echo "OPENAI_API_KEY=sk-proj-your-key-here" > .env

# Start learning
jupyter notebook notebooks/00_index.ipynb

πŸ“– Full guide: docs/GETTING_STARTED.md

πŸ“š What You'll Learn

Fundamentals (30-40 min)

Master the core concepts of RAG:

  • Document Loading & Splitting - Process and chunk text efficiently
  • Embeddings Comparison - OpenAI vs HuggingFace benchmarks
  • Simple RAG - Build your first end-to-end RAG system

πŸ“˜ Start with Fundamentals β†’

Advanced Architectures (4-5 hours)

Explore 15 production-ready patterns:

Architecture Complexity Use Case Key Feature
Memory RAG ⭐⭐ Chatbots Conversation history
Branched RAG ⭐⭐⭐ Research Multi-query parallel retrieval
HyDe ⭐⭐⭐ Ambiguous queries Hypothetical documents
Adaptive RAG ⭐⭐⭐⭐ Mixed workloads Intelligent query routing
Corrective RAG ⭐⭐⭐⭐ High accuracy Quality check + web fallback
Self-RAG ⭐⭐⭐⭐⭐ Self-correcting Autonomous refinement
Agentic RAG ⭐⭐⭐⭐⭐ Complex reasoning Multi-tool agent loops
Contextual RAG ✨ ⭐⭐⭐ Technical docs Context-augmented chunks
Fusion RAG ✨ ⭐⭐⭐ Best ranking Reciprocal Rank Fusion
SQL RAG ✨ ⭐⭐⭐⭐ Analytics/BI Natural Language to SQL
GraphRAG ✨ ⭐⭐⭐⭐⭐ Knowledge graphs Entity relationships + multi-hop
Multimodal RAG πŸ†• ⭐⭐⭐⭐ Images + text GPT-4 Vision + OCR
Fine-tuning Guide πŸ†• ⭐⭐⭐⭐ Domain embeddings Custom embedding models
RAGAS Evaluation - Quality metrics Comprehensive RAG assessment

πŸ”¬ Explore Advanced Patterns β†’

πŸ“– Documentation

Comprehensive docs organized by topic:

πŸ—οΈ Project Structure

llm_rag/
β”œβ”€β”€ docs/                          # πŸ“– Modular documentation
β”‚   β”œβ”€β”€ GETTING_STARTED.md        # Quick start guide
β”‚   β”œβ”€β”€ INSTALLATION.md           # Setup instructions
β”‚   β”œβ”€β”€ API_REFERENCE.md          # Shared module API
β”‚   └── ... (8 more specialized docs)
β”œβ”€β”€ notebooks/
β”‚   β”œβ”€β”€ 00_index.ipynb            # 🎯 START HERE - Navigation hub
β”‚   β”œβ”€β”€ fundamentals/             # Core RAG concepts (01-03)
β”‚   β”‚   β”œβ”€β”€ 01_setup_and_basics.ipynb
β”‚   β”‚   β”œβ”€β”€ 02_embeddings_comparison.ipynb
β”‚   β”‚   └── 03_simple_rag.ipynb
β”‚   └── advanced_architectures/   # Advanced patterns (04-18)
β”‚       β”œβ”€β”€ 04_rag_with_memory.ipynb
β”‚       β”œβ”€β”€ 05_branched_rag.ipynb
β”‚       β”œβ”€β”€ 06_hyde.ipynb
β”‚       β”œβ”€β”€ 07_adaptive_rag.ipynb
β”‚       β”œβ”€β”€ 08_corrective_rag.ipynb
β”‚       β”œβ”€β”€ 09_self_rag.ipynb
β”‚       β”œβ”€β”€ 10_agentic_rag.ipynb
β”‚       β”œβ”€β”€ 11_comparison.ipynb           # All 12 architectures
β”‚       β”œβ”€β”€ 12_contextual_rag.ipynb ✨     # v1.1.0
β”‚       β”œβ”€β”€ 13_fusion_rag.ipynb ✨         # v1.1.0
β”‚       β”œβ”€β”€ 14_sql_rag.ipynb ✨            # v1.1.0
β”‚       β”œβ”€β”€ 15_graphrag.ipynb ✨           # v1.1.0
β”‚       β”œβ”€β”€ 16_evaluation_ragas.ipynb ✨   # v1.1.0 - Quality metrics
β”‚       β”œβ”€β”€ 17_multimodal_rag.ipynb πŸ†•    # v1.2.0 - Images + Text
β”‚       └── 18_finetuning_embeddings.ipynb πŸ†•  # v1.2.0 - Custom embeddings
β”œβ”€β”€ templates/                     # πŸš€ Production deployment templates (NEW v1.2.0)
β”‚   β”œβ”€β”€ fastapi/                  # REST API template
β”‚   β”œβ”€β”€ streamlit/                # Web UI template
β”‚   └── lambda/                   # AWS Lambda serverless
β”œβ”€β”€ tests/                        # πŸ§ͺ Test suite (NEW v1.2.0)
β”‚   β”œβ”€β”€ conftest.py              # pytest fixtures
β”‚   β”œβ”€β”€ test_utils.py            # Utility tests
β”‚   └── test_config.py           # Config tests
β”œβ”€β”€ shared/                        # πŸ”§ Reusable utilities (1500+ lines)
β”‚   β”œβ”€β”€ config.py                 # Configuration management
β”‚   β”œβ”€β”€ utils.py                  # Utility functions
β”‚   β”œβ”€β”€ loaders.py                # Document loading
β”‚   └── prompts.py                # Prompt templates (30+ prompts)
β”œβ”€β”€ data/                         # πŸ’Ύ Vector stores, Chinook DB (gitignored)
β”œβ”€β”€ Dockerfile                    # 🐳 Docker support (NEW v1.2.0)
β”œβ”€β”€ docker-compose.yml            # 🐳 Full stack orchestration (NEW v1.2.0)
β”œβ”€β”€ Makefile                      # πŸ› οΈ Development commands (NEW v1.2.0)
β”œβ”€β”€ pytest.ini                    # πŸ§ͺ Test configuration (NEW v1.2.0)
β”œβ”€β”€ .pre-commit-config.yaml       # πŸ” Pre-commit hooks (NEW v1.2.0)
β”œβ”€β”€ requirements.txt              # Production dependencies
β”œβ”€β”€ requirements-dev.txt          # Development dependencies (NEW v1.2.0)
β”œβ”€β”€ .env.example                  # πŸ”‘ API key template
└── README.md                     # This file

✨ Key Features

Core Capabilities:

  • βœ… 12 RAG Architectures - From simple to graph-based
  • βœ… Multimodal RAG πŸ†• - GPT-4 Vision + OCR for images + text
  • βœ… Fine-tuning Guide πŸ†• - Train custom domain-specific embeddings
  • βœ… RAGAS Evaluation - Comprehensive quality metrics
  • βœ… SQL & Graph Support - Structured data + relationships
  • βœ… Modular Design - Reusable shared utilities (DRY)
  • βœ… Vector Store Persistence - No re-embedding needed
  • βœ… Comprehensive Benchmarks - Performance & cost analysis

Production-Ready Infrastructure: πŸ†•

  • βœ… Docker Support - Multi-stage builds with Redis, Prometheus, Grafana
  • βœ… 3 Deployment Templates - FastAPI, Streamlit, AWS Lambda
  • βœ… Testing Suite - pytest with 70%+ coverage target
  • βœ… CI/CD Pipelines - GitHub Actions (testing, linting, coverage)
  • βœ… Development Tools - Makefile, pre-commit hooks, linting
  • βœ… Security Best Practices - Non-root Docker, API key management

Technical Stack:

  • LangChain v1.0+ - Framework & LCEL
  • OpenAI GPT-4o-mini + GPT-4 Vision - Fast LLM + multimodal
  • FAISS - Vector similarity search
  • Sentence Transformers + Accelerate - Fine-tuning embeddings
  • NetworkX - Graph algorithms
  • SQLAlchemy - Database abstraction
  • RAGAS - RAG evaluation framework
  • Tesseract OCR + Pillow - Image processing
  • FastAPI + Streamlit - Production deployment
  • Docker + Docker Compose - Containerization
  • pytest + GitHub Actions - Testing & CI/CD
  • Python 3.9+ - Modern type hints

πŸ” See Architecture Details β†’

πŸ’‘ Architecture Selection Guide

Choose based on your needs:

Your Need Architecture Docs
πŸš€ Fast & simple Simple RAG 03_simple_rag.ipynb
πŸ’¬ Chatbot with memory Memory RAG 04_rag_with_memory.ipynb
πŸ“š Research tool Fusion RAG 13_fusion_rag.ipynb ✨
πŸ” Ambiguous queries Contextual RAG 12_contextual_rag.ipynb ✨
βš–οΈ Cost optimization Adaptive RAG 07_adaptive_rag.ipynb
🎯 High accuracy Fusion / CRAG 13_fusion_rag.ipynb ✨
πŸ”„ Self-correcting Self-RAG 09_self_rag.ipynb
πŸ€– Complex reasoning Agentic RAG 10_agentic_rag.ipynb
πŸ“Š Analytics/BI SQL RAG 14_sql_rag.ipynb ✨
πŸ•ΈοΈ Knowledge graphs GraphRAG 15_graphrag.ipynb ✨
πŸ–ΌοΈ Images + text πŸ†• Multimodal RAG 17_multimodal_rag.ipynb πŸ†•
🎯 Custom embeddings πŸ†• Fine-tuning Guide 18_finetuning_embeddings.ipynb πŸ†•
πŸ“ˆ Quality evaluation RAGAS 16_evaluation_ragas.ipynb ✨

Rule of thumb: Start with Simple RAG β†’ Add Contextual for quality β†’ Use specialized for specific needs.

Performance tip: For domain-specific content with <75% accuracy, consider fine-tuning embeddings (notebook 18) for +15-25% improvement.

❓ Need help choosing? See FAQ β†’

πŸš€ Production Deployment πŸ†•

Ready to deploy? Choose from 3 production-ready templates:

🐳 Docker (Recommended)

# Quick start with Docker Compose
docker-compose up -d

# Or build custom image
docker build -t langchain-rag:latest .
docker run -p 8000:8000 --env-file .env langchain-rag:latest

Features: Multi-stage builds, Redis caching, Prometheus + Grafana monitoring, non-root security

πŸš‚ FastAPI REST API

Complete REST API with automatic documentation:

cd templates/fastapi
pip install -r requirements.txt
uvicorn app:app --reload
# Visit http://localhost:8000/docs for Swagger UI

Features: Pydantic validation, CORS, health checks, error handling, async support

🎨 Streamlit Web UI

Interactive web application:

cd templates/streamlit
pip install -r requirements.txt
streamlit run streamlit_app.py

Features: Real-time queries, source document display, architecture selection, metrics visualization

⚑ AWS Lambda (Serverless)

Deploy to AWS Lambda:

cd templates/lambda
zip -r function.zip .
aws lambda create-function --function-name rag-api --zip-file fileb://function.zip

Features: S3 vector store integration, cold start optimization, API Gateway ready

πŸ“– Full guide: docs/DEPLOYMENT.md

πŸ“Š Performance at a Glance

Architecture Latency Cost/Query Accuracy Best For
Simple RAG ~2s $0.002 Good General Q&A
Contextual RAG ✨ ~2-3s $0.002 Very Good Technical docs
Fusion RAG ✨ ~5-8s $0.006 Excellent Research
SQL RAG ✨ ~2-5s $0.004 Perfect* Analytics
GraphRAG ✨ ~3-8s $0.010+ Excellent** Relationships
Adaptive RAG Variable $0.003 Very Good Mixed workloads
Agentic RAG ~30s $0.012 Excellent Complex tasks

*For structured data | **For multi-hop queries

Full benchmarks: 11_comparison.ipynb | RAGAS Evaluation

🚦 Prerequisites

  • Python 3.9+ (3.10+ recommended)
  • OpenAI API Key (Get one here)
  • ~2GB RAM (4GB+ recommended for fine-tuning)
  • ~2GB disk space (dependencies + models)
  • System packages (for multimodal): Tesseract OCR, Poppler (see INSTALLATION.md)

πŸ“– Detailed requirements β†’

πŸŽ“ Learning Path

Recommended sequence:

  1. Setup (10 min): GETTING_STARTED.md
  2. Navigation Hub (5 min): 00_index.ipynb
  3. Fundamentals (30-40 min): Notebooks 01-03
  4. Choose Your Path:
    • πŸƒ Fast track: Simple RAG β†’ Contextual RAG β†’ Your use case
    • πŸ”¬ Deep dive: Complete all 12 architectures
    • πŸ“Š Comparison: Jump to 11_comparison.ipynb
    • πŸ“ˆ Evaluation: Try 16_evaluation_ragas.ipynb

Total time:

  • Fast track: ~1-2 hours
  • Complete tutorial: ~5-7 hours
  • With multimodal + evaluation: ~7-9 hours
  • Production deployment: +1-2 hours

🀝 Contributing

Contributions welcome! See CONTRIBUTING.md for guidelines.

Ways to contribute:

  • πŸ› Report bugs
  • ✨ Suggest features
  • πŸ“ Improve documentation
  • πŸ’» Submit pull requests

πŸ› οΈ Development πŸ†•

Quick commands with Makefile:

make install      # Install all dependencies
make test         # Run test suite with coverage
make lint         # Run code quality checks
make format       # Auto-format code (black, isort)
make docker-build # Build Docker image
make docker-run   # Run full stack with Docker Compose
make clean        # Clean cache and build files

Testing:

# Run all tests with coverage
pytest tests/ -v --cov=shared --cov-report=html

# Run specific test file
pytest tests/test_utils.py -v

# Run with markers
pytest -m "not slow"  # Skip slow tests

Pre-commit hooks:

# Install hooks (runs on every commit)
pre-commit install

# Run manually
pre-commit run --all-files

CI/CD: Automated testing and linting on every push via GitHub Actions (Python 3.9, 3.10, 3.11)

πŸ“– More details: docs/CONTRIBUTING.md

πŸ“œ License

MIT License - see LICENSE for details.

TL;DR: Free to use commercially, modify, and distribute. Just include the license.

πŸ”— Resources

πŸ’¬ Getting Help


⭐ If this helps you, please star the repo!

Latest: v1.2.1 - Critical import fixes, Python 3.9 compatibility, accelerate support | Made with ❀️ using Claude Code | View Changelog

About

Complete LangChain RAG tutorial with OpenAI and HuggingFace embeddings comparison

Resources

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •