π€ A production-style Hybrid RAG system that answers from PDFs, the internet, or both β with intelligent routing, reranking, and grounded citations.
ChatPDF v2 is a major upgrade over the initial version β evolving from a simple RAG pipeline into a modular, extensible system with:
- smarter retrieval
- structured source attribution
- hybrid reasoning (PDF + Web)
- cleaner architecture for scaling
| Area | v1 | v2 |
|---|---|---|
| Architecture | Single script | Modular (pipeline / rag / tools / memory) |
| Retrieval | Basic similarity | FAISS + CrossEncoder reranking |
| Sources | Loose references | Inline citations + page grounding |
| Routing | Manual | Intent-based + auto routing |
| Hybrid Mode | Basic fallback | Structured PDF + Web fusion |
| Memory | Minimal | Query-aware conversational memory |
The system is now organized into clean, extensible modules:
pipeline/ β orchestration, routing, evaluation
rag/ β ingestion, retrieval, reranking
llm/ β response generation
tools/ β web search, summarization
memory/ β chat history & query rewriting
This makes it easier to:
- swap components (LLMs, retrievers, rerankers)
- debug individual stages
- extend the pipeline
chatpdf-rag/
β
βββ app.py
βββ config.py
βββ cache_store.py
βββ preload_models.py
β
βββ llm/ # LLM loading & generation
βββ memory/ # Conversational memory
βββ pipeline/ # Routing & orchestration
βββ prompt/ # Prompt templates
βββ rag/ # Embeddings & retrieval
βββ tools/ # Web search & utilities
βββ assets/ # UI images & demos
βββ public/ # Icons and static images
β
βββ README.mdUser Query
β
Intent Classification
β
Source Router (PDF / Internet / Auto)
β
Retriever (FAISS)
β
Reranker (CrossEncoder)
β
Context Builder + Citations
β
LLM Generation (Mistral)
β
Final Response + Sources
π‘ Key idea: Good RAG is not just retrieval β itβs routing + ranking + grounding + UX
- π Multi-PDF ingestion
- π FAISS-based semantic retrieval
- π§ CrossEncoder reranking (BAAI/bge-reranker-base)
- π Internet fallback (DuckDuckGo)
- π€ Intelligent source routing (PDF / Web / Hybrid)
- π§Ύ Inline citations with page references
- π¬ Conversational memory with query rewriting
- β‘ Chainlit UI
| Mode | Behavior |
|---|---|
| π PDF | Answers strictly from documents |
| π Internet | Uses web search only |
| π€ Auto | Dynamically selects best source |
Users can customize system behavior via the Chainlit settings panel, enabling control over model selection, embedding quality, and performance optimizations.
| Setting | Options | Default | Description |
|---|---|---|---|
| AI Model | mistral, mistral2, llama | mistral | Choose the LLM backend for answer generation. |
| Quantization | True / False | True | Enable or disable model quantization to reduce memory usage. |
| Embedding | Fast, Balanced, Better, Strong | Balanced | Controls the embedding model used for PDF retrieval. |
| Reranker | BAAI/bge-reranker-base (default) | BAAI/bge-reranker-base | Cross-encoder used for reranking retrieved results (not user-configurable). |
- Fast:
sentence-transformers/all-MiniLM-L6-v2 - Balanced:
BAAI/bge-base-en-v1.5 - Better:
BAAI/bge-large-en-v1.5 - Strong:
intfloat/e5-large-v2
mistral:mistralai/Mistral-7B-Instruct-v0.2mistral2:mistralai/Mistral-7B-Instruct-v0.3llama:meta-llama/Meta-Llama-3-8B-Instruct
The reranking layer is system-controlled (configured in config.py, not exposed via UI):
- Primary model:
BAAI/bge-reranker-base - Fallback model:
cross-encoder/ms-marco-MiniLM-L-6-v2 - Execution device: CPU (
DEVICE_RERANKER = "cpu")
- Inline citations:
[1], [2] - Page-level grounding from PDFs
- Reduced hallucinated references
- Structured source display
Instead of naive fallback, v2 uses structured fusion:
- PDF answer β primary grounding
- Web answer β supplementary context
- Graceful fallback when PDFs lack coverage
- "Summarize the document"
- "List all risks mentioned"
- "What is the revenue growth?"
- "Compare this with current market trends" (Hybrid)
- "Explain LLMs" (Internet mode)
- LLM: Mistral-7B-Instruct
- Framework: LangChain
- Retrieval: FAISS
- Reranking: BAAI/bge-reranker-base (via SentenceTransformers CrossEncoder)
- Search: DuckDuckGo (ddgs)
- UI: Chainlit
git clone https://github.com/Korunil/chatpdf-rag.git
cd chatpdf-rag
pip install -r requirements.txt
chainlit run app.py- Supports ~5 PDFs per session (configurable)
- Performance depends on local compute (tested on GPU)
- Internet search quality may vary
- Large PDFs may require tuning (chunking, retrieval params)
- Clickable citations β jump to source chunks
- Highlighted answer spans in PDFs
- Confidence scoring
- Streaming responses
- Improved UI (sidebar, collapsible sources)
- Deployment (Docker / Hugging Face Spaces)
RAG is not just βretrieve + generateβ
Itβs: routing + ranking + grounding + memory + UX
Contributions, ideas, and feedback are welcome!
If you're working on RAG systems, I'd especially love input on:
- retrieval quality
- citation UX
- hybrid design



