RAG Document Assistant: AI-Powered Document Q&A
A full-stack AI document assistant that allows users to upload PDF files and ask questions using Retrieval-Augmented Generation, combining Gemini, semantic embeddings, FAISS vector search, and source-aware document retrieval.
Overview
RAG Document Assistant is a full-stack AI application that allows users to upload PDF documents and ask questions directly about their contents using Retrieval-Augmented Generation (RAG).
Instead of relying only on the general knowledge of a Large Language Model, the system retrieves relevant sections from uploaded documents and provides them as context before generating an answer.
This approach helps produce answers that are more grounded in the user's documents and reduces the risk of generating information that is not present in the source material.
Problem
Finding specific information inside long documents can be time-consuming.
Users often need to manually search through dozens or hundreds of pages before finding the information they need.
RAG Document Assistant addresses this problem by allowing users to interact with documents through natural language.
Users can upload a PDF and ask questions such as:
- "What is the main conclusion of this document?"
- "What does the document say about this topic?"
- "Which page contains information about this subject?"
- "Summarize the relevant information about this concept."
The application retrieves relevant document sections and uses them as context for the AI-generated answer.
Retrieval-Augmented Generation
The application uses a Retrieval-Augmented Generation pipeline.
The workflow consists of:
- User uploads a PDF document.
- Text is extracted from each document page.
- Extracted text is divided into smaller chunks.
- Each chunk is converted into an embedding representation.
- Embeddings are stored inside a FAISS vector index.
- The user's question is converted into a semantic search query.
- The most relevant document chunks are retrieved.
- Retrieved context is sent to Gemini together with the user's question.
- The generated answer is returned together with document source information.
This architecture allows the LLM to answer questions using information retrieved from the uploaded documents.
PDF Processing
The backend accepts PDF documents through an upload endpoint.
Before processing, the system validates:
- File extension
- PDF file signature
- File name safety
PDF content is extracted using PyMuPDF.
The application stores page-level metadata so retrieved information can later reference the original document and page number.
Document Chunking
Document text is divided into smaller chunks before being embedded.
The default configuration uses:
- Chunk Size: 900 characters
- Chunk Overlap: 150 characters
Chunk overlap helps preserve context between neighboring sections of a document.
The values can also be adjusted through environment configuration.
Semantic Embeddings
Document chunks are transformed into semantic vector representations using Google Generative AI embeddings.
The default embedding model is:
gemini-embedding-001
These embeddings capture semantic similarity between document content and user questions.
Vector Search with FAISS
FAISS is used as the vector store for document retrieval.
When a user asks a question, the system performs similarity search against the indexed document chunks.
The default configuration retrieves up to four highly relevant chunks before generating the answer.
A relevance threshold is also applied so low-quality retrieval results are excluded from the LLM context.
Document-Scoped Retrieval
Users can optionally restrict a question to a specific document.
When a document_id is provided, semantic search is filtered so retrieved chunks only come from the selected document.
This makes the application suitable for environments containing multiple uploaded PDF files.
Grounded AI Responses
The application uses Gemini to generate responses from retrieved context.
The LLM prompt explicitly instructs the model to:
- Answer only using the provided document context
- Avoid inventing information
- Provide clear and concise answers
- Return a predefined fallback message when the information is not available
This helps reduce hallucination and keeps answers grounded in the indexed documents.
Source-Aware Answers
Retrieved information includes document metadata such as:
- Document name
- Document ID
- Page number
- Relevance score
- Text excerpt
These references allow users to understand where the supporting information originated.
Backend API
The backend is built using FastAPI.
Main endpoints include:
GET /healthGET /documentsPOST /uploadPOST /chat
The API handles document indexing, file management, semantic retrieval, and AI question answering.
Frontend Application
The user interface is built using React and Vite.
The frontend communicates with the FastAPI backend to provide an interactive workflow for:
- Uploading PDF documents
- Viewing available documents
- Selecting document context
- Asking questions
- Displaying AI-generated answers
- Displaying relevant document sources
The frontend and backend are separated so they can be deployed independently.
Application Architecture
The application follows a separated frontend-backend architecture.
Frontend
React + Vite handles the user interface and API communication.
Backend
FastAPI handles:
- PDF upload
- Document parsing
- Text chunking
- Embedding generation
- Vector indexing
- Semantic retrieval
- Gemini integration
- Source metadata
AI Layer
Gemini is used for both semantic embeddings and answer generation.
Vector Store
FAISS provides fast in-memory similarity search for indexed document chunks.
Deployment
The project supports separate deployment for frontend and backend.
The recommended deployment architecture is:
- Frontend: Vercel
- Backend: Hugging Face Spaces using Docker
Environment variables are used to configure the Gemini API key and backend URL.
The current implementation uses an in-memory FAISS index and does not require a permanent database.
Tech Stack
Python, FastAPI, React, Vite, LangChain, Gemini API, Google Generative AI Embeddings, FAISS, PyMuPDF, Pydantic, Uvicorn, and Retrieval-Augmented Generation.
