Skip to content

pratiksontakke/redagent-backend

 
 

Repository files navigation

RedAgent Backend: Multi-Tenant RAG-as-a-Service Platform

Live Project: https://redagent.dev

RedAgent is a production-grade, multi-tenant RAG (Retrieval-Augmented Generation) platform designed as a scalable SaaS product. It provides organizations with a secure, conversational AI to interact with their private knowledge bases, transforming internal documentation into an intelligent, queryable resource.

► The Problem It Solves

Many companies possess vast amounts of information locked away in internal documents (PDFs, DOCX, etc.). Retrieving specific, timely information is often inefficient and time-consuming. RedAgent solves this by ingesting this unstructured data and providing a secure, intuitive chatbot interface for users to get instant, context-aware answers.

► Key Features

  • Multi-Tenant Architecture: Guarantees complete data isolation and security for each client organization using a schema-per-tenant model in PostgreSQL.
  • Role-Based Access Control (RBAC): Fine-grained permissions for Client Admins and Users, restricting access to document categories and platform features.
  • End-to-End RAG Pipeline: A complete workflow for document management:
    • Upload: Securely upload various document formats (PDF, DOCX, TXT).
    • Ingest & Vectorize: Documents are automatically processed, chunked, and converted into vector embeddings using pgvector.
    • Retrieve & Generate: Users receive accurate, context-aware answers from an AI, with responses grounded in the provided documents.
  • Conversational AI Chat: An intuitive chat interface for users to query their organization's knowledge base in natural language.
  • Automated Onboarding: A seamless, automated system for provisioning new tenants, including schema creation and migration.
  • Scalable Cloud Infrastructure: Designed and deployed on AWS to ensure high availability, scalability, and security.

► Architectural Highlights

This platform was architected with security and scalability as first-class citizens.

  1. Schema-per-Tenant Isolation: Each client organization operates within its own dedicated PostgreSQL schema. This prevents any possibility of data leakage between tenants and allows for independent scaling, backup, and extension for each client.

  2. Dynamic Schema Management: New tenants are onboarded automatically. The application programmatically creates a new schema and runs all necessary table migrations, making the platform horizontally scalable with minimal operational overhead.

  3. Asynchronous Backend: Built with FastAPI and asyncpg, the entire backend is asynchronous, ensuring high performance and throughput for concurrent API requests and data processing tasks.

  4. Decoupled RAG Services: The services for document ingestion, vectorization, and querying are logically separated, allowing for independent maintenance and scaling of the AI pipeline.

► Technology Stack

Category Technologies
Backend Python, FastAPI
Database PostgreSQL, pgvector, SQLAlchemy (Async)
AI / ML LangChain, OpenAI API, Google Generative AI API
Cloud & DevOps AWS, Docker
Authentication JWT (JSON Web Tokens), Passlib (for password hashing)

► Getting Started

Prerequisites

  • Python 3.10+
  • PostgreSQL 12+ with the pgvector and uuid-ossp extensions enabled.
  • An account with OpenAI or Google for Generative AI API keys.
  • Docker (Recommended for easy database setup).

1. Clone the Repository

git clone https://github.com/pratiksontakke/redagent-backend.git
cd redagent-backend

2. Set Up the Environment

Create a virtual environment and install the required dependencies.

python -m venv venv
source venv/bin/activate  # On Windows, use `venv\Scripts\activate`
pip install -r requirements.txt

3. Configure Environment Variables

Create a .env file in the project root by copying the example file.

cp .env.example .env

Now, edit the .env file with your specific configurations:

# .env

# Database Connection
DATABASE_URL=postgresql+asyncpg://USER:PASSWORD@HOST:PORT/DATABASE_NAME

# JWT Secret Key for Authentication
JWT_SECRET_KEY=your_strong_secret_key_here

# AI Provider API Keys (use at least one)
OPENAI_API_KEY=sk-...
GOOGLE_API_KEY=AIza...

# SMTP Configuration for sending OTP emails
SMTP_HOST=smtp.example.com
SMTP_PORT=587
SMTP_USER=your-email@example.com
SMTP_PASS=your-email-password
FROM_EMAIL=no-reply@redagent.dev

4. Set Up the Database

The application uses a public schema for global data and creates tenant-specific schemas on the fly.

  1. Connect to PostgreSQL as a superuser:

    psql -U postgres
  2. Create the database and enable extensions:

    CREATE DATABASE crm_rag_db;
    \c crm_rag_db;
    CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
    CREATE EXTENSION IF NOT EXISTS "pgvector";
  3. Run the public schema script: The application will create the public tables automatically on startup. No manual script run is needed.

5. Run the Application

Use Uvicorn to run the FastAPI server.

uvicorn api.main:app --host 0.0.0.0 --port 8000 --reload

The application will be available at http://localhost:8000. The interactive API documentation (Swagger UI) will be at http://localhost:8000/docs.

► Running Tests

The project includes integration tests to verify key functionalities like the RAG pipeline.

Note: Ensure the FastAPI server is running before executing the tests. You will also need a valid JWT token from a logged-in user.

  1. Update the JWT_TOKEN in test_rag_complete.py and test_upload.py with a valid token.
  2. Run the tests:
    python test_rag_complete.py
    python test_upload.py

► API Endpoints

The API is organized by features. Key routers include:

  • /api/auth: User signup, login, and organization creation.
  • /api/users: CRUD operations for tenant users (Admin only).
  • /api/categories: Management of document categories (Admin only).
  • /api/kb: Knowledge Base operations, including document upload, status checks, and RAG queries/chat.
  • /admin/...: Super-admin-only routes for platform management.

For a complete and interactive list of all endpoints, please run the application and visit http://localhost:8000/docs.

► Author

Pratik Sontakke

About

RedAgent is a production-grade, multi-tenant RAG (Retrieval-Augmented Generation) platform designed as a scalable SaaS product. It provides organizations with a secure, conversational AI to interact with their private knowledge bases, transforming internal documentation into an intelligent, queryable resource.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 91.3%
  • PLpgSQL 8.7%