An enterprise-grade LLM API gateway system supporting multi-model aggregation, intelligent routing, and unified authentication.
π Live Demo: https://api.aihao.world
- π Intelligent Load Balancing - Weighted round-robin, health checks, automatic failover
- π Unified Authentication - API Key management, Session control, permission management
- π Usage Monitoring - Token-level statistics, real-time quotas, multi-dimensional analysis
- π Multi-Protocol Support - OpenAI / Anthropic compatible interfaces
- π High Performance - HTTP/2 support, streaming response, connection pool optimization
The easiest way to deploy using Docker Compose:
# 1. Clone the repository
git clone https://github.com/firslov/modelheart.git
cd modelheart
# 2. Configure environment
cp .env.example .env
# Edit .env file with your settings
# 3. Start with Docker Compose
docker-compose up -d
# 4. View logs
docker-compose logs -fDefault Access: http://localhost:8087
Default Admin Credentials:
- Username:
admin - Password:
admin(if ADMIN_PASSWORD_HASH not set)
β οΈ Important: Change the default password after first login!
# Start services
docker-compose up -d
# Stop services
docker-compose down
# Restart
docker-compose restart
# View logs
docker-compose logs -f
# Update to latest version
docker-compose pull && docker-compose up -d
# With Nginx reverse proxy
docker-compose --profile with-nginx up -dpip install -r requirements.txt# Copy configuration template
cp .env.example .env
# Edit configuration file
vim .envRequired Configuration:
# Domain configuration
DOMAIN=your-domain.com
API_BASE_URL=https://api.your-domain.com
# Session secret key (must be changed to a random string)
SESSION_SECRET_KEY=your-random-secret-key
# Admin password (generate hash)
# Generate with: python -c "import bcrypt; print(bcrypt.hashpw(b'your_password', bcrypt.gensalt()).decode())"
ADMIN_PASSWORD_HASH=$2b$12$...python scripts/init_database.py# Production
./start.sh
# Development (auto-reload)
DEV=1 ./start.sh
# Custom parameters
WORKERS=8 PORT=9000 LOG_LEVEL=debug ./start.shVisit: http://localhost:8087
Usage is charged based on tokens consumed. The billing method varies by endpoint:
| Endpoint | Billing Method | Details |
|---|---|---|
/v1/chat/completions |
Token-based | Input tokens Γ Input weight + Output tokens Γ Output weight |
/v1/completions |
Token-based | Input tokens Γ Input weight + Output tokens Γ Output weight |
/v1/embeddings |
Token-based | Input tokens Γ Model weight |
/anthropic/v1/messages |
Request-based | Max(Input weight, Output weight) Γ Request count |
/coding/chat/completions |
Request-based | Max(Input weight, Output weight) Γ Request count |
- Billing: Token-based (input + output tokens)
- Use Case: General chat applications
curl https://api.your-domain.com/v1/chat/completions \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello"}]
}'- Billing: Token-based (input tokens only)
- Use Case: Text embedding and similarity search
curl https://api.your-domain.com/v1/embeddings \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "text-embedding-ada-002",
"input": "Hello world"
}'- Billing: Request-based (max of input/output weights)
- Use Case: Code generation, Zhipu AI Coding Plan, etc.
- Note: OpenAI-compatible format but charged per request
curl https://api.your-domain.com/coding/chat/completions \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "zhipu-coding-model",
"messages": [{"role": "user", "content": "Write a Python function"}]
}'- Billing: Request-based (max of input/output weights)
- Use Case: Claude models and request-based billing models
curl https://api.your-domain.com/anthropic/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-3-5-sonnet-20241022",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Hello"}]
}'| Variable | Description | Default |
|---|---|---|
ENV |
Runtime environment | development |
DOMAIN |
Domain name | localhost |
API_BASE_URL |
API base URL | http://localhost:8087 |
SESSION_SECRET_KEY |
Session secret key | - |
ADMIN_USERNAME |
Admin username | admin |
ADMIN_PASSWORD_HASH |
Admin password hash | - |
DEFAULT_LIMIT |
Default API limit | 1000000 |
-
Prepare Environment
cp .env.example .env # Edit .env with production settings -
Generate Secure Password
# Generate bcrypt hash for admin password docker run --rm python:3.11-slim python -c " import bcrypt password = 'your-secure-password' hashed = bcrypt.hashpw(password.encode(), bcrypt.gensalt()) print(hashed.decode()) "
-
Configure .env
DOMAIN=your-domain.com API_BASE_URL=https://api.your-domain.com SESSION_SECRET_KEY=$(openssl rand -hex 32) ADMIN_PASSWORD_HASH=<generated-hash>
-
Deploy
docker-compose up -d
# Create ssl directory
mkdir -p ssl
# Place your SSL certificates
# ssl/cert.pem
# ssl/key.pem
# Start with Nginx
docker-compose --profile with-nginx up -d# Build image
docker build -t model-heart:latest .
# Run container
docker run -d \
--name model-heart \
-p 8087:8087 \
-v $(pwd)/data:/app/data \
-v $(pwd)/logs:/app/logs \
-v $(pwd)/.env:/app/.env:ro \
--restart unless-stopped \
model-heart:latestmyapi/
βββ app/ # API routes
β βββ api/ # API routes
β βββ config/ # Configuration management
β βββ core/ # Application core
β βββ database/ # Data layer
β βββ middleware/ # Middleware
β βββ models/ # Data models
β βββ services/ # Business logic
β βββ utils/ # Utility functions
βββ static/ # Static assets
βββ templates/ # HTML templates
βββ scripts/ # Scripts
βββ .env.example # Configuration template
βββ requirements.txt # Dependencies
βββ start.sh # Startup script
βββ Dockerfile # Docker image definition
βββ docker-compose.yml # Docker Compose configuration
βββ docker-entrypoint.sh # Docker entrypoint script
βββ nginx.conf # Nginx configuration
MIT License
Issues and Pull Requests are welcome!
Made with β€οΈ by firslov
