diff --git a/docs/reference/backend-architecture.md b/docs/reference/backend-architecture.md new file mode 100644 index 000000000..b56130817 --- /dev/null +++ b/docs/reference/backend-architecture.md @@ -0,0 +1,411 @@ +--- +sidebar_position: 450 +title: "Backend Architecture" +--- + +# Backend Architecture + +The Open WebUI backend is built with **FastAPI** and Python, providing a comprehensive REST API for managing AI conversations, user authentication, file handling, and integrations with various LLM providers. + +## ๐Ÿ—๏ธ Architecture Overview + +The backend follows a modular architecture with clear separation of concerns: + +``` +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ FastAPI Application โ”‚ +โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค +โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ +โ”‚ โ”‚ Routers โ”‚ โ”‚ Middleware โ”‚ โ”‚ Authentication โ”‚ โ”‚ +โ”‚ โ”‚ (API) โ”‚ โ”‚ (CORS, etc) โ”‚ โ”‚ (JWT, OAuth) โ”‚ โ”‚ +โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ +โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค +โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ +โ”‚ โ”‚ Models โ”‚ โ”‚ Utils โ”‚ โ”‚ Services โ”‚ โ”‚ +โ”‚ โ”‚ (Database) โ”‚ โ”‚ (Helpers) โ”‚ โ”‚ (Business โ”‚ โ”‚ +โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ Logic) โ”‚ โ”‚ +โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ +โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค +โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ +โ”‚ โ”‚ Database โ”‚ โ”‚ Redis โ”‚ โ”‚ File Storage โ”‚ โ”‚ +โ”‚ โ”‚ SQLAlchemy โ”‚ โ”‚ (Cache) โ”‚ โ”‚ (Local/ โ”‚ โ”‚ +โ”‚ โ”‚ Peewee โ”‚ โ”‚ โ”‚ โ”‚ Cloud) โ”‚ โ”‚ +โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +``` + +## ๐Ÿ“ Directory Structure + +The backend code is located in the `backend/` directory of the main repository: + +``` +backend/ +โ”œโ”€โ”€ open_webui/ # Main application package +โ”‚ โ”œโ”€โ”€ main.py # FastAPI application entry point +โ”‚ โ”œโ”€โ”€ config.py # Configuration management & environment variables +โ”‚ โ”œโ”€โ”€ constants.py # Application constants +โ”‚ โ”œโ”€โ”€ env.py # Environment variable loading +โ”‚ โ”œโ”€โ”€ functions.py # Python function tools system +โ”‚ โ”œโ”€โ”€ tasks.py # Background task definitions +โ”‚ โ”‚ +โ”‚ โ”œโ”€โ”€ routers/ # API route handlers +โ”‚ โ”‚ โ”œโ”€โ”€ auths.py # Authentication & authorization +โ”‚ โ”‚ โ”œโ”€โ”€ chats.py # Chat management +โ”‚ โ”‚ โ”œโ”€โ”€ users.py # User management +โ”‚ โ”‚ โ”œโ”€โ”€ models.py # LLM model management +โ”‚ โ”‚ โ”œโ”€โ”€ ollama.py # Ollama integration +โ”‚ โ”‚ โ”œโ”€โ”€ openai.py # OpenAI-compatible API +โ”‚ โ”‚ โ”œโ”€โ”€ files.py # File upload & management +โ”‚ โ”‚ โ”œโ”€โ”€ knowledge.py # Knowledge base / RAG +โ”‚ โ”‚ โ”œโ”€โ”€ retrieval.py # Document retrieval +โ”‚ โ”‚ โ”œโ”€โ”€ images.py # Image generation (DALL-E, ComfyUI) +โ”‚ โ”‚ โ”œโ”€โ”€ audio.py # Speech-to-text & text-to-speech +โ”‚ โ”‚ โ”œโ”€โ”€ functions.py # Custom function tools +โ”‚ โ”‚ โ”œโ”€โ”€ tools.py # Tool management +โ”‚ โ”‚ โ”œโ”€โ”€ skills.py # Skill system +โ”‚ โ”‚ โ”œโ”€โ”€ prompts.py # Prompt templates +โ”‚ โ”‚ โ”œโ”€โ”€ memories.py # Conversation memory +โ”‚ โ”‚ โ”œโ”€โ”€ notes.py # User notes +โ”‚ โ”‚ โ”œโ”€โ”€ folders.py # Folder organization +โ”‚ โ”‚ โ”œโ”€โ”€ groups.py # User groups & permissions +โ”‚ โ”‚ โ”œโ”€โ”€ configs.py # System configuration +โ”‚ โ”‚ โ”œโ”€โ”€ channels.py # Communication channels +โ”‚ โ”‚ โ”œโ”€โ”€ evaluations.py # Model evaluations +โ”‚ โ”‚ โ”œโ”€โ”€ analytics.py # Usage analytics +โ”‚ โ”‚ โ”œโ”€โ”€ pipelines.py # Pipeline integrations +โ”‚ โ”‚ โ”œโ”€โ”€ scim.py # SCIM 2.0 provisioning +โ”‚ โ”‚ โ””โ”€โ”€ tasks.py # Task management +โ”‚ โ”‚ +โ”‚ โ”œโ”€โ”€ models/ # Database models (ORM) +โ”‚ โ”‚ โ”œโ”€โ”€ auths.py # Authentication models +โ”‚ โ”‚ โ”œโ”€โ”€ users.py # User models +โ”‚ โ”‚ โ”œโ”€โ”€ chats.py # Chat models +โ”‚ โ”‚ โ”œโ”€โ”€ chat_messages.py # Message models +โ”‚ โ”‚ โ”œโ”€โ”€ files.py # File models +โ”‚ โ”‚ โ”œโ”€โ”€ functions.py # Function models +โ”‚ โ”‚ โ”œโ”€โ”€ tools.py # Tool models +โ”‚ โ”‚ โ”œโ”€โ”€ prompts.py # Prompt models +โ”‚ โ”‚ โ”œโ”€โ”€ knowledge.py # Knowledge base models +โ”‚ โ”‚ โ”œโ”€โ”€ memories.py # Memory models +โ”‚ โ”‚ โ”œโ”€โ”€ groups.py # Group models +โ”‚ โ”‚ โ””โ”€โ”€ ... # Other domain models +โ”‚ โ”‚ +โ”‚ โ”œโ”€โ”€ utils/ # Utility functions & helpers +โ”‚ โ”‚ โ”œโ”€โ”€ auth.py # Authentication utilities +โ”‚ โ”‚ โ”œโ”€โ”€ access_control.py # Permission checking +โ”‚ โ”‚ โ”œโ”€โ”€ middleware.py # Custom middleware +โ”‚ โ”‚ โ”œโ”€โ”€ rate_limit.py # Rate limiting +โ”‚ โ”‚ โ”œโ”€โ”€ embeddings.py # Text embeddings +โ”‚ โ”‚ โ”œโ”€โ”€ chat.py # Chat utilities +โ”‚ โ”‚ โ”œโ”€โ”€ files.py # File handling utilities +โ”‚ โ”‚ โ”œโ”€โ”€ webhook.py # Webhook utilities +โ”‚ โ”‚ โ”œโ”€โ”€ redis.py # Redis utilities +โ”‚ โ”‚ โ”œโ”€โ”€ logger.py # Logging utilities +โ”‚ โ”‚ โ”œโ”€โ”€ security_headers.py # Security headers +โ”‚ โ”‚ โ”œโ”€โ”€ telemetry/ # OpenTelemetry integration +โ”‚ โ”‚ โ”œโ”€โ”€ mcp/ # Model Context Protocol +โ”‚ โ”‚ โ””โ”€โ”€ images/ # Image processing utilities +โ”‚ โ”‚ +โ”‚ โ”œโ”€โ”€ retrieval/ # RAG (Retrieval Augmented Generation) +โ”‚ โ”‚ โ”œโ”€โ”€ vector/ # Vector database integrations +โ”‚ โ”‚ โ”œโ”€โ”€ loaders/ # Document loaders +โ”‚ โ”‚ โ””โ”€โ”€ web/ # Web search integrations +โ”‚ โ”‚ +โ”‚ โ”œโ”€โ”€ storage/ # Storage backends +โ”‚ โ”‚ โ”œโ”€โ”€ local.py # Local file storage +โ”‚ โ”‚ โ”œโ”€โ”€ s3.py # S3-compatible storage +โ”‚ โ”‚ โ””โ”€โ”€ ... # Other cloud storage providers +โ”‚ โ”‚ +โ”‚ โ”œโ”€โ”€ socket/ # WebSocket handlers +โ”‚ โ”‚ โ””โ”€โ”€ main.py # Real-time communication +โ”‚ โ”‚ +โ”‚ โ”œโ”€โ”€ internal/ # Internal modules +โ”‚ โ”‚ โ””โ”€โ”€ db.py # Database connection & session management +โ”‚ โ”‚ +โ”‚ โ”œโ”€โ”€ migrations/ # Alembic database migrations +โ”‚ โ”‚ โ””โ”€โ”€ versions/ # Migration scripts +โ”‚ โ”‚ +โ”‚ โ””โ”€โ”€ test/ # Test suite +โ”‚ โ””โ”€โ”€ ... # Unit and integration tests +โ”‚ +โ”œโ”€โ”€ requirements.txt # Python dependencies +โ”œโ”€โ”€ start.sh # Linux/Mac startup script +โ”œโ”€โ”€ start_windows.bat # Windows startup script +โ””โ”€โ”€ dev.sh # Development mode startup +``` + +:::note +The `alembic.ini` configuration file is located inside the `open_webui/` directory, not at the backend root. +::: + +## ๐Ÿ”‘ Key Components + +### 1. FastAPI Application (`main.py`) +The core application bootstraps: +- CORS middleware for frontend communication +- API routers for all endpoints +- Static file serving for the frontend +- WebSocket support for real-time features +- Database initialization and migrations +- Background task scheduling + +### 2. Routers (`routers/`) +Each router handles a specific domain: +- **Authentication** (`auths.py`): User registration, login, JWT tokens, OAuth +- **LLM Integration** (`ollama.py`, `openai.py`): Proxy to LLM providers +- **Chat Management** (`chats.py`): Conversation history and management +- **RAG System** (`knowledge.py`, `retrieval.py`): Document upload, embedding, and retrieval +- **File Handling** (`files.py`): Upload, storage, and retrieval +- **User Management** (`users.py`, `groups.py`): Users, roles, permissions + +### 3. Database Models (`models/`) +Uses **SQLAlchemy** and **Peewee** for ORM: +- Schema definitions for all entities +- Relationships between models +- Database migrations via Alembic + +### 4. Configuration (`config.py`) +Centralized configuration management: +- Environment variable loading +- Database connection settings +- LLM provider configurations +- Feature flags and system settings +- Redis connection for caching and sessions + +### 5. Utilities (`utils/`) +Reusable helper functions: +- Authentication & authorization helpers +- Rate limiting and security +- File processing +- Embeddings generation +- Webhook handling + +### 6. RAG System (`retrieval/`) +Retrieval Augmented Generation features: +- **Vector Databases**: ChromaDB, PGVector, Qdrant, Milvus, etc. +- **Document Loaders**: PDF, Word, text, web pages +- **Embedding Models**: Sentence transformers, OpenAI embeddings +- **Web Search**: Integration with multiple search providers + +## ๐Ÿš€ Development Setup + +### Prerequisites +- **Python 3.11+** (Python 3.13 recommended) +- **pip** or **uv** for package management +- **Redis** (optional, for caching and sessions) +- **PostgreSQL** or **SQLite** for database + +### Installation + +1. **Clone the repository:** + ```bash + git clone https://github.com/open-webui/open-webui.git + cd open-webui + ``` + +2. **Navigate to backend directory:** + ```bash + cd backend + ``` + +3. **Create virtual environment (recommended):** + ```bash + python -m venv venv + source venv/bin/activate # On Windows: venv\Scripts\activate + ``` + +4. **Install dependencies:** + ```bash + pip install -r requirements.txt + ``` + +5. **Set up environment variables:** + Create a `.env` file in the project root (not in backend/): + ```env + # Database + DATABASE_URL=sqlite:///backend/data/webui.db + + # Ollama Integration + OLLAMA_BASE_URL=http://localhost:11434 + + # OpenAI API (optional) + OPENAI_API_KEY=your_api_key_here + + # CORS + CORS_ALLOW_ORIGIN=http://localhost:5173 + ``` + +6. **Run database migrations:** + ```bash + # Migrations run automatically on first startup + # Or run manually from the backend directory: + cd open_webui + alembic upgrade head + ``` + +7. **Start the development server:** + ```bash + # Linux/Mac + ./dev.sh + + # Windows + start_windows.bat + + # Or manually + uvicorn open_webui.main:app --reload --host 0.0.0.0 --port 8080 + ``` + +The backend API will be available at `http://localhost:8080` + +### Development Mode + +For development with hot-reload: +```bash +# Set CORS for local frontend development +export CORS_ALLOW_ORIGIN="http://localhost:5173" +export PORT=8080 + +# Run with reload +uvicorn open_webui.main:app --reload --host 0.0.0.0 --port 8080 +``` + +## ๐Ÿงช Testing + +Run the test suite: +```bash +pytest open_webui/test/ +``` + +Run with coverage: +```bash +pytest --cov=open_webui --cov-report=html +``` + +## ๐Ÿ“ก API Documentation + +Once the server is running, interactive API documentation is available: +- **Swagger UI**: http://localhost:8080/docs +- **ReDoc**: http://localhost:8080/redoc +- **OpenAPI JSON**: http://localhost:8080/openapi.json + +See the [API Endpoints](/reference/api-endpoints) page for more details. + +## ๐Ÿ” Authentication & Authorization + +The backend implements: +- **JWT tokens** for stateless authentication +- **OAuth 2.0** for third-party authentication (Google, GitHub, etc.) +- **Role-Based Access Control (RBAC)** for permissions +- **Group-based permissions** for team management +- **SCIM 2.0** for enterprise user provisioning + +## ๐Ÿ—„๏ธ Database + +The backend supports multiple database backends: +- **SQLite** (default, for development and single-user) +- **PostgreSQL** (recommended for production) +- **MySQL/MariaDB** (supported) + +Migrations are managed with **Alembic**. + +## ๐Ÿ“ฆ Dependencies + +Key dependencies include: +- **FastAPI** - Web framework +- **SQLAlchemy** - ORM for database operations +- **Peewee** - Alternative ORM for some models +- **Pydantic** - Data validation +- **LangChain** - LLM orchestration and RAG +- **ChromaDB** - Vector database for embeddings +- **Sentence Transformers** - Text embeddings +- **OpenAI** - OpenAI API client +- **Redis** - Caching and session management + +See `requirements.txt` for the complete list. + +## ๐ŸŒ Environment Variables + +Key environment variables: + +| Variable | Description | Default | +|----------|-------------|---------| +| `DATABASE_URL` | Database connection string | `sqlite:///./backend/data/webui.db` | +| `OLLAMA_BASE_URL` | Ollama server URL | `http://localhost:11434` | +| `OPENAI_API_KEY` | OpenAI API key | - | +| `WEBUI_AUTH` | Enable authentication | `True` | +| `CORS_ALLOW_ORIGIN` | Allowed CORS origins | `*` | +| `REDIS_URL` | Redis connection URL | - | +| `PORT` | Server port | `8080` | + +For a complete list, see the [Environment Configuration](/reference/env-configuration) page and the `config.py` file in the repository. + +## ๐Ÿ”Œ LLM Provider Integrations + +The backend supports multiple LLM providers: +- **Ollama** - Local LLM runtime +- **OpenAI** - GPT models +- **Anthropic** - Claude models +- **Google** - Gemini models +- **Any OpenAI-compatible API** (LMStudio, LocalAI, etc.) + +## ๐Ÿ“ Adding New Features + +### Adding a New Router + +1. Create a new file in `routers/`: + ```python + from fastapi import APIRouter, Depends + from open_webui.utils.auth import get_current_user + + router = APIRouter() + + @router.get("/my-endpoint") + async def my_endpoint(user=Depends(get_current_user)): + return {"message": "Hello from my endpoint"} + ``` + +2. Register the router in `main.py`: + ```python + from open_webui.routers import my_router + + app.include_router(my_router.router, prefix="/api/my-feature", tags=["my-feature"]) + ``` + +### Adding a New Database Model + +1. Create a model in `models/`: + ```python + from sqlalchemy import Column, String, Integer + from open_webui.internal.db import Base + + class MyModel(Base): + __tablename__ = "my_table" + + id = Column(Integer, primary_key=True) + name = Column(String) + ``` + +2. Create a migration: + ```bash + # From backend/open_webui directory: + cd open_webui + alembic revision --autogenerate -m "Add my_table" + alembic upgrade head + ``` + +## ๐Ÿค Contributing + +Please read the [Contributing Guide](/contributing) before submitting pull requests. + +Key guidelines: +- Follow PEP 8 style guide +- Add tests for new features +- Update documentation +- Ensure all tests pass +- Target the `dev` branch for PRs + +## ๐Ÿ†˜ Support + +- **Documentation**: https://docs.openwebui.com +- **Discord**: https://discord.gg/5rJgQTnV4s +- **GitHub Issues**: https://github.com/open-webui/open-webui/issues