Skip to content

Latest commit

 

History

9 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RAG-based Document Q&A System

A production-ready Retrieval-Augmented Generation (RAG) system that allows users to upload PDF documents, automatically index and embed their content, and ask natural language questions to retrieve precise answers based only on the uploaded documents.

This project implements a complete dual-service architecture: a fast and robust FastAPI backend API that handles PDF parsing, semantic chunking, database storage, and retrieval-augmented generation, coupled with an interactive Streamlit frontend for user-friendly document management and Q&A.


Tech Stack & Architecture

The application is built using modern AI and web technologies:

  • Frontend: Streamlit – Provides a clean, interactive user interface for uploading PDFs and asking questions.
  • Backend API: FastAPI – High-performance asynchronous API framework to handle uploads, retrieval pipelines, and model calls.
  • LLM Orchestration: LangChain – Powers the ingestion pipeline, text splitting, prompt management, and LLM chains.
  • Vector Database: Chroma DB – High-performance, developer-friendly vector store for saving and querying document chunk embeddings.
  • Text Embeddings: Hugging Face (sentence-transformers/all-MiniLM-L6-v2) – Embeds text chunks locally into 384-dimensional vectors.
  • Large Language Model (LLM): Groq API – Utilizes the state-of-the-art Llama 3.3 70B Versatile model for high-accuracy, low-latency responses.
  • PDF Processing: PyPDFLoader – Parses text from uploaded PDF documents.
  • Containerization: Docker – Package the FastAPI backend service for seamless deployments.

Project Directory Structure

rag-qa-system/
│
├── data/                  # Local folder where uploaded PDFs are stored (git-ignored)
├── chroma_db/             # SQLite/ChromaDB vector persistence directory (git-ignored)
│
├── src/                   # Source Code
│   ├── __init__.py        # Package initialization & path resolution
│   ├── app.py             # Streamlit Frontend UI
│   ├── main.py            # FastAPI Backend API Server
│   ├── ingest.py          # PDF Document Ingestion & Chunking Logic
│   ├── embed_store.py     # Embedding Generation & ChromaDB Storage
│   ├── retrieve.py        # Vector Store Loading & Similarity Search
│   └── generate.py        # LLM Invocation & Context-based Q&A Generation
│
├── tests/                 # Unit and integration tests folder
├── Dockerfile             # Docker container configuration for backend service
├── .dockerignore          # Docker build exclusion patterns
├── .gitignore             # Git exclusion rules
├── .env.example           # Template for environment configuration
├── requirements.txt       # Python dependencies list
└── README.md              # Detailed project documentation

How the RAG Pipeline Works

The RAG pipeline operates in two distinct phases:

1. Document Ingestion Phase

graph TD
    A[Upload PDF via Streamlit UI] --> B[FastAPI Endpoint: /upload]
    B --> C[Save PDF to data/ folder]
    C --> D[Load & Extract Text via PyPDFLoader]
    D --> E[Chunk Text recursively chunk_size=1000, overlap=200]
    E --> F[Generate Embeddings via sentence-transformers]
    F --> G[Save Vectors and Metadata to ChromaDB]
Loading

2. Retrieval & Generation Phase

graph TD
    A[User enters query in UI] --> B[FastAPI Endpoint: /ask?query=...]
    B --> C[Generate Query Embedding]
    C --> D[Query ChromaDB for Top-4 similar chunks]
    D --> E[Construct Prompt with Context + Query]
    E --> F[Send to Groq Llama 3.3 70B]
    F --> G[Retrieve Answer]
    G --> H[Return Answer to Streamlit UI]
Loading

Setup & Installation

Follow these steps to run the project locally.

Prerequisites

Ensure you have the following installed:

  • Python 3.11 or higher
  • A Groq API Key (Get one from Groq Console)

1. Clone the Repository

git clone https://github.com/vivekducs/docs-rag.git
cd docs-rag

2. Create and Activate a Virtual Environment

On Windows (PowerShell):

python -m venv .venv
.venv\Scripts\Activate.ps1

On macOS/Linux:

python3 -m venv .venv
source .venv/bin/activate

3. Install Dependencies

pip install -r requirements.txt

4. Configure Environment Variables

Create a .env file in the root directory:

cp .env.example .env

Open the .env file and enter your API keys:

GROQ_API_KEY=your_groq_api_key_here

Running the Application

To run the application, you need to start both the Backend (FastAPI) and the Frontend (Streamlit).

Step 1: Run the Backend API (FastAPI)

Start the FastAPI server on port 8000:

uvicorn src.main:app --reload --host 127.0.0.1 --port 8000

Step 2: Run the Frontend (Streamlit)

Open a new terminal window, activate the virtual environment, and run:

streamlit run src/app.py

Running with Docker

You can containerize the backend API using Docker:

1. Build the Docker Image

docker build -t rag-qa-system .

2. Run the Container

Pass the environment variables file .env during execution:

docker run -d -p 8000:8000 --env-file .env --name rag-backend rag-qa-system

The FastAPI service will be running on http://localhost:8000.

About

RAG Application to chat with the provided document

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages