Skip to content

Repository files navigation

RAG Python SaaS

A Retrieval-Augmented Generation (RAG) SaaS application with FastAPI backend, multi-tenant architecture, and an embeddable chat widget.

Features

  • 🔐 Authentication - JWT-based auth with user roles (admin/user)
  • 📄 Document Management - Upload and process documents per user
  • 🤖 RAG Chat - AI-powered chat with context from your documents
  • 💳 Subscriptions - Stripe integration for paid plans
  • 💾 Multi-tenant - Each user has isolated data storage
  • 💬 Embeddable Widget - Chat widget for any website

Tech Stack

  • Backend: FastAPI, SQLAlchemy, PostgreSQL/SQLite
  • AI: LangChain, Ollama, Pinecone
  • Auth: fastapi-users (JWT)
  • Payments: Stripe
  • Frontend Widget: Vanilla JS Web Components + Shadow DOM

Quick Start

Prerequisites

  • Python 3.13+
  • Node.js 18+ (for widget build)
  • Ollama (for local LLM)
  • Pinecone account (optional, for vector storage)

Backend Setup

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Linux/Mac
# venv\Scripts\activate  # Windows

# Install dependencies
pip install -r requirements.txt

# Set up environment variables
cp .env.example .env
# Edit .env with your API keys

# Run the server
python main.py

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

Frontend Widget Setup

cd widget

# Install dependencies
npm install

# Build for production
npm run build

# The built files will be in dist/

API Endpoints

Authentication

Method Endpoint Description
POST /api/auth/register Register new user
POST /api/auth/login Login and get JWT
GET /api/auth/me Get current user

Storage

Method Endpoint Description
POST /storage/upload Upload file
GET /storage/files List user files
DELETE /storage/files/{filename} Delete file

RAG

Method Endpoint Description
POST /rag/query Query RAG system
POST /rag/documents Process documents
GET /rag/stats Get RAG statistics

Payments

Method Endpoint Description
GET /payments/subscription Get subscription status
POST /payments/checkout Create checkout session
POST /payments/portal Open customer portal

Widget

Method Endpoint Description
GET /widget/config Get widget configuration
GET /widget/theme Get theme for premium users

Embeddable Chat Widget

Integration

Add the widget to any website with just a few lines of code:

<!-- Add the script -->
<script src="https://your-domain.com/widget/rag-chat-widget.js"></script>

<!-- Add the widget element -->
<rag-chat-widget 
    site-id="your-site-id"
    api-url="https://your-api.com">
</rag-chat-widget>

Configuration Options

Attribute Required Description
site-id Yes Your site identifier
api-url No Backend API URL (defaults to current domain)
position No bottom-right (default) or bottom-left
theme No Theme name for premium users

Theme Customization (Premium Users)

Premium users can customize colors by calling the theme endpoint:

// Fetch custom theme
fetch('/widget/theme?user_id=123')

Returns CSS variables that can be applied to the widget.

Building the Widget

cd widget
npm install
npm run build

Output files:

  • dist/rag-chat-widget.js - ES module
  • dist/rag-chat-widget.umd.cjs - UMD bundle (for CDN)

Project Structure

rag-python/
├── app/
│   ├── auth/           # Authentication (fastapi-users)
│   ├── db/             # Database models
│   ├── payments/       # Stripe integration
│   ├── services/       # RAG service
│   ├── storage/        # File storage
│   ├── widget/         # Widget backend endpoints
│   └── app.py          # Main FastAPI app
├── widget/
│   ├── src/
│   │   ├── components/ # UI components
│   │   ├── services/   # API client
│   │   └── styles/     # CSS
│   └── vite.config.js  # Build config
├── alembic/            # Database migrations
├── tests/              # Test suite
└── main.py             # Entry point

Subscription Tiers

Feature Free Premium
Max Documents 10 Unlimited
Max File Size 10 MB 100 MB
Max Vectors 1,000 Unlimited
RAG Queries/Day 50 Unlimited
Custom Theme

Development

Running Tests

pytest tests/ -v

Code Quality

# Format with black
black .

# Lint with flake8
flake8 .

# Type check with mypy
mypy .

License

MIT License

About

another rag in python

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages