A lightweight, local Retrieval-Augmented Generation (RAG) engine that extracts text from PDFs, generates vector embeddings, stores them in a local Qdrant vector database, and retrieves the most relevant context for a given user query. It generates a perfectly structured prompt ready to be sent to an LLM (such as Llama-3, GPT-4, etc.) to answer the user's question based strictly on the document.
- PDF Parsing: Extracts raw text from PDF files using
PyMuPDF(fitz). - Text Chunking: Splits extracted text into manageable, context-preserving chunks using
langchain-text-splitters. - Vector Embeddings: Uses Hugging Face's
sentence-transformers(all-MiniLM-L6-v2) to convert text chunks into high-quality 384-dimensional vector embeddings. - Local Vector Database: Uses
Qdrantrunning entirely locally (saving to./qdrant_pdf_db) to store and query the document embeddings without needing an external service or cloud database. - Prompt Generation: Combines the most relevant document excerpts with a strict system prompt to ensure accurate, context-bound LLM answers.
Ensure you have Python 3.8+ and install the necessary dependencies:
pip install qdrant-client sentence-transformers torch PyMuPDF langchain-text-splitters- Place your target PDF file in the root directory (e.g.,
sample.pdf). - Open
pdf_rag.pyand scroll to the bottom. Edit thepdf_fileandquestionvariables to point to your PDF and query:
if __name__ == "__main__":
pdf_file = "sample.pdf"
question = "What are the Key Takeaway from this document?"
run_mini_rag(pdf_file, question)- Run the script:
python pdf_rag.pyThe script will:
- Extract text and chunk it.
- Initialize the local Qdrant Vector database and embed the chunks.
- Search for the top context chunks that answer your question.
- Output a formatted prompt to the terminal that you can provide directly to an LLM to get an accurate answer based solely on your document.
.
├── pdf_rag.py # Main application script
├── qdrant_pdf_db/ # Auto-generated local Qdrant vector database storage
└── README.md # Project documentation