Oliver Hofmann 562f6ecd9c Init
2026-04-27 18:54:27 +02:00
2026-04-27 18:54:27 +02:00
2026-04-27 18:54:27 +02:00
2026-04-27 18:54:27 +02:00
2026-04-27 18:54:27 +02:00
2026-04-27 18:54:27 +02:00
2026-04-27 18:54:27 +02:00

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)

# 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)

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

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:

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

// 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:

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:

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

Description
No description provided
Readme 206 KiB
Languages
Python 55.3%
JavaScript 28.4%
CSS 9.8%
Shell 4.9%
Dockerfile 1.1%
Other 0.5%