Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

PDF RAG Engine

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.

Features

  • 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 Qdrant running 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.

Requirements

Ensure you have Python 3.8+ and install the necessary dependencies:

pip install qdrant-client sentence-transformers torch PyMuPDF langchain-text-splitters

Usage

  1. Place your target PDF file in the root directory (e.g., sample.pdf).
  2. Open pdf_rag.py and scroll to the bottom. Edit the pdf_file and question variables 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)
  1. Run the script:
python pdf_rag.py

The 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.

Project Structure

.
├── pdf_rag.py            # Main application script
├── qdrant_pdf_db/        # Auto-generated local Qdrant vector database storage
└── README.md             # Project documentation

About

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.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages