Skip to content

Repository files navigation

AI Backend

AI backend platform built with FastAPI, featuring RAG pipelines, vector search, streaming AI responses, and scalable async architecture.


Architecture Overview

┌─────────────────────────────────────────────────────────┐
│ FastAPI App │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Routes │ │ Auth │ │ Health │ │
│ └────┬─────┘ └────┬─────┘ └──────────┘ │
│ │ │ │
│ ┌────▼──────────────▼────┐ │
│ │ Service Layer │ │
│ │ AuthService │ AIService│ │
│ │ FileService │ SubService │
│ └────┬─────────────┬─────┘ │
│ │ │ │
│ ┌────▼────┐ ┌────▼──────────────────┐ │
│ │ Repos │ │ AI Pipeline │ │
│ │ (DB) │ │ Embed → Index → RAG │ │
│ └────┬────┘ └────┬──────────────────┘ │
│ │ │ │
│ ┌────▼────┐ ┌────▼────────┐ ┌───────────────┐ │
│ │PostgreSQL│ │ Qdrant │ │ OpenAI API │ │
│ └─────────┘ └─────────────┘ └───────────────┘ │
└─────────────────────────────────────────────────────────┘
│
┌────▼──────────────────────┐
│ Redis + Celery Workers │
│ Embedding │ Indexing │ GC │
└────────────────────────────┘

Tech Stack

LayerTechnology
FrameworkFastAPI + Uvicorn
DatabasePostgreSQL 16 + SQLAlchemy 2.0 Async
Cache / BrokerRedis 7
Vector DBQdrant
AI ProviderOpenAI (GPT-4o + text-embedding-3-small)
Background TasksCelery
AuthJWT (access + refresh tokens)
ValidationPydantic v2
MigrationsAlembic
TestingPytest + pytest-asyncio + HTTPX
ContainerizationDocker + Docker Compose

Features

Authentication

  • JWT access tokens (30-minute expiry)
  • Refresh token rotation with secure hashing
  • bcrypt password hashing
  • Role-based access control (user / admin)

AI Capabilities

  • Document Chat — RAG-powered Q&A over uploaded documents
  • Resume Analyzer — Structured resume feedback with optional job description matching
  • Code Review — Security, performance, and quality analysis
  • Meeting Summarizer — Transcript summarization with action items
  • Streaming Responses — Real-time SSE token streaming for all AI endpoints

File Processing Pipeline

Upload → Validate → Store → Queue Celery Task
→ Extract Text (PDF/DOCX/TXT/Code)
→ Chunk Text (configurable window + overlap)
→ Generate Embeddings (OpenAI batch)
→ Index to Qdrant
→ Update File Status → Done

Vector Search (RAG)

  • Semantic similarity search via Qdrant cosine distance
  • Per-user + per-file metadata filtering
  • Configurable top-k retrieval with score threshold
  • Context injection into structured prompts

Subscription System

  • Free / Pro / Enterprise tiers
  • Per-month request and token quotas
  • Quota enforcement via dependency injection
  • Stripe-ready schema (customer_id, subscription_id columns)

Background Processing (Celery)

  • embeddings queue — document processing & indexing
  • indexing queue — vector operations
  • cleanup queue — expired token purge, orphaned file cleanup

Project Structure

app/
├── api/v1/ # Thin route handlers
│ ├── auth.py
│ ├── ai.py
│ ├── files.py
│ ├── stream.py
│ └── subscriptions.py
├── ai/ # OpenAI integration & RAG pipeline
│ ├── client.py
│ ├── completions.py
│ ├── embeddings.py
│ └── pipeline.py
├── core/ # Security, exceptions, logging
├── db/ # SQLAlchemy engine & session
├── models/ # ORM models (7 tables)
├── schemas/ # Pydantic v2 request/response schemas
├── repositories/ # Data access layer (no business logic)
├── services/ # Business logic layer
├── tasks/ # Celery workers
├── vector/ # Qdrant client, indexer, retriever
├── streaming/ # SSE helpers
├── middleware/ # Request logging, rate limiting
├── dependencies/ # FastAPI DI (auth, db, quota)
├── utils/ # File extraction, text chunking
└── tests/ # pytest async test suite

Quick Start

Using Docker (recommended)

cp .env.example .env
# Edit .env with your OPENAI_API_KEY and SECRET_KEY
docker compose up --build

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

Local Development

python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
cp .env.example .env
# Fill in DATABASE_URL, REDIS_URL, OPENAI_API_KEY, SECRET_KEY
alembic upgrade head
uvicorn app.main:app --reload

API Reference

Authentication

POST /api/v1/auth/register Register new user
POST /api/v1/auth/login Obtain access + refresh tokens
POST /api/v1/auth/refresh Rotate refresh token

Files

POST /api/v1/files/upload Upload document (PDF/DOCX/TXT/code)
GET /api/v1/files List user's files
DELETE /api/v1/files/{id} Delete file + vectors

AI Endpoints

POST /api/v1/ai/chat General AI chat
POST /api/v1/ai/document-chat RAG chat over uploaded document
POST /api/v1/ai/resume-analyze Resume analysis
POST /api/v1/ai/code-review Code review
POST /api/v1/ai/meeting-summary Meeting transcript summarizer

Streaming (SSE)

POST /api/v1/stream/chat Streaming general chat
POST /api/v1/stream/document-chat Streaming RAG document chat

SSE events: token, done, error

Subscriptions

GET /api/v1/subscriptions/plans Available plans
GET /api/v1/subscriptions/me Current subscription
POST /api/v1/subscriptions/upgrade Upgrade tier
GET /api/v1/subscriptions/usage Current period usage

Health

GET /health Service health check

Environment Variables

VariableDescriptionDefault
DATABASE_URLPostgreSQL async URLrequired
REDIS_URLRedis URLrequired
OPENAI_API_KEYOpenAI secret keyrequired
SECRET_KEYJWT signing secretrequired
QDRANT_URLQdrant HTTP URLhttp://localhost:6333
OPENAI_MODELChat modelgpt-4o
OPENAI_EMBEDDING_MODELEmbedding modeltext-embedding-3-small
ACCESS_TOKEN_EXPIRE_MINUTESAccess token TTL30
REFRESH_TOKEN_EXPIRE_DAYSRefresh token TTL7
MAX_FILE_SIZEMax upload bytes10485760 (10MB)
CHUNK_SIZEEmbedding chunk word count512
CHUNK_OVERLAPChunk overlap words50

Running Tests

# Requires a test PostgreSQL database: ai_saas_test
pytest -v

Database Migrations

# Generate migration after model changes
alembic revision --autogenerate -m "description"# Apply migrations
alembic upgrade head
# Roll back one
alembic downgrade -1

Streaming Example (JavaScript)

constresponse=awaitfetch('/api/v1/stream/chat',{method: 'POST',headers: {'Content-Type': 'application/json','Authorization': `Bearer ${token}`},body: JSON.stringify({message: 'Explain async/await in Python'})});constreader=response.body.getReader();constdecoder=newTextDecoder();while(true){const{ done, value }=awaitreader.read();if(done)break;constlines=decoder.decode(value).split('\n');for(constlineoflines){if(line.startsWith('data:')){constdata=JSON.parse(line.slice(5));if(data.token)process.stdout.write(data.token);}}}

About

No description, website, or topics provided.

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages