138 lines
3.1 KiB
Markdown
138 lines
3.1 KiB
Markdown
# Ollama Proxy mit API-Keys und Quotas
|
|
|
|
Ein Reverse-Proxy für Ollama mit API-Key-Authentifizierung und Quota-Management.
|
|
|
|
## Features
|
|
|
|
- 🔑 API-Key-Authentifizierung (Bearer Token oder `sk-` Prefix)
|
|
- 📊 Quota-Management (Tokens & Requests pro Tag/Monat)
|
|
- 📈 Usage-Tracking in Echtzeit
|
|
- 🖥️ Web-Admin-Oberfläche für User & Quotas
|
|
- 🔒 Benutzer-Management
|
|
|
|
## Installation & Start
|
|
|
|
### Voraussetzungen
|
|
|
|
- Python 3.12+
|
|
- PostgreSQL 16+
|
|
- Node.js 18+ (für Frontend)
|
|
|
|
### lokal mit SQLite (Entwicklung)
|
|
|
|
```bash
|
|
# Backend
|
|
cd backend
|
|
python init_db.py
|
|
python setup_admin.py
|
|
uvicorn main:app --reload
|
|
|
|
# Frontend (in neuem Terminal)
|
|
cd frontend
|
|
npm install
|
|
npm run dev
|
|
```
|
|
|
|
### mit PostgreSQL & Docker (Produktion)
|
|
|
|
```bash
|
|
docker compose up -d
|
|
docker compose exec backend python init_db.py
|
|
docker compose exec backend python setup_admin.py
|
|
```
|
|
|
|
## Usage
|
|
|
|
### Mit API-Key authentifizieren
|
|
|
|
```bash
|
|
curl -X POST http://localhost:8000/api/generate \
|
|
-H "Authorization: sk-xxxxxx" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"model":"llama3","prompt":"Say hello"}'
|
|
```
|
|
|
|
Oder mit Bearer Token:
|
|
|
|
```bash
|
|
curl -X POST http://localhost:8000/api/generate \
|
|
-H "Authorization: Bearer sk-xxxxxx" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"model":"llama3","prompt":"Say hello"}'
|
|
```
|
|
|
|
### Admin API Endpoints
|
|
|
|
| Endpoint | Method | Description |
|
|
|----------|--------|-------------|
|
|
| `/api/users` | GET | Liste aller User |
|
|
| `/api/users` | POST | Neuen User erstellen |
|
|
| `/api/api-keys` | GET | Liste aller API-Keys |
|
|
| `/api/api-keys` | POST | Neuen API-Key erstellen |
|
|
| `/api/api-keys/{id}/deactivate` | PUT | API-Key deaktivieren |
|
|
| `/api/quotas/{user_id}` | PUT | Quota für User setzen |
|
|
|
|
### Quota-Beispiele
|
|
|
|
```json
|
|
// Quota setzen (per PUT /api/quotas/{user_id})
|
|
{
|
|
"daily_tokens": 1000000,
|
|
"monthly_tokens": 10000000,
|
|
"daily_requests": 1000,
|
|
"monthly_requests": 10000
|
|
}
|
|
```
|
|
|
|
## Konfiguration
|
|
|
|
### Umgebungsvariablen
|
|
|
|
Im `backend/` Verzeichnis `.env` Datei erstellen:
|
|
|
|
```env
|
|
OLLAMA_URL=http://ollama:11434
|
|
DATABASE_URL=postgresql://user:pass@host:5432/db
|
|
SECRET_KEY=your-secret-key-min-32-chars
|
|
```
|
|
|
|
### Docker Compose
|
|
|
|
`docker-compose.yml` anpassen:
|
|
|
|
```yaml
|
|
environment:
|
|
- DATABASE_URL=postgresql://ollama:password@db:5432/ollama_proxy
|
|
- OLLAMA_URL=http://ollama:11434
|
|
- SECRET_KEY=your-secret-key
|
|
```
|
|
|
|
## Projektstruktur
|
|
|
|
```
|
|
llm_quota/
|
|
├── backend/
|
|
│ ├── main.py # Proxy-Server (Ollama API forwarden)
|
|
│ ├── admin.py # Admin API (User/Quota Management)
|
|
│ ├── database.py # DB-Verbindung & Session
|
|
│ ├── models.py # SQLAlchemy Models
|
|
│ ├── schemas.py # Pydantic Schemas
|
|
│ ├── crud.py # Database Operations
|
|
│ ├── types.py # Response Types
|
|
│ ├── requirements.txt
|
|
│ ├── Dockerfile
|
|
│ └── setup_admin.py # Admin User erstellen
|
|
├── frontend/
|
|
│ ├── src/
|
|
│ │ ├── main.jsx
|
|
│ │ └── styles.css
|
|
│ ├── index.html
|
|
│ ├── package.json
|
|
│ └── vite.config.js
|
|
└── docker-compose.yml
|
|
```
|
|
|
|
## Lizenz
|
|
|
|
MIT
|