LightRAG/starter/docker-compose.yml
Raphael MANSUY fe9b8ec02a
tests: stabilize integration tests + skip external services; fix multi-tenant API behavior and idempotency (#4)
* feat: Implement multi-tenant architecture with tenant and knowledge base models

- Added data models for tenants, knowledge bases, and related configurations.
- Introduced role and permission management for users in the multi-tenant system.
- Created a service layer for managing tenants and knowledge bases, including CRUD operations.
- Developed a tenant-aware instance manager for LightRAG with caching and isolation features.
- Added a migration script to transition existing workspace-based deployments to the new multi-tenant architecture.

* chore: ignore lightrag/api/webui/assets/ directory

* chore: stop tracking lightrag/api/webui/assets (ignore in .gitignore)

* feat: Initialize LightRAG Multi-Tenant Stack with PostgreSQL

- Added README.md for project overview, setup instructions, and architecture details.
- Created docker-compose.yml to define services: PostgreSQL, Redis, LightRAG API, and Web UI.
- Introduced env.example for environment variable configuration.
- Implemented init-postgres.sql for PostgreSQL schema initialization with multi-tenant support.
- Added reproduce_issue.py for testing default tenant access via API.

* feat: Enhance TenantSelector and update related components for improved multi-tenant support

* feat: Enhance testing capabilities and update documentation

- Updated Makefile to include new test commands for various modes (compatibility, isolation, multi-tenant, security, coverage, and dry-run).
- Modified API health check endpoint in Makefile to reflect new port configuration.
- Updated QUICK_START.md and README.md to reflect changes in service URLs and ports.
- Added environment variables for testing modes in env.example.
- Introduced run_all_tests.sh script to automate testing across different modes.
- Created conftest.py for pytest configuration, including database fixtures and mock services.
- Implemented database helper functions for streamlined database operations in tests.
- Added test collection hooks to skip tests based on the current MULTITENANT_MODE.

* feat: Implement multi-tenant support with demo mode enabled by default

- Added multi-tenant configuration to the environment and Docker setup.
- Created pre-configured demo tenants (acme-corp and techstart) for testing.
- Updated API endpoints to support tenant-specific data access.
- Enhanced Makefile commands for better service management and database operations.
- Introduced user-tenant membership system with role-based access control.
- Added comprehensive documentation for multi-tenant setup and usage.
- Fixed issues with document visibility in multi-tenant environments.
- Implemented necessary database migrations for user memberships and legacy support.

* feat(audit): Add final audit report for multi-tenant implementation

- Documented overall assessment, architecture overview, test results, security findings, and recommendations.
- Included detailed findings on critical security issues and architectural concerns.

fix(security): Implement security fixes based on audit findings

- Removed global RAG fallback and enforced strict tenant context.
- Configured super-admin access and required user authentication for tenant access.
- Cleared localStorage on logout and improved error handling in WebUI.

chore(logs): Create task logs for audit and security fixes implementation

- Documented actions, decisions, and next steps for both audit and security fixes.
- Summarized test results and remaining recommendations.

chore(scripts): Enhance development stack management scripts

- Added scripts for cleaning, starting, and stopping the development stack.
- Improved output messages and ensured graceful shutdown of services.

feat(starter): Initialize PostgreSQL with AGE extension support

- Created initialization scripts for PostgreSQL extensions including uuid-ossp, vector, and AGE.
- Ensured successful installation and verification of extensions.

* feat: Implement auto-select for first tenant and KB on initial load in WebUI

- Removed WEBUI_INITIAL_STATE_FIX.md as the issue is resolved.
- Added useTenantInitialization hook to automatically select the first available tenant and KB on app load.
- Integrated the new hook into the Root component of the WebUI.
- Updated RetrievalTesting component to ensure a KB is selected before allowing user interaction.
- Created end-to-end tests for multi-tenant isolation and real service interactions.
- Added scripts for starting, stopping, and cleaning the development stack.
- Enhanced API and tenant routes to support tenant-specific pipeline status initialization.
- Updated constants for backend URL to reflect the correct port.
- Improved error handling and logging in various components.

* feat: Add multi-tenant support with enhanced E2E testing scripts and client functionality

* update client

* Add integration and unit tests for multi-tenant API, models, security, and storage

- Implement integration tests for tenant and knowledge base management endpoints in `test_tenant_api_routes.py`.
- Create unit tests for tenant isolation, model validation, and role permissions in `test_tenant_models.py`.
- Add security tests to enforce role-based permissions and context validation in `test_tenant_security.py`.
- Develop tests for tenant-aware storage operations and context isolation in `test_tenant_storage_phase3.py`.

* feat(e2e): Implement OpenAI model support and database reset functionality

* Add comprehensive test suite for gpt-5-nano compatibility

- Introduced tests for parameter normalization, embeddings, and entity extraction.
- Implemented direct API testing for gpt-5-nano.
- Validated .env configuration loading and OpenAI API connectivity.
- Analyzed reasoning token overhead with various token limits.
- Documented test procedures and expected outcomes in README files.
- Ensured all tests pass for production readiness.

* kg(postgres_impl): ensure AGE extension is loaded in session and configure graph initialization

* dev: add hybrid dev helper scripts, Makefile, docker-compose.dev-db and local development docs

* feat(dev): add dev helper scripts and local development documentation for hybrid setup

* feat(multi-tenant): add detailed specifications and logs for multi-tenant improvements, including UX, backend handling, and ingestion pipeline

* feat(migration): add generated tenant/kb columns, indexes, triggers; drop unused tables; update schema and docs

* test(backward-compat): adapt tests to new StorageNameSpace/TenantService APIs (use concrete dummy storages)

* chore: multi-tenant and UX updates — docs, webui, storage, tenant service adjustments

* tests: stabilize integration tests + skip external services; fix multi-tenant API behavior and idempotency

- gpt5_nano_compatibility: add pytest-asyncio markers, skip when OPENAI key missing, prevent module-level asyncio.run collection, add conftest
- Ollama tests: add server availability check and skip markers; avoid pytest collection warnings by renaming helper classes
- Graph storage tests: rename interactive test functions to avoid pytest collection
- Document & Tenant routes: support external_ids for idempotency; ensure HTTPExceptions are re-raised
- LightRAG core: support external_ids in apipeline_enqueue_documents and idempotent logic
- Tests updated to match API changes (tenant routes & document routes)
- Add logs and scripts for inspection and audit
2025-12-04 16:04:21 +08:00

322 lines
9.9 KiB
YAML

# ============================================================================
# LightRAG Multi-Tenant Stack with PostgreSQL Backend
#
# This Docker Compose file sets up a complete multi-tenant RAG system with:
# • PostgreSQL database (with pgvector support)
# • Redis cache
# • LightRAG API server
# • Web UI interface
#
# Features:
# - Multi-tenant data isolation at database level
# - Composite key pattern (tenant_id, kb_id, id)
# - Automatic tenant context enforcement
# - Cross-tenant access prevention
#
# Quick Start:
# make up # Start all services
# make init-db # Initialize database schema
# make logs # View logs
# http://localhost:3000 # WebUI
# http://localhost:9621 # API
# ============================================================================
services:
# =========================================================================
# PostgreSQL Database
#
# Stores:
# • Multi-tenant documents (with tenant_id + kb_id isolation)
# • Knowledge graph entities and relationships
# • Vector embeddings (using pgvector extension)
# • Document status and metadata
# =========================================================================
postgres:
build:
context: .
dockerfile: Dockerfile.postgres
container_name: lightrag-postgres
hostname: postgres
environment:
POSTGRES_USER: ${POSTGRES_USER:-lightrag}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-lightrag_secure_password}
POSTGRES_DB: ${POSTGRES_DATABASE:-lightrag_multitenant}
PGTZ: UTC
# Don't publish PostgreSQL port to the host. Keep it available only on the
# compose network for app-to-db communication (safer defaults).
# Use 'expose' so other services in the compose network can access it,
# but it won't be reachable from the host machine.
expose:
- "5432"
volumes:
# Persist database data
- postgres_data:/var/lib/postgresql/data
# Custom initialization script (optional)
- ./init-postgres.sql:/docker-entrypoint-initdb.d/01-init.sql:ro
networks:
- lightrag-network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-lightrag} -d ${POSTGRES_DATABASE:-lightrag_multitenant}"]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s
restart: unless-stopped
# Performance tuning
command:
- "postgres"
- "-c"
- "max_connections=100"
- "-c"
- "shared_buffers=256MB"
- "-c"
- "effective_cache_size=1GB"
- "-c"
- "work_mem=16MB"
- "-c"
- "maintenance_work_mem=64MB"
# =========================================================================
# Redis Cache
#
# Used for:
# • Caching LLM responses (ENABLE_LLM_CACHE)
# • Session management
# • Rate limiting
# • Temporary data with TTL
# =========================================================================
redis:
image: redis:7-alpine
container_name: lightrag-redis
hostname: redis
ports:
- "${REDIS_PORT:-6379}:6379"
volumes:
- redis_data:/data
networks:
- lightrag-network
command: redis-server --appendonly yes --requirepass ${REDIS_PASSWORD:-redis_secure_password}
healthcheck:
test: ["CMD", "redis-cli", "-a", "${REDIS_PASSWORD:-redis_secure_password}", "ping"]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s
restart: unless-stopped
# =========================================================================
# LightRAG API Server
#
# Responsibilities:
# • Handle multi-tenant requests
# • Enforce tenant context (tenant_id, kb_id)
# • Route queries to appropriate backends
# • Manage document ingestion and processing
# • Return multi-tenant isolated results
#
# Storage Configuration:
# • KV Storage: PostgreSQL (PGKVStorage)
# • Doc Status: PostgreSQL (PGDocStatusStorage)
# • Graph: PostgreSQL (PGGraphStorage)
# • Vector: PostgreSQL with pgvector (PGVectorStorage)
# • Cache: Redis (LLM response caching)
# =========================================================================
lightrag-api:
image: lightrag-api:local
build:
context: ..
dockerfile: Dockerfile
container_name: lightrag-api
hostname: lightrag-api
environment:
# Server Configuration
HOST: 0.0.0.0
PORT: 8621
WORKERS: ${WORKERS:-2}
TIMEOUT: ${TIMEOUT:-150}
WEBUI_TITLE: ${WEBUI_TITLE:-"LightRAG Multi-Tenant"}
WEBUI_DESCRIPTION: ${WEBUI_DESCRIPTION:-"Graph RAG with Multi-Tenant Support"}
# PostgreSQL Configuration
LIGHTRAG_KV_STORAGE: PGKVStorage
LIGHTRAG_DOC_STATUS_STORAGE: PGDocStatusStorage
LIGHTRAG_GRAPH_STORAGE: PGGraphStorage
LIGHTRAG_VECTOR_STORAGE: PGVectorStorage
POSTGRES_HOST: postgres
POSTGRES_PORT: 5432
POSTGRES_USER: ${POSTGRES_USER:-lightrag}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-lightrag_secure_password}
POSTGRES_DATABASE: ${POSTGRES_DATABASE:-lightrag_multitenant}
POSTGRES_MAX_CONNECTIONS: ${POSTGRES_MAX_CONNECTIONS:-20}
# Vector Index Configuration
POSTGRES_VECTOR_INDEX_TYPE: ${POSTGRES_VECTOR_INDEX_TYPE:-HNSW}
POSTGRES_HNSW_M: ${POSTGRES_HNSW_M:-16}
POSTGRES_HNSW_EF: ${POSTGRES_HNSW_EF:-200}
# Redis Configuration (for caching)
REDIS_URI: ${REDIS_URI:-redis://:redis_secure_password@redis:6379}
# LLM Configuration
LLM_BINDING: ${LLM_BINDING:-openai}
LLM_MODEL: ${LLM_MODEL:-gpt-4o}
LLM_BINDING_HOST: ${LLM_BINDING_HOST:-https://api.openai.com/v1}
LLM_BINDING_API_KEY: ${LLM_BINDING_API_KEY:-your_api_key}
LLM_TIMEOUT: ${LLM_TIMEOUT:-180}
# Embedding Configuration
EMBEDDING_BINDING: ${EMBEDDING_BINDING:-ollama}
EMBEDDING_MODEL: ${EMBEDDING_MODEL:-bge-m3:latest}
EMBEDDING_DIM: ${EMBEDDING_DIM:-1024}
EMBEDDING_BINDING_HOST: ${EMBEDDING_BINDING_HOST:-http://host.docker.internal:11434}
# Document Processing
ENABLE_LLM_CACHE: ${ENABLE_LLM_CACHE:-true}
ENABLE_LLM_CACHE_FOR_EXTRACT: ${ENABLE_LLM_CACHE_FOR_EXTRACT:-true}
SUMMARY_LANGUAGE: ${SUMMARY_LANGUAGE:-English}
CHUNK_SIZE: ${CHUNK_SIZE:-1200}
CHUNK_OVERLAP_SIZE: ${CHUNK_OVERLAP_SIZE:-100}
# Concurrency Configuration
MAX_ASYNC: ${MAX_ASYNC:-1}
MAX_PARALLEL_INSERT: ${MAX_PARALLEL_INSERT:-1}
EMBEDDING_FUNC_MAX_ASYNC: ${EMBEDDING_FUNC_MAX_ASYNC:-1}
EMBEDDING_BATCH_NUM: ${EMBEDDING_BATCH_NUM:-1}
# Query Configuration
TOP_K: ${TOP_K:-40}
CHUNK_TOP_K: ${CHUNK_TOP_K:-20}
MAX_ENTITY_TOKENS: ${MAX_ENTITY_TOKENS:-6000}
MAX_RELATION_TOKENS: ${MAX_RELATION_TOKENS:-8000}
MAX_TOTAL_TOKENS: ${MAX_TOTAL_TOKENS:-30000}
# Logging
LOG_LEVEL: ${LOG_LEVEL:-INFO}
VERBOSE: ${VERBOSE:-false}
# Multi-Tenant Configuration
MULTITENANT_MODE: ${MULTITENANT_MODE:-demo}
DEFAULT_TENANT: ${DEFAULT_TENANT:-default}
DEFAULT_KB: ${DEFAULT_KB:-default}
ports:
- "${API_PORT:-8000}:8621"
volumes:
# Persist LightRAG data
- ./data/inputs:/app/data/inputs
- ./data/rag_storage:/app/data/rag_storage
- ./data/tiktoken:/app/data/tiktoken
# Configuration files
- ./.env:/app/.env:ro
- ../config.ini:/app/config.ini:ro
# Mount source code for development
- ../lightrag:/app/lightrag
networks:
- lightrag-network
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8621/health"]
interval: 30s
timeout: 15s
retries: 5
start_period: 60s
restart: unless-stopped
# Limit resource usage
deploy:
resources:
limits:
cpus: '2'
memory: 4G
reservations:
cpus: '1'
memory: 2G
# =========================================================================
# Web UI
#
# Frontend interface for:
# • Document upload and management
# • Knowledge graph visualization
# • Query interface
# • Multi-tenant tenant/KB selection
# • Results exploration
# =========================================================================
lightrag-webui:
image: lightrag-webui:local
build:
context: ../lightrag_webui
dockerfile: Dockerfile
container_name: lightrag-webui
hostname: lightrag-webui
environment:
# API Backend Configuration
VITE_API_BASE_URL: ${WEBUI_API_BASE_URL:-http://localhost:8000}
# WebUI Configuration
VITE_WEBUI_TITLE: ${WEBUI_TITLE:-"LightRAG Multi-Tenant"}
VITE_WEBUI_DESCRIPTION: ${WEBUI_DESCRIPTION:-"Graph RAG with Multi-Tenant Support"}
ports:
- "${WEBUI_PORT:-3001}:3000"
networks:
- lightrag-network
depends_on:
lightrag-api:
condition: service_healthy
healthcheck:
test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost:3000"]
interval: 30s
timeout: 10s
retries: 3
start_period: 10s
restart: unless-stopped
# ============================================================================
# Networks
# ============================================================================
networks:
lightrag-network:
driver: bridge
# ============================================================================
# Volumes
#
# postgres_data: Persists PostgreSQL database files
# redis_data: Persists Redis snapshot data
# ============================================================================
volumes:
postgres_data:
driver: local
redis_data:
driver: local