Large Language Models (LLMs) are great at reasoning and writing, but they have one major limitation: they only know what they were trained on. They have no idea what is inside your private PDFs, company docs, or personal study notes.

That is where RAG (Retrieval-Augmented Generation) comes in. Instead of relying solely on a model's pretraining, RAG connects an LLM to an external knowledge base so it can fetch relevant facts at query time and generate grounded, accurate answers.

Understanding the RAG Pipeline: Offline vs. Online (and Local vs. Cloud)
Every RAG system is built around two core pipelines working together:

The Offline Ingestion Pipeline: Runs when you add documents. It extracts text, splits it into smaller overlapping chunks, converts those chunks into numerical vectors (embeddings), and stores them in a vector database.

The Online Query Pipeline: Runs in real time when you ask a question. It embeds your question, searches the vector database for the most relevant chunks, injects them into a prompt, and asks the LLM to answer strictly using that context.

When people talk about a Local RAG Pipeline versus an Online (Cloud) RAG Pipeline, the difference is where the brains live. An online/cloud RAG sends your documents and prompts to third-party APIs (like OpenAI or Gemini) and managed cloud databases. A Local RAG Pipeline runs everything—the embedding model, the vector database, and the LLM—directly on your own machine. That means 100% data privacy, zero API costs, and full offline capability.

What This Project Does

This project is a local "Chat with Your PDFs" assistant built from scratch in Python without heavy abstraction frameworks. You drop PDF files into a folder, index them into a local vector database, ask questions in your terminal, and get grounded answers with source citations—plus an automated evaluation script to measure how accurate the system actually is.

What I Built

To truly learn how RAG works under the hood, I avoided black-box frameworks and built three focused Python scripts:

  1. ingest.py (Document Ingestion): Reads PDFs from a local docs/ folder, splits the text into overlapping chunks, generates vector embeddings locally, and persists them into ChromaDB. It also syncs deletions so removed PDFs don't linger in the database.

  2. chat.py (Retrieval & Generation): Takes a user question from the terminal, retrieves the top matching chunks via semantic search, streams a grounded answer from a local LLM, and cites the source PDF filenames.

  3. eval.py (Automated Evaluation): Uses an LLM-as-a-Judge approach against a custom JSON test set (eval_dataset.json) to score Retrieval Recall (did search find the right text?) and Answer Faithfulness (did the LLM avoid hallucinating?).

Architecture Overview & Tech Stack

Here is how data flows through the system:

  1. Ingestion: PDFs → pypdf (text extraction) → Chunker (overlapping slices) → nomic-embed-text (embeddings) → ChromaDB (local persistent storage)

  2. Querying: User Question → nomic-embed-text → ChromaDB (cosine similarity search) → Top-K Chunks + Prompt → llama3.2:3b → Streamed Answer + Citations

How I Built It

Step 1: Setting Up the Local Models

First, I installed Ollama and pulled the two local models—one for embeddings and one for chat:

ollama pull llama3.2:3b ollama pull nomic-embed-text 

Then I created a Python virtual environment and installed the only three packages needed:

python -m venv .venv # Activate virtual environment: # Windows: .venv\Scripts\activate # macOS/Linux: source .venv/bin/activate pip install ollama chromadb pypdf 

Step 2: Building the Ingestion Pipeline (ingest.py)
The ingestion script extracts text from every PDF in ./docs, breaks it into overlapping chunks (so sentences aren't cut off mid-thought), embeds each chunk with nomic-embed-text, and saves the vectors + metadata into ChromaDB's

response = ollama.embed(model="nomic-embed-text", input=chunk) embedding = response["embeddings"][0] collection.upsert( ids=[f"{filename}_chunk_{i}"], embeddings=[embedding], documents=[chunk], metadatas=[{"source": filename, "chunk_index": i}] ) 

Real-world gotcha I hit: When I deleted a PDF from my docs/ folder, its old chunks still showed up in search results! Deleting a file doesn't automatically wipe its vectors from ChromaDB. I fixed this by adding a sync check in ingest.py that compares the current docs/ folder against the source metadata stored in ChromaDB and deletes any orphaned records.

Step 3: Building the Query & Chat Loop (chat.py)
When the user types a question, chat.py embeds the query using the exact same embedding model (nomic-embed-text), fetches the top matching chunks from ChromaDB, and passes them into a strict system prompt for llama3.2:3b:

system_prompt = ( "You are a helpful assistant. Answer the user's question using ONLY " "the provided context below. If the answer is not in the context, " "say 'I don't have enough information in the documents to answer that.'\n\n" f"Context:\n{context}" ) 

Debugging lesson: Early on, the assistant refused to answer a question even though I knew the answer was in the PDF. Printing the retrieved chunks before sending them to the LLM immediately showed why: my initial 500-character chunks were too small and were cutting off context. Increasing the chunk size and fetching more chunks (n_results)
solved it

Step 4: Measuring Quality with eval.py

To stop guessing whether tweaks actually improved the pipeline, I built eval.py. It loads a set of test questions and ground-truth answers from eval_dataset.json, runs the RAG pipeline, and asks llama3.2:3b to return a structured JSON score (0.0 to 1.0) for both Retrieval Recall and Answer Faithfulness.

To keep it fast on local hardware, I batch-embedded all test questions upfront (so Ollama doesn't repeatedly swap between the embedding model and chat model) and combined both metrics into a single judge call.

Building this from scratch made one thing crystal clear: most RAG bugs are retrieval bugs, not LLM bugs. Getting chunking, metadata syncing, and evaluation right matters far more than tweaking prompts.