Ask natural-language questions about any PDF and get answers grounded in the document's actual content, with the source passages shown. Built from scratch to understand every part of a Retrieval-Augmented Generation (RAG) pipeline — no high-level framework hiding the internals.
- Loads a PDF and splits it into overlapping text chunks
- Embeds each chunk locally (free, on-device) and stores them in a Chroma vector database
- Retrieves the most semantically relevant chunks for each question
- Sends those chunks to the OpenAI API to generate a grounded answer
- Refuses to answer ("I don't know based on the document") when the answer isn't in the source — reducing hallucination
Indexing (once): PDF -> chunk -> embed -> store in Chroma Answering (per Q): question -> embed -> retrieve top-k chunks -> LLM answers with that context
- Python
- OpenAI API (gpt-4o-mini) — answer generation
- Chroma — local vector database + embeddings (all-MiniLM-L6-v2)
- pypdf — PDF text extraction
- python-dotenv — API key management
- Clone the repo and
cdinto it - Create a virtual environment and install dependencies: python3 -m venv venv source venv/bin/activate pip install -r requirements.txt
- Copy
.env.exampleto.envand add your OpenAI API key - Put a PDF named
document.pdfin the folder - Run it: python app.py
- Ask questions in the terminal; type
quitto exit
- Chunk with overlap so meaning isn't split across boundaries
- Embeddings run locally (free, private); only answer generation calls the API
- A strict grounding prompt keeps answers faithful to the document
- Index once, then loop on questions — retrieval stays fast, no re-embedding per query