Skip to content

Ganesh153/Production-Ready-RAG-AI-Agent-in-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

RAG AI PDF Chat Application - Production Ready

A production-ready Retrieval-Augmented Generation (RAG) system that allows you to upload PDF documents, process them into a vector database, and ask questions about their content using AI.

Features

  • 📄 PDF Document Processing: Upload and automatically chunk PDF documents
  • 🔍 Vector Search: Efficiently search through document embeddings using Qdrant
  • 🤖 AI-Powered Answers: Get accurate answers from your documents using Groq LLM
  • 🔄 Workflow Orchestration: Reliable event-driven processing with Inngest
  • 🎨 User-Friendly Interface: Clean Streamlit UI for document upload and querying

Tech Stack

  • Backend Framework: FastAPI
  • Workflow Engine: Inngest
  • Vector Database: Qdrant
  • LLM Provider: ChatGroq
  • Embeddings: HuggingFace sentence-transformers/all-MiniLM-L6-v2 (free)
  • Document Processing: LlamaIndex
  • Frontend: Streamlit
  • Language: Python

Prerequisites

  • Python 3.8+
  • Docker (for Qdrant database)
  • Node.js/npm (for Inngest CLI)
  • UV package manager (recommended) or pip

Installation

1. Clone the Repository

git clone <your-repo-url>

2. Install Dependencies

Using UV (recommended):

uv pip install -r requirements.txt

Or using pip:

pip install -r requirements.txt

3. Environment Setup

Create a .env file in the root directory:

GROQ_API_KEY=your_groq_api_key_here

Getting a Groq API Key:

  1. Visit [https://console.groq.com]
  2. Sign up for a free account
  3. Navigate to API Keys section
  4. Create a new API key

4. Start Qdrant Database

Ensure Docker is running, then execute:

docker run -d --name qdrantRagDb -p 6333:6333 -v "$(pwd)/qdrant_storage:/qdrant/storage" qdrant/qdrant

This command:

  • Creates a Qdrant container named qdrantRagDb
  • Maps port 6333 for database access
  • Persists data in ./qdrant_storage directory

Running the Application

You need to run three services simultaneously. Open three separate terminal windows:

Terminal 1: FastAPI Backend

uv run uvicorn main:app --reload

The API will be available at http://127.0.0.1:8000

Terminal 2: Inngest Dev Server

npx inngest-cli@latest dev -u http://127.0.0.1:8000/api/inngest --no-discovery

The Inngest dashboard will be available at http://127.0.0.1:8288

Terminal 3: Streamlit Frontend

streamlit run streamlit_app.py

The Streamlit app will open automatically in your browser at http://localhost:8501

Project Structure

rag-ai-agent/
│
├── main.py                 # FastAPI app with Inngest functions
├── streamlit_app.py        # Streamlit frontend interface
├── vector_db.py            # Qdrant vector database operations
├── data_loader.py          # PDF loading and embedding functions
├── custom_types.py         # Pydantic models for type safety
├── .env                    # Environment variables (create this)
├── requirements.txt        # Python dependencies
├── README.md              # This file
│
├── uploads/               # Uploaded PDF storage (auto-created)
└── qdrant_storage/        # Qdrant database persistence (auto-created)

Usage

1. Upload a PDF Document

  1. Open the Streamlit app in your browser
  2. Click "Browse files" and select a PDF document
  3. The system will automatically:
    • Save the PDF to the uploads/ directory
    • Chunk the document into manageable pieces
    • Generate embeddings using HuggingFace
    • Store vectors in Qdrant database

2. Ask Questions

  1. Enter your question in the text input field
  2. Adjust "How many chunks to retrieve" (default: 5)
  3. Click "Ask"
  4. The system will:
    • Search for relevant document chunks
    • Send context to the LLM
    • Generate an answer based on your documents
    • Display sources used

How It Works

Architecture Overview

User Upload → Streamlit → Inngest Event → FastAPI Function
                                          ↓
                                    PDF Processing
                                          ↓
                                    Chunking (LlamaIndex)
                                          ↓
                                    Embedding (HuggingFace)
                                          ↓
                                    Qdrant Storage

User Query → Streamlit → Inngest Event → FastAPI Function
                                          ↓
                                    Vector Search (Qdrant)
                                          ↓
                                    Context Retrieval
                                          ↓
                                    LLM Generation (Groq)
                                          ↓
                                    Answer Display

Workflow Steps

Ingestion Workflow (rag/ingest_pdf event):

  1. Load PDF using LlamaIndex PDFReader
  2. Split text into chunks (1000 chars, 200 overlap)
  3. Generate embeddings using sentence-transformers
  4. Store in Qdrant with metadata

Query Workflow (rag/query_pdf_ai event):

  1. Embed user question
  2. Search Qdrant for top-k similar chunks
  3. Build context from retrieved chunks
  4. Send to Groq LLM with system prompt
  5. Return answer with sources

Configuration

Vector Database Settings

Edit vector_db.py to customize:

  • url: Qdrant server URL (default: http://localhost:6333)
  • collection: Collection name (default: docs)
  • dim: Embedding dimensions (default: 384 for MiniLM-L6-v2)

Chunking Parameters

Edit data_loader.py:

  • chunk_size: Characters per chunk (default: 1000)
  • chunk_overlap: Overlap between chunks (default: 200)

LLM Settings

Edit main.py in the rag_query_pdf_ai function:

  • model: Groq model (default: openai/gpt-oss-120b)
  • temperature: Response randomness (default: 0.2)
  • top_k: Number of chunks to retrieve (default: 5)

Embedding Model Download

First run may take time to download the sentence-transformers model (~80MB). This is normal and happens once.

API Endpoints

FastAPI Endpoints

  • GET /: Root endpoint
  • POST /api/inngest: Inngest webhook endpoint

Inngest Functions

  • rag/ingest_pdf: Process and ingest PDF documents
  • rag/query_pdf_ai: Query documents and generate answers

Free Resources Used

This project uses entirely free services:

  • Groq: Free tier with generous limits for LLM inference
  • HuggingFace: Free open-source embedding models
  • Qdrant: Free self-hosted vector database
  • Inngest: Free local development server

Performance Tips

  1. Chunk Size: Smaller chunks (500-1000 chars) work better for specific questions
  2. Top-K: Start with 5 chunks, increase if answers lack detail
  3. Temperature: Keep low (0.1-0.3) for factual answers
  4. Embeddings: MiniLM-L6-v2 is fast; use larger models for better accuracy

Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

License

This project is licensed under the MIT License.

Acknowledgments

Support

For issues and questions:

  • Open an issue on GitHub
  • Check existing issues for solutions
  • Review Inngest dashboard for workflow errors

About

A production-ready Retrieval-Augmented Generation (RAG) system that allows you to upload PDF documents, process them into a vector database, and ask questions about their content using AI.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages