Skip to content

Latest commit

ย 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ“„ QueryDoc โ€” RAG-Based Document Question Answering System

Ask your documents. Get grounded answers.

QueryDoc is a Retrieval-Augmented Generation (RAG) based document question-answering system that allows users to upload a PDF and ask questions about its content.

Instead of relying only on the knowledge stored inside an LLM, QueryDoc retrieves the most relevant information from the uploaded document and provides it to the LLM as context before generating an answer.

The goal of this project is to understand and implement the complete RAG pipeline from scratch.


๐Ÿš€ Overview

Large Language Models can answer questions about general knowledge, but they may not know the contents of a user's private documents.

QueryDoc solves this problem by combining:

  • ๐Ÿ“‘ Document processing
  • โœ‚๏ธ Text chunking
  • ๐Ÿง  Text embeddings
  • ๐Ÿ”Ž Semantic similarity search
  • ๐Ÿ—„๏ธ Vector storage
  • ๐Ÿค– Large Language Models
  • ๐Ÿ“š Source/page references

Basic Workflow

                PDF DOCUMENT
                     โ”‚
                     โ–ผ
              Text Extraction
                     โ”‚
                     โ–ผ
                 Chunking
                     โ”‚
                     โ–ผ
                Embeddings
                     โ”‚
                     โ–ผ
              Vector Database
                   (FAISS)
                     โ”‚
                     โ”‚
                     โ–ผ
              User Question
                     โ”‚
                     โ–ผ
             Question Embedding
                     โ”‚
                     โ–ผ
             Similarity Search
                     โ”‚
                     โ–ผ
           Relevant Document Chunks
                     โ”‚
                     โ–ผ
             Context + Question
                     โ”‚
                     โ–ผ
                   Gemini
                     โ”‚
                     โ–ผ
              Generated Answer
                     โ”‚
                     โ–ผ
               ๐Ÿ“š Source Pages

โœจ Features

๐Ÿ“ค PDF Upload

Upload a PDF document directly through the application.

๐Ÿ“– PDF Text Extraction

Extract text from individual PDF pages while preserving page information.

โœ‚๏ธ Intelligent Text Chunking

Large documents are divided into smaller chunks so that relevant information can be efficiently retrieved.

๐Ÿง  Semantic Embeddings

Text chunks are converted into numerical vector representations using an embedding model.

๐Ÿ”Ž Semantic Search

QueryDoc searches the vector database to find the chunks most relevant to the user's question.

๐Ÿ—„๏ธ Vector Storage

FAISS is used for efficient similarity search over document embeddings.

๐Ÿค– AI-Powered Answers

The retrieved document context is passed to a Large Language Model to generate a natural-language answer.

๐Ÿ›ก๏ธ Grounded Responses

The system is instructed to answer using the retrieved document context instead of relying on unrelated information.

๐Ÿ“š Source References

Relevant page numbers are displayed with answers so users can identify where the information came from.


๐Ÿงฉ Technology Stack

Component Technology
Programming Language Python
User Interface Streamlit
PDF Processing PyPDF
Embeddings Sentence Transformers
Vector Database FAISS
LLM Google Gemini API
Version Control Git & GitHub

๐Ÿ—๏ธ Project Architecture

QueryDoc
โ”‚
โ”œโ”€โ”€ app.py
โ”‚   โ””โ”€โ”€ Streamlit user interface
โ”‚
โ”œโ”€โ”€ pdf_processor.py
โ”‚   โ”œโ”€โ”€ PDF loading
โ”‚   โ”œโ”€โ”€ Text extraction
โ”‚   โ””โ”€โ”€ Text cleaning
โ”‚
โ”œโ”€โ”€ embeddings.py
โ”‚   โ””โ”€โ”€ Generate embeddings for text
โ”‚
โ”œโ”€โ”€ vector_store.py
โ”‚   โ”œโ”€โ”€ Store vectors
โ”‚   โ””โ”€โ”€ Similarity search using FAISS
โ”‚
โ”œโ”€โ”€ rag.py
โ”‚   โ”œโ”€โ”€ Retrieve relevant chunks
โ”‚   โ”œโ”€โ”€ Build context
โ”‚   โ”œโ”€โ”€ Call Gemini
โ”‚   โ””โ”€โ”€ Generate grounded response
โ”‚
โ”œโ”€โ”€ data/
โ”‚   โ””โ”€โ”€ Uploaded documents
โ”‚
โ”œโ”€โ”€ requirements.txt
โ””โ”€โ”€ README.md

๐Ÿ”„ RAG Pipeline

QueryDoc follows a standard Retrieval-Augmented Generation architecture.

1. Document Loading

The user uploads a PDF.

PDF
 โ†“
PyPDF
 โ†“
Extracted text

Each page is processed separately so that page numbers can later be used as sources.


2. Text Chunking

The extracted text is divided into smaller sections.

Large Document
      โ†“
 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
 โ”‚ Chunk 1  โ”‚
 โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
 โ”‚ Chunk 2  โ”‚
 โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
 โ”‚ Chunk 3  โ”‚
 โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
 โ”‚   ...    โ”‚
 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Each chunk contains metadata such as:

chunk_id
page_number
text

This allows QueryDoc to retrieve information while maintaining its original location.


3. Embedding Generation

Every chunk is converted into a vector representation.

Text Chunk
    โ†“
Embedding Model
    โ†“
Numerical Vector

Example:

"Deadlock occurs when processes wait indefinitely..."

        โ†“

[0.12, -0.42, 0.81, 0.19, ...]

These vectors capture the semantic meaning of the text.


4. Vector Storage

The generated embeddings are stored in a FAISS index.

Chunk 1 โ†’ Vector 1
Chunk 2 โ†’ Vector 2
Chunk 3 โ†’ Vector 3
...
Chunk N โ†’ Vector N

FAISS allows QueryDoc to efficiently search for vectors that are similar to a user's question.


5. Query Processing

When a user asks a question:

User Question
      โ†“
Embedding Model
      โ†“
Question Vector

The question vector is compared with the stored document vectors.


6. Retrieval

QueryDoc retrieves the most relevant chunks.

For example:

Question:
"What are the necessary conditions for deadlock?"

Retrieved:

Page 12 โ†’ Chunk 35
Page 13 โ†’ Chunk 36
Page 14 โ†’ Chunk 41

These chunks become the context for the LLM.


7. Context Augmentation

The retrieved information and user question are combined:

Retrieved Context
       +
User Question
       โ†“
Prompt

The prompt instructs the LLM to answer using the retrieved document context.


8. Answer Generation

The final prompt is sent to Gemini.

Context + Question
        โ†“
      Gemini
        โ†“
     Answer

The answer is then displayed to the user along with relevant source pages.


๐ŸŽฏ Example

Uploaded document

Operating Systems Notes.pdf

User question

What are the four necessary conditions for deadlock?

QueryDoc

1. Converts the question into an embedding
2. Searches FAISS
3. Retrieves relevant chunks
4. Sends the chunks + question to Gemini
5. Generates the answer
6. Displays source pages

Output

The four necessary conditions for deadlock are:

1. Mutual Exclusion
2. Hold and Wait
3. No Preemption
4. Circular Wait

Sources:
๐Ÿ“„ Page 12
๐Ÿ“„ Page 13

๐Ÿ›ก๏ธ Handling Unknown Questions

QueryDoc is designed to avoid answering questions that cannot be supported by the uploaded document.

For example:

User:
Who invented the telephone?

If the information is not available in the uploaded document:

I couldn't find this information in the uploaded document.

This helps reduce unsupported or hallucinated answers.


๐Ÿ’ป Installation

1. Clone the Repository

git clone https://github.com/YOUR-USERNAME/QueryDoc.git
cd QueryDoc

2. Create a Virtual Environment

Windows

python -m venv venv

Activate it:

venv\Scripts\activate

macOS / Linux

python3 -m venv venv
source venv/bin/activate

3. Install Dependencies

pip install -r requirements.txt

๐Ÿ”‘ API Configuration

QueryDoc uses the Google Gemini API for answer generation.

Create an API key and store it as an environment variable.

For local development, you can use a .env file:

GEMINI_API_KEY=your_api_key_here

โš ๏ธ Never commit your API key to GitHub.

Add the following to .gitignore:

.env
venv/
__pycache__/
*.pyc

โ–ถ๏ธ Run the Application

Start the Streamlit application:

streamlit run app.py

The application will open in your browser.


๐Ÿ“‚ Example Usage

Step 1

Upload a PDF.

๐Ÿ“„ Upload Document
[ Select PDF ]

Step 2

Process the document.

[ Process Document ]

Step 3

Ask a question.

What is normalization in DBMS?

Step 4

QueryDoc retrieves relevant content.

Step 5

Gemini generates a grounded answer.

Step 6

Relevant source pages are displayed.


๐Ÿ“Š Current Project Scope

The initial version focuses on:

  • Single PDF processing
  • Text extraction
  • Text chunking
  • Embedding generation
  • FAISS vector search
  • Question answering
  • Source/page references
  • Basic hallucination prevention

๐Ÿ”ฎ Future Improvements

The project can be extended into a more advanced RAG system.

๐Ÿ“š Multiple Documents

Allow users to upload and search across multiple PDFs.

๐Ÿท๏ธ Metadata Filtering

Filter documents based on:

  • Subject
  • Topic
  • Date
  • Document type
  • Page

๐Ÿ”Ž Hybrid Search

Combine:

Keyword Search
       +
Vector Search
       โ†“
Better Retrieval

๐ŸŽฏ Reranking

Retrieve a larger number of chunks and use a reranker to select the most relevant context.

๐Ÿง  Query Rewriting

Rewrite ambiguous questions before retrieval.

๐Ÿ’ฌ Conversation Memory

Allow users to ask follow-up questions while maintaining conversation context.

๐Ÿ“Š RAG Evaluation

Evaluate:

  • Retrieval accuracy
  • Context relevance
  • Answer faithfulness
  • Answer quality

๐Ÿ–ผ๏ธ Multimodal RAG

Support:

  • Images
  • Tables
  • Charts
  • Scanned PDFs

โ˜๏ธ Deployment

Deploy QueryDoc as a publicly accessible web application.


๐Ÿงช Learning Objectives

This project is primarily designed as a hands-on learning project for understanding RAG.

Through QueryDoc, the following concepts are explored:

LLM
 โ†“
Prompt Engineering
 โ†“
Embeddings
 โ†“
Vector Representations
 โ†“
Cosine Similarity
 โ†“
Vector Databases
 โ†“
Document Chunking
 โ†“
Semantic Retrieval
 โ†“
Context Augmentation
 โ†“
RAG
 โ†“
Grounded Generation

๐Ÿ“š Key Concepts Learned

Concept Purpose
LLM Generates natural-language responses
Embeddings Represent text as numerical vectors
Chunking Break large documents into manageable pieces
Vector Database Store and search embeddings
Similarity Search Find relevant document sections
Retrieval Select useful context
Prompt Engineering Control how the LLM uses context
RAG Combine retrieval with generation
Grounding Keep answers connected to source information
Metadata Track document/page information

โš ๏ธ Limitations

The initial version may have limitations such as:

  • PDF extraction quality depends on document structure
  • Scanned PDFs may require OCR
  • Complex tables may not be extracted correctly
  • Retrieval quality depends on chunking strategy
  • Very ambiguous questions may retrieve irrelevant content
  • LLM responses depend on the quality of retrieved context

These limitations provide opportunities for future RAG improvements.


๐Ÿ” Security

  • API keys should never be committed to the repository.
  • User documents should be handled securely.
  • Sensitive documents should not be uploaded to an untrusted deployment.
  • Production deployments should implement authentication and access control.

๐Ÿ“ˆ Future Architecture

The project can eventually evolve from:

Basic RAG

to:

                 Query
                   โ”‚
                   โ–ผ
             Query Rewriter
                   โ”‚
                   โ–ผ
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
          โ”‚ Hybrid Retrievalโ”‚
          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ†“
               Reranker
                   โ†“
           Relevant Context
                   โ†“
                 LLM
                   โ†“
          Answer + Citations
                   โ†“
             Evaluation

๐Ÿค Contributing

Contributions, suggestions, and improvements are welcome.

Steps

git clone https://github.com/YOUR-USERNAME/QueryDoc.git

Create a new branch:

git checkout -b feature/new-feature

Make your changes and commit:

git add .
git commit -m "Add new feature"

Push your branch:

git push origin feature/new-feature

Then open a Pull Request.


๐Ÿ“œ License

This project is intended for educational and research purposes.

A suitable open-source license can be added when the project is ready for public distribution.


๐Ÿ‘จโ€๐Ÿ’ป Author

Your Name

Built as a hands-on project to understand and implement Retrieval-Augmented Generation (RAG) from the ground up.


โญ Project Vision

QueryDoc started as a simple PDF question-answering system and is designed to evolve into a complete document intelligence platform.

From documents to knowledge โ€” QueryDoc makes information searchable, understandable, and grounded.

โญ If you find this project useful, consider giving the repository a star.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages