Skip to content

talhamasood0000/multi-tenant-rag

Repository files navigation

Advanced RAG - Django Project

A production-ready Django application with PostgreSQL, JWT authentication (SimpleJWT), Celery for background tasks, and RAG (Retrieval Augmented Generation) capabilities using Google Cloud Storage and Qdrant vector database.

🌟 Features

  • JWT Authentication - Secure authentication using djangorestframework-simplejwt
  • PostgreSQL Database - Robust data persistence with Django ORM
  • GCS Presigned URLs - Direct client-to-GCS uploads (no server bandwidth)
  • Celery Background Tasks - Async document processing and RAG response generation
  • Django REST Framework - RESTful API with ModelViewSets
  • Multi-tenant Architecture - Organization-based data isolation
  • Docker Compose - Complete orchestration of all services
  • RAG System Integration - Vector embeddings and semantic search with Qdrant

🏗️ Architecture

┌──────────────┐         ┌──────────────┐         ┌──────────────┐
│  Django App  │  ═══>   │  GCP Storage │  ═══>   │   Celery     │
│  (REST API)  │         │  (Documents) │         │   Worker     │
└──────────────┘         └──────────────┘         └──────────────┘
       │                        │                         │
       │                        │                         │
       └────────────────────────┴─────────────────────────┘
                               │
                    ┌──────────▼──────────┐
                    │   PostgreSQL DB     │
                    │   (Shared State)    │
                    └─────────────────────┘
                               │
                    ┌──────────▼──────────┐
                    │    Qdrant Vector    │
                    │      Database       │
                    └─────────────────────┘

📁 Project Structure

advanced-rag/
├── config/                  # Django project configuration
├── users/                   # User authentication app
├── documents/               # Document management app
├── chats/                   # Chat and RAG app
├── common/                  # Shared utilities (Qdrant client)
├── manage.py               # Django management script
├── requirements.txt        # Python dependencies
├── docker-compose.yml      # Docker orchestration
├── Makefile               # Convenience commands
└── .env.example           # Environment template

🚀 Quick Start

Prerequisites

  • Python 3.12+
  • PostgreSQL
  • Redis
  • Docker & Docker Compose (recommended)
  • GCP account with Cloud Storage enabled
  • Qdrant (optional, included in docker-compose)

1. Environment Setup

# Copy environment template
cp .env.example .env

# Edit .env with your configuration
nano .env

Required environment variables:

SECRET_KEY=your-secret-key
DB_NAME=advanced_rag
DB_USER=postgres
DB_PASSWORD=password
DB_HOST=localhost
DB_PORT=5432
GCS_BUCKET_NAME=your-bucket-name
GCS_PROJECT_ID=your-project-id
GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json
CELERY_BROKER_URL=redis://localhost:6379/0

2. Using Docker (Recommended)

# Start all services
docker-compose up -d

# Run migrations
docker-compose exec web python3 manage.py migrate

# Create superuser
docker-compose exec web python3 manage.py createsuperuser

# View logs
docker-compose logs -f

Services will start on:

3. Local Development

# Install dependencies
make install

# Run migrations
make makemigrations
make migrate

# Create superuser
make superuser

# Start services in separate terminals:

# Terminal 1: Django server
make run

# Terminal 2: Celery worker
make celery

# Terminal 3: Celery beat (optional)
make celery-beat

🔌 API Endpoints

Authentication (/api/auth/)

  • POST /api/auth/users/ - Register new user
  • POST /api/auth/login/ - Login and get JWT token
  • POST /api/auth/token/refresh/ - Refresh JWT token
  • GET /api/auth/users/me/ - Get current user info
  • PUT /api/auth/users/me/profile/ - Update user profile

Documents (/api/documents/)

  • GET /api/documents/ - List user documents
  • POST /api/documents/presign-upload/ - Get presigned upload URL
  • POST /api/documents/{id}/process/ - Trigger document processing
  • GET /api/documents/{id}/download/ - Get presigned download URL
  • DELETE /api/documents/{id}/ - Delete document

Chats (/api/chats/)

  • GET /api/chats/chats/ - List user chats
  • POST /api/chats/chats/ - Create new chat
  • POST /api/chats/chats/{id}/messages/send/ - Send message and get RAG response
  • DELETE /api/chats/chats/{id}/ - Delete chat

📚 API Usage Examples

Register User

curl -X POST http://localhost:8000/api/auth/users/ \
  -H "Content-Type: application/json" \
  -d '{
    "email": "[email protected]",
    "password": "securepassword123",
    "password_confirm": "securepassword123"
  }'

Login

curl -X POST http://localhost:8000/api/auth/login/ \
  -H "Content-Type: application/json" \
  -d '{
    "email": "[email protected]",
    "password": "securepassword123"
  }'

Request Presigned Upload URL

TOKEN="your-jwt-token"

curl -X POST http://localhost:8000/api/documents/presign-upload/ \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "filename": "document.pdf",
    "file_type": "application/pdf",
    "file_size": 1024000
  }'

Upload File to GCS

# Use the presigned URL from the previous response
curl -X PUT "$PRESIGNED_URL" \
  -H "Content-Type: application/pdf" \
  --data-binary @document.pdf

Trigger Document Processing

curl -X POST http://localhost:8000/api/documents/{id}/process/ \
  -H "Authorization: Bearer $TOKEN"

Send Chat Message

curl -X POST http://localhost:8000/api/chats/chats/{id}/messages/send/ \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "content": "What is the main topic of the document?"
  }'

🛠️ Development Commands

# Database
make migrate              # Run migrations
make makemigrations       # Create migrations
make db-reset            # Reset database

# Server
make run                 # Start Django server
make celery              # Start Celery worker
make celery-beat         # Start Celery beat

# Docker
make docker-up           # Start all services
make docker-down         # Stop all services
make docker-logs         # View logs
make docker-rebuild      # Rebuild and start

# Cleanup
make clean               # Remove Python cache files

# Testing
make test                # Run tests

🔐 Security Features

  • JWT token-based authentication with SimpleJWT
  • Django's built-in password hashing
  • Environment-based secrets management
  • Multi-tenant data isolation (organization-based)
  • CORS configuration
  • SQL injection protection via Django ORM
  • Presigned URLs for secure file uploads/downloads

🧪 Testing the System

End-to-End Test

# 1. Register user
TOKEN=$(curl -X POST http://localhost:8000/api/auth/users/ \
  -H "Content-Type: application/json" \
  -d '{"email":"[email protected]","password":"test123","password_confirm":"test123"}' \
  | jq -r '.access')

# 2. Request presigned URL
RESPONSE=$(curl -X POST http://localhost:8000/api/documents/presign-upload/ \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"filename":"test.pdf","file_type":"application/pdf"}')

UPLOAD_URL=$(echo $RESPONSE | jq -r '.upload_url')
DOC_ID=$(echo $RESPONSE | jq -r '.document_id')

# 3. Upload file to GCS
curl -X PUT "$UPLOAD_URL" \
  -H "Content-Type: application/pdf" \
  --data-binary @test.pdf

# 4. Trigger processing
curl -X POST http://localhost:8000/api/documents/$DOC_ID/process/ \
  -H "Authorization: Bearer $TOKEN"

# 5. Check status
curl -X GET http://localhost:8000/api/documents/$DOC_ID/ \
  -H "Authorization: Bearer $TOKEN"

📊 Monitoring

Key Metrics

  • Document processing time
  • Celery task queue depth
  • RAG response generation time
  • GCS storage usage
  • Database query performance

Logs

  • Django application logs
  • Celery worker logs
  • GCS client logs
  • Qdrant client logs

🚀 Deployment

Production Checklist

  • Set DEBUG=False
  • Configure proper SECRET_KEY
  • Set up production database (PostgreSQL)
  • Configure GCS with proper IAM roles
  • Set up Redis for Celery
  • Configure HTTPS/SSL
  • Set up static file serving (Whitenoise/CDN)
  • Configure monitoring and logging
  • Set up database backups
  • Configure rate limiting
  • Set up Celery with proper concurrency

🐛 Troubleshooting

Document Processing Issues

  • Check Celery worker logs: docker-compose logs celery
  • Verify GCS credentials
  • Check document status in admin panel
  • Verify Qdrant is running

Authentication Issues

  • Check JWT token expiration
  • Verify user is active
  • Check CORS configuration in settings

Database Issues

  • Run migrations: make migrate
  • Check PostgreSQL connection
  • Verify database credentials in .env

📝 Migration from FastAPI

This project was converted from FastAPI to Django:

  • FastAPI routes → Django REST Framework ViewSets
  • Pydantic models → DRF Serializers
  • SQLAlchemy models → Django ORM models
  • AWS S3 → Google Cloud Storage
  • Background processor service → Celery tasks
  • FastAPI JWT → djangorestframework-simplejwt

🤝 Contributing

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

📄 License

MIT License

📞 Support

For detailed documentation, see:


Built with Django, PostgreSQL, Celery, GCS, and Qdrant

About

This repo contains the code for multi-tenant Rag applicaiton

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published