LiteLLM / README.md
Sanvity's picture
Update README.md
71a8300 verified
|
Raw
History Blame Contribute Delete
6.66 kB
---
title: LiteLLM Gateway
emoji: πŸ›‘οΈ
colorFrom: blue
colorTo: green
sdk: docker
app_port: 8000
pinned: false
---
# πŸ›‘οΈ LiteLLM Gateway Console β€” Production-Ready AI Proxy
An enterprise-grade AI gateway with **intelligent LLM routing**, **real-time PII shielding**, and a **built-in Streamlit dashboard** β€” accessible from a single URL in any browser, no local setup required.
Built with **FastAPI**, **LiteLLM**, **DeBERTa-v3**, and **Streamlit**, deployed as a single Docker container.
---
## ☁️ One-Click Deploy
Deploy to your preferred cloud platform by clicking a button below. Set your API key secrets in the platform dashboard after deployment.
### Railway (Recommended)
[![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/template/new?template=https://github.com/sanvity/LiteLLM_Proxy)
> After deploy: Go to **Variables** β†’ add `GROQ_API_KEY`, `CEREBRAS_API_KEY`, `TOGETHERAI_API_KEY`
### Render
[![Deploy to Render](https://render.com/images/deploy-to-render-button.svg)](https://render.com/deploy?repo=https://github.com/sanvity/LiteLLM_Proxy)
> After deploy: Go to **Environment** β†’ add your API keys as secret variables
### Fly.io
```bash
# Install Fly CLI, then:
fly launch --no-deploy
fly secrets set GROQ_API_KEY=gsk_... CEREBRAS_API_KEY=csk_... TOGETHERAI_API_KEY=...
fly deploy
```
---
## πŸ”‘ Required Environment Variables
| Variable | Description | Required |
|---|---|---|
| `GROQ_API_KEY` | Groq API key ([console.groq.com](https://console.groq.com)) | βœ… |
| `CEREBRAS_API_KEY` | Cerebras API key ([cloud.cerebras.ai](https://cloud.cerebras.ai)) | βœ… |
| `TOGETHERAI_API_KEY` | Together AI key ([api.together.ai](https://api.together.ai)) | βœ… |
| `MISTRAL_API_KEY` | Mistral API key (optional) | ⬜ |
| `PORT` | HTTP port (auto-set by cloud platform) | auto |
| `OLLAMA_API_BASE` | Local Ollama URL (local only) | ⬜ |
See [`.env.example`](.env.example) for a full template.
---
## ✨ Key Features
| Feature | Details |
|---|---|
| πŸ”€ **Intelligent Routing** | `usage-based-routing` β€” automatically balances load across Groq, Cerebras, Together AI |
| πŸ›‘οΈ **PII Shield (DeBERTa-v3)** | Real-time detection of 10+ PII entity types with BLOCK / MASK / REWRITE controls per entity |
| πŸ“Š **Live Dashboard** | Streamlit UI served at `/` β€” no separate port, works on any cloud |
| ⚑ **OpenAI-Compatible API** | Drop-in replacement for any OpenAI SDK client |
| πŸ” **Auto Fallbacks** | `primary-cluster β†’ backup-cluster` with context-window escalation |
| πŸ—οΈ **Single Container** | One Docker image, one port, one URL |
---
## πŸ—οΈ Architecture
```
Browser β†’ https://myapp.com/ (single public URL)
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ FastAPI Gateway β”‚ port $PORT (e.g. 8000)
β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ HTTP + WebSocket reverse proxy
β”‚ β”‚ Streamlit UI │◄─┼─── / (root) and /_stcore/* paths
β”‚ β”‚ (port 8501) β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ LLM Router │◄─┼─── /v1/chat/completions
β”‚ β”‚ DeBERTa PII β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β–Ό β–Ό β–Ό
Groq Cerebras Together AI
```
---
## πŸ“ Directory Structure
```text
LiteLLM_Proxy/
β”œβ”€β”€ Dockerfile # Multi-stage build (model pre-cache, slim runtime)
β”œβ”€β”€ .dockerignore # Keeps secrets out of the image
β”œβ”€β”€ railway.toml # Railway deployment config
β”œβ”€β”€ render.yaml # Render.com deployment blueprint
β”œβ”€β”€ fly.toml # Fly.io app configuration
β”œβ”€β”€ Procfile # Heroku/Railway fallback entrypoint
β”œβ”€β”€ .env.example # Environment variable template (safe to commit)
β”œβ”€β”€ config.yaml # LiteLLM routing, capacity, and fallback settings
β”œβ”€β”€ requirements.txt # Python production dependencies
β”œβ”€β”€ main.py # Unified entrypoint: spawns Streamlit + starts FastAPI
β”œβ”€β”€ app.py # Streamlit dashboard frontend
β”œβ”€β”€ test_proxy.py # Automated unit & integration test suite
└── proxy/ # FastAPI package
β”œβ”€β”€ app.py # Routes, Streamlit reverse proxy, PII config endpoints
β”œβ”€β”€ router.py # Token estimation, routing, DeBERTa PII guardrail
β”œβ”€β”€ config.py # Pydantic config models
└── guardrails/ # DeBERTa-v3 PII detection engine
```
---
## πŸ–₯️ Local Development
### 1. Clone & Install
```bash
git clone https://github.com/sanvity/LiteLLM_Proxy.git
cd LiteLLM_Proxy
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
```
### 2. Configure API Keys
```bash
cp .env.example .env
# Edit .env and add your API keys
```
### 3. Start the Application
```bash
python main.py
```
The app will:
1. Spawn the Streamlit dashboard on internal port 8501
2. Wait for Streamlit to be ready
3. Start FastAPI on port 8000
Then visit **http://localhost:8000** β€” the full UI loads in your browser.
---
## 🐳 Local Docker Build
```bash
docker build -t litellm-gateway .
docker run -p 8000:8000 --env-file .env litellm-gateway
# Visit http://localhost:8000
```
---
## πŸ§ͺ Running Tests
```bash
python -m unittest test_proxy.py
```
---
## πŸ“‘ API Reference
| Endpoint | Method | Description |
|---|---|---|
| `/` | GET | Streamlit dashboard (proxied) |
| `/health` | GET | Liveness probe β€” returns `{"status": "healthy"}` |
| `/metrics` | GET | Real-time routing and token metrics |
| `/v1/models` | GET | List virtual and physical models |
| `/v1/chat/completions` | POST | OpenAI-compatible chat completion |
| `/ui/pii-config` | GET/POST | Read/update PII guardrail configuration |
| `/preference-config` | GET/POST | Read/update model preference routing |
| `/old-ui` | GET | Legacy HTML dashboard (debugging) |
### Example Request
```bash
curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "primary-cluster",
"messages": [{"role": "user", "content": "Hello!"}],
"temperature": 0.7,
"max_tokens": 500
}'
```