# 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