Compare commits
12
Commits
0.9.2
...
256bafe30d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
256bafe30d | ||
|
|
555d9899fe | ||
|
|
31504d1a5b | ||
|
|
2e7b13227d | ||
|
|
5469981eb5 | ||
|
|
a1e293b1d7 | ||
|
|
9f92c09586 | ||
|
|
3974010156 | ||
|
|
8d3f9a7661 | ||
|
|
280b3b0762 | ||
|
|
89661dafcc | ||
|
|
3ccc7f325d |
No files matched your search
+16
-77
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
A lightweight reverse proxy for [Ollama](https://ollama.com) that manages API keys with configurable token and request quotas. Incoming requests in OpenAI-compatible format are authenticated, checked against the quota, and forwarded to the configured Ollama server.
|
A lightweight reverse proxy for [Ollama](https://ollama.com) that manages API keys with configurable token and request quotas. Incoming requests in OpenAI-compatible format are authenticated, checked against the quota, and forwarded to the configured Ollama server.
|
||||||
|
|
||||||
Ollama does not need to run on the same host — `OLLAMA_URL` can point to any reachable server: the Docker host itself, another machine on the network, or a remote server.
|
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- OpenAI-compatible endpoint (`/v1/chat/completions`, `/v1/models`)
|
- OpenAI-compatible endpoint (`/v1/chat/completions`, `/v1/models`)
|
||||||
@@ -21,12 +19,7 @@ Ollama does not need to run on the same host — `OLLAMA_URL` can point to any r
|
|||||||
| `8000` | Proxy endpoint (OpenAI API) |
|
| `8000` | Proxy endpoint (OpenAI API) |
|
||||||
| `8001` | Admin API + web interface |
|
| `8001` | Admin API + web interface |
|
||||||
|
|
||||||
Port 8001 must be exposed because the container serves the admin interface directly on this port. To restrict access to the local machine, bind it to `127.0.0.1` — this makes the port reachable only from the host, not from the network:
|
All API endpoints require the `ADMIN_PASSWORD` — without a valid token, only the public frontend files (HTML/JS/CSS of the login page) are accessible. The password is therefore the primary protection.
|
||||||
|
|
||||||
```
|
|
||||||
ports:
|
|
||||||
- "127.0.0.1:8001:8001"
|
|
||||||
```
|
|
||||||
|
|
||||||
## Environment Variables
|
## Environment Variables
|
||||||
|
|
||||||
@@ -38,84 +31,41 @@ ports:
|
|||||||
| `DATABASE_URL` | `sqlite:///./test.db` | Database connection string (SQLite or PostgreSQL) |
|
| `DATABASE_URL` | `sqlite:///./test.db` | Database connection string (SQLite or PostgreSQL) |
|
||||||
| `PROXY_HOST` | `0.0.0.0` | Proxy bind address |
|
| `PROXY_HOST` | `0.0.0.0` | Proxy bind address |
|
||||||
| `PROXY_PORT` | `8000` | Proxy port |
|
| `PROXY_PORT` | `8000` | Proxy port |
|
||||||
|
| `ADMIN_HOST` | `0.0.0.0` | Admin API bind address (`127.0.0.1` to restrict to local access) |
|
||||||
| `ADMIN_PORT` | `8001` | Admin API port |
|
| `ADMIN_PORT` | `8001` | Admin API port |
|
||||||
| `APP_TZ` | `Europe/Berlin` | Timezone for daily/monthly quota resets |
|
| `APP_TZ` | `Europe/Berlin` | Timezone for daily/monthly quota resets |
|
||||||
| `LOG_FILE` | `logs/usage.log` | Path of the rotating usage log file |
|
| `LOG_FILE` | `logs/usage.log` | Path of the rotating usage log file |
|
||||||
|
|
||||||
## Docker Compose – External Ollama, SQLite
|
## Docker Compose – Ollama on the Host (Linux, recommended)
|
||||||
|
|
||||||
Use this when Ollama runs outside of Docker — on the Docker host or any other reachable server. Adjust `OLLAMA_URL` accordingly.
|
`network_mode: host` gives the container direct access to the host network stack. Ollama runs on the host and is reachable at `localhost:11434` — not visible from outside. The proxy and admin interface are available directly on host ports 8000 and 8001.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
services:
|
services:
|
||||||
llmproxy:
|
llmproxy:
|
||||||
image: mediaeng/llmproxy:latest
|
image: mediaeng/llmproxy:latest
|
||||||
|
container_name: llmproxy
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
network_mode: host
|
||||||
- "8000:8000"
|
env_file: .env
|
||||||
- "127.0.0.1:8001:8001"
|
|
||||||
environment:
|
|
||||||
ADMIN_PASSWORD: changeme
|
|
||||||
OLLAMA_URL: http://host.docker.internal:11434 # or http://<ip>:11434
|
|
||||||
DEFAULT_MODEL: llama3
|
|
||||||
APP_TZ: Europe/Berlin
|
|
||||||
volumes:
|
volumes:
|
||||||
- llmproxy-data:/app/backend
|
- llmproxy-data:/app/backend
|
||||||
# On Linux, add extra_hosts since host.docker.internal is not
|
|
||||||
# available automatically:
|
|
||||||
# extra_hosts:
|
|
||||||
# - "host.docker.internal:host-gateway"
|
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
llmproxy-data:
|
llmproxy-data:
|
||||||
```
|
```
|
||||||
|
|
||||||
## Docker Compose – External Ollama, PostgreSQL
|
`.env`:
|
||||||
|
```env
|
||||||
```yaml
|
ADMIN_PASSWORD=changeme
|
||||||
services:
|
OLLAMA_URL=http://localhost:11434
|
||||||
llmproxy:
|
DEFAULT_MODEL=llama3
|
||||||
image: mediaeng/llmproxy:latest
|
APP_TZ=Europe/Berlin
|
||||||
restart: unless-stopped
|
|
||||||
ports:
|
|
||||||
- "8000:8000"
|
|
||||||
- "127.0.0.1:8001:8001"
|
|
||||||
environment:
|
|
||||||
ADMIN_PASSWORD: changeme
|
|
||||||
OLLAMA_URL: http://host.docker.internal:11434 # or http://<ip>:11434
|
|
||||||
DEFAULT_MODEL: llama3
|
|
||||||
APP_TZ: Europe/Berlin
|
|
||||||
DATABASE_URL: postgresql://llmproxy:secret@db:5432/llmproxy
|
|
||||||
volumes:
|
|
||||||
- llmproxy-data:/app/backend
|
|
||||||
depends_on:
|
|
||||||
db:
|
|
||||||
condition: service_healthy
|
|
||||||
# extra_hosts:
|
|
||||||
# - "host.docker.internal:host-gateway"
|
|
||||||
|
|
||||||
db:
|
|
||||||
image: postgres:16-alpine
|
|
||||||
restart: unless-stopped
|
|
||||||
environment:
|
|
||||||
POSTGRES_DB: llmproxy
|
|
||||||
POSTGRES_USER: llmproxy
|
|
||||||
POSTGRES_PASSWORD: secret
|
|
||||||
volumes:
|
|
||||||
- pg-data:/var/lib/postgresql/data
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD-SHELL", "pg_isready -U llmproxy"]
|
|
||||||
interval: 5s
|
|
||||||
timeout: 5s
|
|
||||||
retries: 5
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
pg-data:
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Docker Compose – Ollama as Container, SQLite
|
## Docker Compose – Ollama as Container, SQLite
|
||||||
|
|
||||||
Ollama and llmproxy run together in Docker, data persisted in a volume.
|
Ollama and llmproxy run together in Docker. Ollama is not exposed externally.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
services:
|
services:
|
||||||
@@ -124,7 +74,7 @@ services:
|
|||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
ports:
|
||||||
- "8000:8000"
|
- "8000:8000"
|
||||||
- "127.0.0.1:8001:8001"
|
- "8001:8001"
|
||||||
environment:
|
environment:
|
||||||
ADMIN_PASSWORD: changeme
|
ADMIN_PASSWORD: changeme
|
||||||
OLLAMA_URL: http://ollama:11434
|
OLLAMA_URL: http://ollama:11434
|
||||||
@@ -157,7 +107,7 @@ services:
|
|||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
ports:
|
||||||
- "8000:8000"
|
- "8000:8000"
|
||||||
- "127.0.0.1:8001:8001"
|
- "8001:8001"
|
||||||
environment:
|
environment:
|
||||||
ADMIN_PASSWORD: changeme
|
ADMIN_PASSWORD: changeme
|
||||||
OLLAMA_URL: http://ollama:11434
|
OLLAMA_URL: http://ollama:11434
|
||||||
@@ -196,17 +146,6 @@ volumes:
|
|||||||
ollama-data:
|
ollama-data:
|
||||||
```
|
```
|
||||||
|
|
||||||
## Quick Start
|
|
||||||
|
|
||||||
```bash
|
|
||||||
docker run -d \
|
|
||||||
-p 8000:8000 \
|
|
||||||
-e ADMIN_PASSWORD=changeme \
|
|
||||||
-e OLLAMA_URL=http://host.docker.internal:11434 \
|
|
||||||
-v llmproxy-data:/app/backend \
|
|
||||||
mediaeng/llmproxy:latest
|
|
||||||
```
|
|
||||||
|
|
||||||
## Client Configuration
|
## Client Configuration
|
||||||
|
|
||||||
Configure the proxy as an OpenAI-compatible endpoint:
|
Configure the proxy as an OpenAI-compatible endpoint:
|
||||||
|
|||||||
+16
-77
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
Ein schlanker Reverse-Proxy für [Ollama](https://ollama.com), der API-Keys mit konfigurierbaren Token- und Request-Quoten verwaltet. Eingehende Anfragen im OpenAI-kompatiblen Format werden authentifiziert, auf Quota geprüft und an den konfigurierten Ollama-Server weitergeleitet.
|
Ein schlanker Reverse-Proxy für [Ollama](https://ollama.com), der API-Keys mit konfigurierbaren Token- und Request-Quoten verwaltet. Eingehende Anfragen im OpenAI-kompatiblen Format werden authentifiziert, auf Quota geprüft und an den konfigurierten Ollama-Server weitergeleitet.
|
||||||
|
|
||||||
Ollama muss dabei nicht auf demselben Host laufen — `OLLAMA_URL` kann auf jeden erreichbaren Server zeigen, also auf den Docker-Host selbst, einen anderen Rechner im Netzwerk oder einen Remote-Server.
|
|
||||||
|
|
||||||
## Funktionen
|
## Funktionen
|
||||||
|
|
||||||
- OpenAI-kompatibler Endpunkt (`/v1/chat/completions`, `/v1/models`)
|
- OpenAI-kompatibler Endpunkt (`/v1/chat/completions`, `/v1/models`)
|
||||||
@@ -21,12 +19,7 @@ Ollama muss dabei nicht auf demselben Host laufen — `OLLAMA_URL` kann auf jede
|
|||||||
| `8000` | Proxy-Endpunkt (OpenAI-API) |
|
| `8000` | Proxy-Endpunkt (OpenAI-API) |
|
||||||
| `8001` | Admin-API + Web-Oberfläche |
|
| `8001` | Admin-API + Web-Oberfläche |
|
||||||
|
|
||||||
Port 8001 muss exposed werden, da der Container die Admin-Oberfläche selbst auf diesem Port ausliefert. Um den Zugriff auf den lokalen Rechner zu beschränken, die Portbindung auf `127.0.0.1` setzen — so ist der Port nur vom Host erreichbar, nicht aus dem Netzwerk:
|
Alle API-Endpunkte erfordern das `ADMIN_PASSWORD` — ein Zugriff ohne gültiges Token liefert nur die öffentlichen Frontend-Dateien (HTML/JS/CSS der Login-Seite). Das Passwort ist damit die primäre Schutzmaßnahme.
|
||||||
|
|
||||||
```
|
|
||||||
ports:
|
|
||||||
- "127.0.0.1:8001:8001"
|
|
||||||
```
|
|
||||||
|
|
||||||
## Umgebungsvariablen
|
## Umgebungsvariablen
|
||||||
|
|
||||||
@@ -38,84 +31,41 @@ ports:
|
|||||||
| `DATABASE_URL` | `sqlite:///./test.db` | Datenbank-Verbindungsstring (SQLite oder PostgreSQL) |
|
| `DATABASE_URL` | `sqlite:///./test.db` | Datenbank-Verbindungsstring (SQLite oder PostgreSQL) |
|
||||||
| `PROXY_HOST` | `0.0.0.0` | Bind-Adresse des Proxy |
|
| `PROXY_HOST` | `0.0.0.0` | Bind-Adresse des Proxy |
|
||||||
| `PROXY_PORT` | `8000` | Port des Proxy |
|
| `PROXY_PORT` | `8000` | Port des Proxy |
|
||||||
|
| `ADMIN_HOST` | `0.0.0.0` | Bind-Adresse der Admin-API (`127.0.0.1` für lokalen Zugriff) |
|
||||||
| `ADMIN_PORT` | `8001` | Port der Admin-API |
|
| `ADMIN_PORT` | `8001` | Port der Admin-API |
|
||||||
| `APP_TZ` | `Europe/Berlin` | Zeitzone für Tages-/Monats-Reset der Quoten |
|
| `APP_TZ` | `Europe/Berlin` | Zeitzone für Tages-/Monats-Reset der Quoten |
|
||||||
| `LOG_FILE` | `logs/usage.log` | Pfad der rotierenden Nutzungs-Logdatei |
|
| `LOG_FILE` | `logs/usage.log` | Pfad der rotierenden Nutzungs-Logdatei |
|
||||||
|
|
||||||
## Docker Compose – Ollama extern, SQLite
|
## Docker Compose – Ollama auf dem Host (Linux, empfohlen)
|
||||||
|
|
||||||
Wenn Ollama außerhalb von Docker läuft — auf dem Docker-Host oder einem anderen erreichbaren Server. `OLLAMA_URL` entsprechend anpassen.
|
`network_mode: host` gibt dem Container direkten Zugriff auf das Host-Netzwerk. Ollama läuft auf dem Host und ist über `localhost:11434` erreichbar — nach außen nicht sichtbar. Proxy und Admin-Oberfläche sind direkt auf den Host-Ports 8000 und 8001 verfügbar.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
services:
|
services:
|
||||||
llmproxy:
|
llmproxy:
|
||||||
image: mediaeng/llmproxy:latest
|
image: mediaeng/llmproxy:latest
|
||||||
|
container_name: llmproxy
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
network_mode: host
|
||||||
- "8000:8000"
|
env_file: .env
|
||||||
- "127.0.0.1:8001:8001"
|
|
||||||
environment:
|
|
||||||
ADMIN_PASSWORD: changeme
|
|
||||||
OLLAMA_URL: http://host.docker.internal:11434 # oder http://<ip>:11434
|
|
||||||
DEFAULT_MODEL: llama3
|
|
||||||
APP_TZ: Europe/Berlin
|
|
||||||
volumes:
|
volumes:
|
||||||
- llmproxy-data:/app/backend
|
- llmproxy-data:/app/backend
|
||||||
# Auf Linux extra_hosts ergänzen, da host.docker.internal dort
|
|
||||||
# nicht automatisch verfügbar ist:
|
|
||||||
# extra_hosts:
|
|
||||||
# - "host.docker.internal:host-gateway"
|
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
llmproxy-data:
|
llmproxy-data:
|
||||||
```
|
```
|
||||||
|
|
||||||
## Docker Compose – Ollama extern, PostgreSQL
|
`.env`:
|
||||||
|
```env
|
||||||
```yaml
|
ADMIN_PASSWORD=changeme
|
||||||
services:
|
OLLAMA_URL=http://localhost:11434
|
||||||
llmproxy:
|
DEFAULT_MODEL=llama3
|
||||||
image: mediaeng/llmproxy:latest
|
APP_TZ=Europe/Berlin
|
||||||
restart: unless-stopped
|
|
||||||
ports:
|
|
||||||
- "8000:8000"
|
|
||||||
- "127.0.0.1:8001:8001"
|
|
||||||
environment:
|
|
||||||
ADMIN_PASSWORD: changeme
|
|
||||||
OLLAMA_URL: http://host.docker.internal:11434 # oder http://<ip>:11434
|
|
||||||
DEFAULT_MODEL: llama3
|
|
||||||
APP_TZ: Europe/Berlin
|
|
||||||
DATABASE_URL: postgresql://llmproxy:secret@db:5432/llmproxy
|
|
||||||
volumes:
|
|
||||||
- llmproxy-data:/app/backend
|
|
||||||
depends_on:
|
|
||||||
db:
|
|
||||||
condition: service_healthy
|
|
||||||
# extra_hosts:
|
|
||||||
# - "host.docker.internal:host-gateway"
|
|
||||||
|
|
||||||
db:
|
|
||||||
image: postgres:16-alpine
|
|
||||||
restart: unless-stopped
|
|
||||||
environment:
|
|
||||||
POSTGRES_DB: llmproxy
|
|
||||||
POSTGRES_USER: llmproxy
|
|
||||||
POSTGRES_PASSWORD: secret
|
|
||||||
volumes:
|
|
||||||
- pg-data:/var/lib/postgresql/data
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD-SHELL", "pg_isready -U llmproxy"]
|
|
||||||
interval: 5s
|
|
||||||
timeout: 5s
|
|
||||||
retries: 5
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
pg-data:
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Docker Compose – Ollama als Container, SQLite
|
## Docker Compose – Ollama als Container, SQLite
|
||||||
|
|
||||||
Ollama und llmproxy laufen gemeinsam in Docker, Daten in einem Volume.
|
Ollama und llmproxy laufen gemeinsam in Docker. Ollama ist nicht nach außen exposed.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
services:
|
services:
|
||||||
@@ -124,7 +74,7 @@ services:
|
|||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
ports:
|
||||||
- "8000:8000"
|
- "8000:8000"
|
||||||
- "127.0.0.1:8001:8001"
|
- "8001:8001"
|
||||||
environment:
|
environment:
|
||||||
ADMIN_PASSWORD: changeme
|
ADMIN_PASSWORD: changeme
|
||||||
OLLAMA_URL: http://ollama:11434
|
OLLAMA_URL: http://ollama:11434
|
||||||
@@ -157,7 +107,7 @@ services:
|
|||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
ports:
|
||||||
- "8000:8000"
|
- "8000:8000"
|
||||||
- "127.0.0.1:8001:8001"
|
- "8001:8001"
|
||||||
environment:
|
environment:
|
||||||
ADMIN_PASSWORD: changeme
|
ADMIN_PASSWORD: changeme
|
||||||
OLLAMA_URL: http://ollama:11434
|
OLLAMA_URL: http://ollama:11434
|
||||||
@@ -196,17 +146,6 @@ volumes:
|
|||||||
ollama-data:
|
ollama-data:
|
||||||
```
|
```
|
||||||
|
|
||||||
## Schnellstart
|
|
||||||
|
|
||||||
```bash
|
|
||||||
docker run -d \
|
|
||||||
-p 8000:8000 \
|
|
||||||
-e ADMIN_PASSWORD=changeme \
|
|
||||||
-e OLLAMA_URL=http://host.docker.internal:11434 \
|
|
||||||
-v llmproxy-data:/app/backend \
|
|
||||||
mediaeng/llmproxy:latest
|
|
||||||
```
|
|
||||||
|
|
||||||
## Client-Konfiguration
|
## Client-Konfiguration
|
||||||
|
|
||||||
Den Proxy als OpenAI-kompatibler Endpunkt konfigurieren:
|
Den Proxy als OpenAI-kompatibler Endpunkt konfigurieren:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# Ollama Proxy mit API-Keys und Quotas
|
# Ollama Proxy mit API-Keys und Quotas
|
||||||
|
|
||||||
Ein Reverse-Proxy für Ollama mit API-Key-Authentifizierung, Quota-Management und Web-Admin-Oberfläche.
|
Ollama bietet von sich aus keine Authentifizierung — wer die API erreicht, kann sie nutzen. Dieses Projekt löst das Problem: Ollama bleibt an `localhost` gebunden und ist von außen nicht erreichbar. Vorgeschaltet läuft ein Proxy (Port 8000), der jeden Request auf einen gültigen API-Key prüft und optional Token- sowie Request-Quoten pro Key durchsetzt. Eine Web-Admin-Oberfläche (Port 8001) erlaubt das Verwalten von Keys, Quoten und Ollama-Einstellungen.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
@@ -8,30 +8,34 @@ Ein Reverse-Proxy für Ollama mit API-Key-Authentifizierung, Quota-Management un
|
|||||||
- Optionales Ablaufdatum pro API-Key
|
- Optionales Ablaufdatum pro API-Key
|
||||||
- Quota-Management mit getrennten Tages- und Monatslimits (Tokens & Requests)
|
- Quota-Management mit getrennten Tages- und Monatslimits (Tokens & Requests)
|
||||||
- Token-Zählung via tiktoken, Reset-Grenzen in der Zeitzone Europe/Berlin
|
- Token-Zählung via tiktoken, Reset-Grenzen in der Zeitzone Europe/Berlin
|
||||||
- Web-Admin-Oberfläche (API-Keys verwalten, Ollama-Einstellungen, Proxy-Info)
|
- Web-Admin-Oberfläche (API-Keys verwalten, Ollama-Einstellungen, Verbrauchsanzeige)
|
||||||
- OpenAI-kompatibler `/v1/chat/completions`-Endpunkt
|
- OpenAI-kompatibler `/v1/chat/completions`-Endpunkt mit Streaming und Tool-Use
|
||||||
|
- Rotierende Nutzungs-Logs
|
||||||
|
- SQLite (Standard) oder PostgreSQL
|
||||||
|
- Docker-Image auf DockerHub: `mediaeng/llmproxy`
|
||||||
|
|
||||||
## Sicherheit
|
## Sicherheit
|
||||||
|
|
||||||
- Admin-Oberfläche passwortgeschützt (`ADMIN_PASSWORD`)
|
- Admin-Oberfläche passwortgeschützt (`ADMIN_PASSWORD`) — alle API-Endpunkte erfordern den Token
|
||||||
- Admin-API bindet lokal auf `127.0.0.1` (nicht von außen erreichbar)
|
|
||||||
- API-Keys als SHA-256-Hash in der DB — Plaintext nur einmalig bei Erstellung
|
- API-Keys als SHA-256-Hash in der DB — Plaintext nur einmalig bei Erstellung
|
||||||
- Quota-Check atomar mit `SELECT FOR UPDATE` (kein TOCTOU-Race)
|
- Quota-Check atomar mit `SELECT FOR UPDATE` (kein TOCTOU-Race)
|
||||||
- CORS-Origins konfigurierbar via `ALLOWED_ORIGINS`
|
- Admin-Port 8001 über `ADMIN_HOST=127.0.0.1` auf lokalen Zugriff beschränkbar
|
||||||
|
|
||||||
## Konfiguration
|
## Konfiguration
|
||||||
|
|
||||||
`.env`-Datei im Projektverzeichnis anlegen (Vorlage: `.env.example`):
|
`.env`-Datei im Projektverzeichnis anlegen:
|
||||||
|
|
||||||
```env
|
```env
|
||||||
ADMIN_PASSWORD=change-me
|
ADMIN_PASSWORD=change-me
|
||||||
PROXY_HOST=0.0.0.0
|
PROXY_HOST=0.0.0.0
|
||||||
PROXY_PORT=8000
|
PROXY_PORT=8000
|
||||||
|
ADMIN_HOST=0.0.0.0
|
||||||
ADMIN_PORT=8001
|
ADMIN_PORT=8001
|
||||||
DATABASE_URL=sqlite:///./test.db
|
DATABASE_URL=sqlite:///./test.db
|
||||||
OLLAMA_URL=http://localhost:11434
|
OLLAMA_URL=http://localhost:11434
|
||||||
DEFAULT_MODEL=llama3
|
DEFAULT_MODEL=llama3
|
||||||
APP_TZ=Europe/Berlin
|
APP_TZ=Europe/Berlin
|
||||||
|
LOG_FILE=logs/usage.log
|
||||||
```
|
```
|
||||||
|
|
||||||
| Variable | Standard | Beschreibung |
|
| Variable | Standard | Beschreibung |
|
||||||
@@ -39,26 +43,17 @@ APP_TZ=Europe/Berlin
|
|||||||
| `ADMIN_PASSWORD` | — | Passwort für die Admin-Oberfläche (**Pflicht**) |
|
| `ADMIN_PASSWORD` | — | Passwort für die Admin-Oberfläche (**Pflicht**) |
|
||||||
| `PROXY_HOST` | `0.0.0.0` | Bind-Adresse des Proxys |
|
| `PROXY_HOST` | `0.0.0.0` | Bind-Adresse des Proxys |
|
||||||
| `PROXY_PORT` | `8000` | Port des Proxys |
|
| `PROXY_PORT` | `8000` | Port des Proxys |
|
||||||
|
| `ADMIN_HOST` | `0.0.0.0` | Bind-Adresse der Admin-API (z. B. `127.0.0.1` für lokalen Zugriff) |
|
||||||
| `ADMIN_PORT` | `8001` | Port der Admin-API |
|
| `ADMIN_PORT` | `8001` | Port der Admin-API |
|
||||||
| `DATABASE_URL` | `sqlite:///./test.db` | DB-Verbindungsstring |
|
| `DATABASE_URL` | `sqlite:///./test.db` | DB-Verbindungsstring (SQLite oder PostgreSQL) |
|
||||||
| `OLLAMA_URL` | `http://localhost:11434` | Adresse der Ollama-Instanz (auch in der UI änderbar) |
|
| `OLLAMA_URL` | `http://localhost:11434` | Adresse der Ollama-Instanz (auch in der UI änderbar) |
|
||||||
| `DEFAULT_MODEL` | `llama3` | Standard-Modell für `/v1/chat/completions` (auch in der UI änderbar) |
|
| `DEFAULT_MODEL` | `llama3` | Standard-Modell für `/v1/chat/completions` (auch in der UI änderbar) |
|
||||||
| `APP_TZ` | `Europe/Berlin` | Zeitzone für tägliche/monatliche Quota-Resets |
|
| `APP_TZ` | `Europe/Berlin` | Zeitzone für tägliche/monatliche Quota-Resets |
|
||||||
| `ALLOWED_ORIGINS` | `http://localhost:5173` | Kommagetrennte CORS-Origins |
|
| `LOG_FILE` | `logs/usage.log` | Pfad der rotierenden Nutzungs-Logdatei |
|
||||||
|
| `ALLOWED_ORIGINS` | `http://localhost:5173` | CORS-Origins (nur für Entwicklung relevant) |
|
||||||
|
|
||||||
## Entwicklung (lokal)
|
## Entwicklung (lokal)
|
||||||
|
|
||||||
```bash
|
|
||||||
cp .env.example .env
|
|
||||||
# ADMIN_PASSWORD in .env setzen
|
|
||||||
|
|
||||||
./start.sh
|
|
||||||
```
|
|
||||||
|
|
||||||
Das Script prüft alle Ports auf Belegung, aktiviert automatisch eine vorhandene `.venv`, initialisiert die Datenbank und startet Proxy, Admin-API und Vite-Dev-Server.
|
|
||||||
|
|
||||||
Admin-Oberfläche: `http://localhost:5173`
|
|
||||||
|
|
||||||
### Voraussetzungen
|
### Voraussetzungen
|
||||||
|
|
||||||
- Python 3.12+ mit virtualenv
|
- Python 3.12+ mit virtualenv
|
||||||
@@ -68,71 +63,117 @@ Admin-Oberfläche: `http://localhost:5173`
|
|||||||
python -m venv .venv
|
python -m venv .venv
|
||||||
source .venv/bin/activate
|
source .venv/bin/activate
|
||||||
pip install -r backend/requirements-dev.txt
|
pip install -r backend/requirements-dev.txt
|
||||||
|
|
||||||
cd frontend && npm install
|
cd frontend && npm install
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Starten
|
||||||
|
|
||||||
|
**Per Script:**
|
||||||
|
```bash
|
||||||
|
cp .env.example .env # ADMIN_PASSWORD setzen
|
||||||
|
./start.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
**Per PyCharm:** Run-Config „Dev" starten (startet Proxy, Admin-API und Vite-Dev-Server gemeinsam).
|
||||||
|
|
||||||
|
Das Script prüft alle Ports auf Belegung, initialisiert die Datenbank und startet alle drei Dienste.
|
||||||
|
|
||||||
|
Admin-Oberfläche: `http://localhost:5173`
|
||||||
|
|
||||||
## Produktion (Docker)
|
## Produktion (Docker)
|
||||||
|
|
||||||
### Image bauen
|
### Docker Compose (empfohlen)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker build -t llm-quota .
|
docker compose up -d
|
||||||
```
|
```
|
||||||
|
|
||||||
### Container starten
|
Zieht das Image von DockerHub und lädt Variablen aus `.env`.
|
||||||
|
|
||||||
|
Das Setup verwendet `network_mode: host`: Der Container teilt den Netzwerkstack des Hosts, statt ein eigenes virtuelles Netzwerk zu bekommen. Das ist hier aus zwei Gründen die richtige Wahl:
|
||||||
|
|
||||||
|
1. **Ollama soll nicht von außen erreichbar sein.** Ollama läuft auf dem Host und ist an `127.0.0.1:11434` gebunden — nur lokal erreichbar. Mit einem eigenen Container-Netzwerk (Bridge-Mode) wäre `localhost` aus Sicht des Containers der Container selbst, nicht der Host. Die übliche Alternative (`host.docker.internal` + `extra_hosts`) ist auf Linux unzuverlässig.
|
||||||
|
|
||||||
|
2. **Kein doppeltes Port-Mapping nötig.** Mit `network_mode: host` sind Port 8000 und 8001 direkt auf dem Host verfügbar, ohne `ports:`-Einträge in der Compose-Datei.
|
||||||
|
|
||||||
|
### Image selbst bauen und pushen
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker run -d \
|
./build_push.sh
|
||||||
-p 8000:8000 \
|
|
||||||
-p 127.0.0.1:8001:8001 \
|
|
||||||
-e ADMIN_PASSWORD=geheim \
|
|
||||||
-e OLLAMA_URL=http://host.docker.internal:11434 \
|
|
||||||
-e DATABASE_URL=sqlite:///./data/quota.db \
|
|
||||||
-v $(pwd)/data:/app/backend/data \
|
|
||||||
--name llm-quota \
|
|
||||||
llm-quota
|
|
||||||
```
|
```
|
||||||
|
|
||||||
| Port | Bindung | Dienst |
|
Das Script zeigt den aktuellen Git-Tag, bietet an einen neuen zu setzen, baut das Image für `linux/arm64` und pusht zu `mediaeng/llmproxy`.
|
||||||
|------|---------|--------|
|
|
||||||
| `8000` | `0.0.0.0` — öffentlich | Proxy (für LLM-Clients) |
|
|
||||||
| `8001` | `127.0.0.1` — nur lokal am Server | Admin-API + Admin-Oberfläche |
|
|
||||||
|
|
||||||
Docker unterscheidet beim Port-Mapping zwischen `0.0.0.0` (alle Interfaces, öffentlich erreichbar) und `127.0.0.1` (nur der Server selbst kann zugreifen). Mit `-p 127.0.0.1:8001:8001` ist Port 8001 am Server verfügbar, aber von außen nicht direkt ansprechbar.
|
### Port 8001 (Admin)
|
||||||
|
|
||||||
### Admin-Oberfläche per SSH-Tunnel erreichbar machen
|
Alle Admin-Endpunkte erfordern das `ADMIN_PASSWORD` — der Token ist der primäre Schutz. Für zusätzliche Härtung lässt sich die Admin-API auf lokalen Zugriff beschränken:
|
||||||
|
|
||||||
Der SSH-Tunnel leitet einen lokalen Port auf den Server weiter und nutzt dabei, dass Port 8001 dort auf `127.0.0.1` erreichbar ist:
|
```env
|
||||||
|
ADMIN_HOST=127.0.0.1
|
||||||
```
|
|
||||||
Admin-Laptop:8001 ──SSH──► Server:127.0.0.1:8001 ──► Container:8001
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```bash
|
Bei `network_mode: host` (Produktions-Standard) ist das die einzig wirksame Methode — Docker-Port-Mapping greift dort nicht.
|
||||||
ssh -L 8001:localhost:8001 user@server
|
|
||||||
|
### HTTPS via Reverse-Proxy (ungetestet)
|
||||||
|
|
||||||
|
Wer Proxy und Admin-Oberfläche per HTTPS bereitstellen will, kann einen weiteren Reverse-Proxy (z. B. Nginx oder Caddy) vorschalten. Bei `network_mode: host` lauschen beide Dienste direkt auf dem Host, Nginx/Caddy proxyen auf `localhost`.
|
||||||
|
|
||||||
|
**Caddy** (empfohlen — automatisches TLS via Let's Encrypt):
|
||||||
|
|
||||||
|
```
|
||||||
|
llm.example.com {
|
||||||
|
reverse_proxy localhost:8000 {
|
||||||
|
flush_interval -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
llm-admin.example.com {
|
||||||
|
reverse_proxy localhost:8001
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Danach ist die Admin-Oberfläche auf dem Laptop unter `http://localhost:8001` erreichbar — ohne dass Port 8001 öffentlich exponiert wird.
|
**Nginx** (mit Certbot-Zertifikaten):
|
||||||
|
|
||||||
### Mit PostgreSQL
|
```nginx
|
||||||
|
server {
|
||||||
|
listen 443 ssl;
|
||||||
|
server_name llm.example.com;
|
||||||
|
|
||||||
```bash
|
ssl_certificate /etc/letsencrypt/live/llm.example.com/fullchain.pem;
|
||||||
docker run -d \
|
ssl_certificate_key /etc/letsencrypt/live/llm.example.com/privkey.pem;
|
||||||
-p 8000:8000 \
|
|
||||||
-p 127.0.0.1:8001:8001 \
|
location / {
|
||||||
-e ADMIN_PASSWORD=geheim \
|
proxy_pass http://127.0.0.1:8000;
|
||||||
-e DATABASE_URL=postgresql://user:pass@db-host:5432/llm_quota \
|
proxy_set_header Host $host;
|
||||||
-e OLLAMA_URL=http://ollama:11434 \
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
llm-quota
|
proxy_buffering off; # nötig für Streaming
|
||||||
|
proxy_cache off;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
server {
|
||||||
|
listen 443 ssl;
|
||||||
|
server_name llm-admin.example.com;
|
||||||
|
|
||||||
|
ssl_certificate /etc/letsencrypt/live/llm-admin.example.com/fullchain.pem;
|
||||||
|
ssl_certificate_key /etc/letsencrypt/live/llm-admin.example.com/privkey.pem;
|
||||||
|
|
||||||
|
location / {
|
||||||
|
proxy_pass http://127.0.0.1:8001;
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Clients konfigurieren dann `https://llm.example.com/v1` als Base URL.
|
||||||
|
|
||||||
## Proxy-Endpunkte (Port 8000)
|
## Proxy-Endpunkte (Port 8000)
|
||||||
|
|
||||||
Alle Endpunkte erfordern einen gültigen API-Key im `Authorization`-Header.
|
Alle Endpunkte erfordern einen gültigen API-Key im `Authorization`-Header.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
curl -X POST http://localhost:8000/api/chat \
|
curl -X POST http://localhost:8000/v1/chat/completions \
|
||||||
-H "Authorization: Bearer sk-xxxxxx" \
|
-H "Authorization: Bearer sk-xxxxxx" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d '{"model":"llama3","messages":[{"role":"user","content":"Hallo"}]}'
|
-d '{"model":"llama3","messages":[{"role":"user","content":"Hallo"}]}'
|
||||||
@@ -140,12 +181,12 @@ curl -X POST http://localhost:8000/api/chat \
|
|||||||
|
|
||||||
| Endpunkt | Methode | Beschreibung |
|
| Endpunkt | Methode | Beschreibung |
|
||||||
|----------|---------|--------------|
|
|----------|---------|--------------|
|
||||||
| `/api/generate` | POST | Ollama generate |
|
| `/v1/chat/completions` | POST | Chat (OpenAI-Format, Streaming + Tool-Use) |
|
||||||
| `/api/chat` | POST | Ollama chat |
|
| `/v1/models` | GET | Modelle (OpenAI-Format) |
|
||||||
|
| `/api/generate` | POST | Ollama generate (nativ) |
|
||||||
|
| `/api/chat` | POST | Ollama chat (nativ) |
|
||||||
| `/api/tags` | GET | Verfügbare Modelle |
|
| `/api/tags` | GET | Verfügbare Modelle |
|
||||||
| `/api/versions` | GET | Ollama-Version |
|
| `/api/versions` | GET | Ollama-Version |
|
||||||
| `/v1/models` | GET | Modelle (OpenAI-Format) |
|
|
||||||
| `/v1/chat/completions` | POST | Chat (OpenAI-Format) |
|
|
||||||
|
|
||||||
## Admin-API (Port 8001)
|
## Admin-API (Port 8001)
|
||||||
|
|
||||||
@@ -153,10 +194,12 @@ Alle Endpunkte erfordern `Authorization: Bearer <ADMIN_PASSWORD>`.
|
|||||||
|
|
||||||
| Endpunkt | Methode | Beschreibung |
|
| Endpunkt | Methode | Beschreibung |
|
||||||
|----------|---------|--------------|
|
|----------|---------|--------------|
|
||||||
| `/api/api-keys` | GET | Alle API-Keys auflisten |
|
| `/api/api-keys` | GET | Alle API-Keys mit Verbrauchsdaten |
|
||||||
| `/api/api-keys` | POST | Neuen API-Key erstellen |
|
| `/api/api-keys` | POST | Neuen API-Key erstellen |
|
||||||
|
| `/api/api-keys/{id}/quota` | PATCH | Limits eines Keys aktualisieren |
|
||||||
|
| `/api/api-keys/{id}/activate` | PUT | API-Key aktivieren |
|
||||||
| `/api/api-keys/{id}/deactivate` | PUT | API-Key deaktivieren |
|
| `/api/api-keys/{id}/deactivate` | PUT | API-Key deaktivieren |
|
||||||
| `/api/api-keys/{id}/quota` | PATCH | Quota eines Keys aktualisieren |
|
| `/api/api-keys/{id}` | DELETE | API-Key löschen |
|
||||||
| `/api/settings` | GET/PUT | Ollama-URL und Standard-Modell |
|
| `/api/settings` | GET/PUT | Ollama-URL und Standard-Modell |
|
||||||
| `/api/ollama-models` | GET | Verfügbare Modelle von Ollama |
|
| `/api/ollama-models` | GET | Verfügbare Modelle von Ollama |
|
||||||
| `/api/proxy-info` | GET | Lokaler Proxy-Endpunkt |
|
| `/api/proxy-info` | GET | Lokaler Proxy-Endpunkt |
|
||||||
@@ -180,22 +223,27 @@ llm_quota/
|
|||||||
│ ├── schemas.py # Pydantic-Schemas
|
│ ├── schemas.py # Pydantic-Schemas
|
||||||
│ ├── crud.py # DB-Operationen, Token-Zählung, Quota-Logik
|
│ ├── crud.py # DB-Operationen, Token-Zählung, Quota-Logik
|
||||||
│ ├── init_db.py # Tabellen anlegen & Settings seeden
|
│ ├── init_db.py # Tabellen anlegen & Settings seeden
|
||||||
│ ├── setup_admin.py # Standard-API-Key erstellen
|
|
||||||
│ ├── requirements.txt # Produktiv-Dependencies
|
│ ├── requirements.txt # Produktiv-Dependencies
|
||||||
│ ├── requirements-dev.txt # Test-Dependencies
|
│ ├── requirements-dev.txt # Test-Dependencies
|
||||||
│ └── tests/
|
│ └── tests/
|
||||||
│ ├── conftest.py # Fixtures
|
│ ├── conftest.py
|
||||||
│ ├── test_auth.py # Authentifizierungs-Tests
|
│ ├── test_auth.py
|
||||||
│ └── test_quota.py # Quota-, Token- und Ablauf-Tests
|
│ └── test_quota.py
|
||||||
├── frontend/
|
├── frontend/
|
||||||
│ └── src/
|
│ └── src/
|
||||||
│ ├── main.jsx # React-Admin-UI
|
│ ├── main.jsx # React-Admin-UI
|
||||||
│ └── styles.css
|
│ └── styles.css
|
||||||
|
├── .idea/runConfigurations/
|
||||||
|
│ └── Dev.xml # PyCharm Run-Config
|
||||||
├── Dockerfile
|
├── Dockerfile
|
||||||
|
├── docker-compose.yml # Produktiv-Start mit DockerHub-Image
|
||||||
├── docker-entrypoint.sh
|
├── docker-entrypoint.sh
|
||||||
├── .dockerignore
|
├── .dockerignore
|
||||||
├── .env.example
|
|
||||||
├── start.sh # Entwicklungs-Startscript
|
├── start.sh # Entwicklungs-Startscript
|
||||||
|
├── run_dev.py # Entwicklungs-Runner für PyCharm
|
||||||
|
├── build_push.sh # Docker-Build & Push zu DockerHub
|
||||||
|
├── DOCKERHUB.md # DockerHub-Beschreibung (deutsch)
|
||||||
|
├── DOCKERHUB.en.md # DockerHub-Beschreibung (englisch)
|
||||||
└── .gitignore
|
└── .gitignore
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -162,9 +162,9 @@ async def get_ollama_models(
|
|||||||
async with httpx.AsyncClient(timeout=5.0) as client:
|
async with httpx.AsyncClient(timeout=5.0) as client:
|
||||||
response = await client.get(f"{ollama_url}/api/tags")
|
response = await client.get(f"{ollama_url}/api/tags")
|
||||||
models = [m["name"] for m in response.json().get("models", [])]
|
models = [m["name"] for m in response.json().get("models", [])]
|
||||||
|
return {"models": models, "reachable": True}
|
||||||
except Exception:
|
except Exception:
|
||||||
models = []
|
return {"models": [], "reachable": False}
|
||||||
return {"models": models}
|
|
||||||
|
|
||||||
# Statisches Frontend ausliefern (nur im Produktivbetrieb, wenn dist/ existiert)
|
# Statisches Frontend ausliefern (nur im Produktivbetrieb, wenn dist/ existiert)
|
||||||
_dist = Path(__file__).parent.parent / "frontend" / "dist"
|
_dist = Path(__file__).parent.parent / "frontend" / "dist"
|
||||||
|
|||||||
+77
-18
@@ -10,20 +10,40 @@ from database import get_db
|
|||||||
import crud
|
import crud
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
|
_log_dir = Path(os.getenv("LOG_FILE", "logs/usage.log")).parent
|
||||||
|
_log_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
_fmt = logging.Formatter("%(asctime)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
|
||||||
|
|
||||||
# Rotating usage log (8 KB per file, 3 backups)
|
# Rotating usage log (8 KB per file, 3 backups)
|
||||||
_log_path = Path(os.getenv("LOG_FILE", "logs/usage.log"))
|
_usage_handler = RotatingFileHandler(str(_log_dir / "usage.log"), maxBytes=8192, backupCount=3, encoding="utf-8")
|
||||||
_log_path.parent.mkdir(parents=True, exist_ok=True)
|
_usage_handler.setFormatter(_fmt)
|
||||||
_handler = RotatingFileHandler(str(_log_path), maxBytes=8192, backupCount=3, encoding="utf-8")
|
|
||||||
_handler.setFormatter(logging.Formatter("%(asctime)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S"))
|
|
||||||
usage_log = logging.getLogger("proxy.usage")
|
usage_log = logging.getLogger("proxy.usage")
|
||||||
usage_log.setLevel(logging.INFO)
|
usage_log.setLevel(logging.INFO)
|
||||||
usage_log.addHandler(_handler)
|
usage_log.addHandler(_usage_handler)
|
||||||
usage_log.propagate = False
|
usage_log.propagate = False
|
||||||
|
|
||||||
|
# Rotating error log (64 KB per file, 5 backups)
|
||||||
|
_error_handler = RotatingFileHandler(str(_log_dir / "error.log"), maxBytes=65536, backupCount=5, encoding="utf-8")
|
||||||
|
_error_handler.setFormatter(_fmt)
|
||||||
|
error_log = logging.getLogger("proxy.error")
|
||||||
|
error_log.setLevel(logging.ERROR)
|
||||||
|
error_log.addHandler(_error_handler)
|
||||||
|
error_log.propagate = False
|
||||||
|
|
||||||
|
def _content_to_str(content) -> str:
|
||||||
|
"""Normalize OpenAI content: string or array of content parts → plain string."""
|
||||||
|
if isinstance(content, list):
|
||||||
|
return " ".join(
|
||||||
|
part.get("text", "") if isinstance(part, dict) else str(part)
|
||||||
|
for part in content
|
||||||
|
)
|
||||||
|
return content or ""
|
||||||
|
|
||||||
|
|
||||||
def _last_user_msg(messages: list, max_len: int = 120) -> str:
|
def _last_user_msg(messages: list, max_len: int = 120) -> str:
|
||||||
for msg in reversed(messages):
|
for msg in reversed(messages):
|
||||||
if msg.get("role") == "user":
|
if msg.get("role") == "user":
|
||||||
text = (msg.get("content") or "").replace("\n", " ").strip()
|
text = _content_to_str(msg.get("content")).replace("\n", " ").strip()
|
||||||
return text[:max_len] + ("…" if len(text) > max_len else "")
|
return text[:max_len] + ("…" if len(text) > max_len else "")
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
@@ -43,6 +63,25 @@ async def require_api_key(request: Request, db: Session = Depends(get_db)):
|
|||||||
|
|
||||||
app = FastAPI(title="Ollama Proxy", dependencies=[Depends(require_api_key)])
|
app = FastAPI(title="Ollama Proxy", dependencies=[Depends(require_api_key)])
|
||||||
|
|
||||||
|
@app.on_event("startup")
|
||||||
|
def apply_env_settings():
|
||||||
|
"""Write env-configured values into DB so they take effect until next restart."""
|
||||||
|
db = next(get_db())
|
||||||
|
try:
|
||||||
|
if url := os.getenv("OLLAMA_URL"):
|
||||||
|
crud.set_setting(db, "ollama_url", url)
|
||||||
|
if model := os.getenv("DEFAULT_MODEL"):
|
||||||
|
crud.set_setting(db, "default_model", model)
|
||||||
|
db.commit()
|
||||||
|
finally:
|
||||||
|
db.close()
|
||||||
|
|
||||||
|
@app.exception_handler(Exception)
|
||||||
|
async def unhandled_exception_handler(request: Request, exc: Exception):
|
||||||
|
error_log.error("Unhandled exception | %s %s | %s: %s",
|
||||||
|
request.method, request.url.path, type(exc).__name__, exc, exc_info=exc)
|
||||||
|
return JSONResponse(status_code=500, content={"error": {"message": "Internal server error", "type": "server_error"}})
|
||||||
|
|
||||||
async def proxy_request(url: str, method: str = "GET", json_data: dict = None):
|
async def proxy_request(url: str, method: str = "GET", json_data: dict = None):
|
||||||
async with httpx.AsyncClient(timeout=300.0) as client:
|
async with httpx.AsyncClient(timeout=300.0) as client:
|
||||||
response = await client.request(method=method, url=url, json=json_data)
|
response = await client.request(method=method, url=url, json=json_data)
|
||||||
@@ -60,23 +99,33 @@ async def generate(request: Request, db: Session = Depends(get_db)):
|
|||||||
prompt_preview = (body.get("prompt", "").replace("\n", " ").strip())[:120]
|
prompt_preview = (body.get("prompt", "").replace("\n", " ").strip())[:120]
|
||||||
usage_log.info('%s | /api/generate | %s | ~%d tokens | "%s"',
|
usage_log.info('%s | /api/generate | %s | ~%d tokens | "%s"',
|
||||||
request.state.api_key_name, body.get("model", "?"), prompt_tokens, prompt_preview)
|
request.state.api_key_name, body.get("model", "?"), prompt_tokens, prompt_preview)
|
||||||
response = await proxy_request(f"{ollama_url}/api/generate", method="POST", json_data=body)
|
try:
|
||||||
return JSONResponse(content=response.json(), status_code=response.status_code)
|
response = await proxy_request(f"{ollama_url}/api/generate", method="POST", json_data=body)
|
||||||
|
return JSONResponse(content=response.json(), status_code=response.status_code)
|
||||||
|
except Exception as exc:
|
||||||
|
error_log.error("Proxy error | %s | /api/generate | %s | %s: %s",
|
||||||
|
request.state.api_key_name, body.get("model", "?"), type(exc).__name__, exc, exc_info=exc)
|
||||||
|
raise
|
||||||
|
|
||||||
@app.post("/api/chat")
|
@app.post("/api/chat")
|
||||||
async def chat(request: Request, db: Session = Depends(get_db)):
|
async def chat(request: Request, db: Session = Depends(get_db)):
|
||||||
ollama_url = crud.get_setting(db, "ollama_url", os.getenv("OLLAMA_URL", "http://localhost:11434"))
|
ollama_url = crud.get_setting(db, "ollama_url", os.getenv("OLLAMA_URL", "http://localhost:11434"))
|
||||||
body = await request.json()
|
body = await request.json()
|
||||||
messages = body.get("messages", [])
|
messages = body.get("messages", [])
|
||||||
prompt_tokens = sum(crud.count_tokens(msg.get("content") or "") for msg in messages)
|
prompt_tokens = sum(crud.count_tokens(_content_to_str(msg.get("content"))) for msg in messages)
|
||||||
|
|
||||||
if not crud.check_and_increment_quota(db, request.state.api_key_id, tokens=prompt_tokens, requests=1):
|
if not crud.check_and_increment_quota(db, request.state.api_key_id, tokens=prompt_tokens, requests=1):
|
||||||
raise HTTPException(status_code=429, detail="Quota exceeded")
|
raise HTTPException(status_code=429, detail="Quota exceeded")
|
||||||
|
|
||||||
usage_log.info('%s | /api/chat | %s | ~%d tokens | "%s"',
|
usage_log.info('%s | /api/chat | %s | ~%d tokens | "%s"',
|
||||||
request.state.api_key_name, body.get("model", "?"), prompt_tokens, _last_user_msg(messages))
|
request.state.api_key_name, body.get("model", "?"), prompt_tokens, _last_user_msg(messages))
|
||||||
response = await proxy_request(f"{ollama_url}/api/chat", method="POST", json_data=body)
|
try:
|
||||||
return JSONResponse(content=response.json(), status_code=response.status_code)
|
response = await proxy_request(f"{ollama_url}/api/chat", method="POST", json_data=body)
|
||||||
|
return JSONResponse(content=response.json(), status_code=response.status_code)
|
||||||
|
except Exception as exc:
|
||||||
|
error_log.error("Proxy error | %s | /api/chat | %s | %s: %s",
|
||||||
|
request.state.api_key_name, body.get("model", "?"), type(exc).__name__, exc, exc_info=exc)
|
||||||
|
raise
|
||||||
|
|
||||||
@app.get("/api/tags")
|
@app.get("/api/tags")
|
||||||
async def list_models(db: Session = Depends(get_db)):
|
async def list_models(db: Session = Depends(get_db)):
|
||||||
@@ -103,7 +152,7 @@ async def openai_chat_completions(request: Request, db: Session = Depends(get_db
|
|||||||
|
|
||||||
body = await request.json()
|
body = await request.json()
|
||||||
messages = body.get("messages", [])
|
messages = body.get("messages", [])
|
||||||
prompt_tokens = sum(crud.count_tokens(msg.get("content") or "") for msg in messages)
|
prompt_tokens = sum(crud.count_tokens(_content_to_str(msg.get("content"))) for msg in messages)
|
||||||
|
|
||||||
if not crud.check_and_increment_quota(db, request.state.api_key_id, tokens=prompt_tokens, requests=1):
|
if not crud.check_and_increment_quota(db, request.state.api_key_id, tokens=prompt_tokens, requests=1):
|
||||||
raise HTTPException(status_code=429, detail="Quota exceeded")
|
raise HTTPException(status_code=429, detail="Quota exceeded")
|
||||||
@@ -119,15 +168,25 @@ async def openai_chat_completions(request: Request, db: Session = Depends(get_db
|
|||||||
|
|
||||||
if body.get("stream"):
|
if body.get("stream"):
|
||||||
async def generate():
|
async def generate():
|
||||||
async with httpx.AsyncClient(timeout=300.0) as client:
|
try:
|
||||||
async with client.stream("POST", target, json=body) as resp:
|
async with httpx.AsyncClient(timeout=300.0) as client:
|
||||||
async for chunk in resp.aiter_bytes():
|
async with client.stream("POST", target, json=body) as resp:
|
||||||
yield chunk
|
async for chunk in resp.aiter_bytes():
|
||||||
|
yield chunk
|
||||||
|
except Exception as exc:
|
||||||
|
error_log.error("Stream error | %s | /v1/chat/completions | %s | %s: %s",
|
||||||
|
request.state.api_key_name, model_name, type(exc).__name__, exc, exc_info=exc)
|
||||||
|
raise
|
||||||
return StreamingResponse(
|
return StreamingResponse(
|
||||||
generate(),
|
generate(),
|
||||||
media_type="text/event-stream",
|
media_type="text/event-stream",
|
||||||
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
|
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
|
||||||
)
|
)
|
||||||
|
|
||||||
response = await proxy_request(target, method="POST", json_data=body)
|
try:
|
||||||
return JSONResponse(content=response.json(), status_code=response.status_code)
|
response = await proxy_request(target, method="POST", json_data=body)
|
||||||
|
return JSONResponse(content=response.json(), status_code=response.status_code)
|
||||||
|
except Exception as exc:
|
||||||
|
error_log.error("Proxy error | %s | /v1/chat/completions | %s | %s: %s",
|
||||||
|
request.state.api_key_name, model_name, type(exc).__name__, exc, exc_info=exc)
|
||||||
|
raise
|
||||||
+16
-9
@@ -5,17 +5,24 @@ cd "$(dirname "$0")"
|
|||||||
IMAGE=mediaeng/llmproxy
|
IMAGE=mediaeng/llmproxy
|
||||||
PLATFORM=linux/arm64
|
PLATFORM=linux/arm64
|
||||||
|
|
||||||
CURRENT=$(git describe --tags --always)
|
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || true)
|
||||||
if [ -z "$CURRENT" ]; then
|
HEAD_TAG=$(git tag --points-at HEAD | head -1)
|
||||||
echo "Fehler: git describe liefert kein Ergebnis"
|
|
||||||
exit 1
|
if [ -n "$HEAD_TAG" ]; then
|
||||||
|
echo "HEAD bereits getaggt: $HEAD_TAG"
|
||||||
|
read -rp "Neuer Tag [${HEAD_TAG}]: " INPUT
|
||||||
|
VERSION="${INPUT:-$HEAD_TAG}"
|
||||||
|
else
|
||||||
|
echo "Letzter Tag: ${LAST_TAG:-kein Tag}"
|
||||||
|
read -rp "Neuer Tag: " INPUT
|
||||||
|
if [ -z "$INPUT" ]; then
|
||||||
|
echo "Kein Tag angegeben, breche ab."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
VERSION="$INPUT"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Aktueller Tag: $CURRENT"
|
if [ "$VERSION" != "$HEAD_TAG" ]; then
|
||||||
read -rp "Neuer Tag [${CURRENT}]: " INPUT
|
|
||||||
VERSION="${INPUT:-$CURRENT}"
|
|
||||||
|
|
||||||
if [ "$VERSION" != "$CURRENT" ]; then
|
|
||||||
git tag "$VERSION"
|
git tag "$VERSION"
|
||||||
git push origin "$VERSION"
|
git push origin "$VERSION"
|
||||||
echo "Tag '$VERSION' gesetzt und gepusht."
|
echo "Tag '$VERSION' gesetzt und gepusht."
|
||||||
|
|||||||
+3
-3
@@ -1,11 +1,11 @@
|
|||||||
services:
|
services:
|
||||||
llmproxy:
|
llmproxy:
|
||||||
image: mediaeng/llmproxy:latest
|
image: mediaeng/llmproxy:latest
|
||||||
|
container_name: llmproxy
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
network_mode: host
|
||||||
env_file: .env
|
env_file: .env
|
||||||
ports:
|
|
||||||
- "${PROXY_PORT:-8000}:${PROXY_PORT:-8000}"
|
|
||||||
- "127.0.0.1:8001:8001"
|
|
||||||
volumes:
|
volumes:
|
||||||
- ./backend/test.db:/app/backend/test.db
|
- ./backend/test.db:/app/backend/test.db
|
||||||
- ./backend/logs:/app/backend/logs
|
- ./backend/logs:/app/backend/logs
|
||||||
|
|
||||||
@@ -10,7 +10,7 @@ uvicorn main:app \
|
|||||||
PROXY_PID=$!
|
PROXY_PID=$!
|
||||||
|
|
||||||
uvicorn admin:app \
|
uvicorn admin:app \
|
||||||
--host "0.0.0.0" \
|
--host "${ADMIN_HOST:-0.0.0.0}" \
|
||||||
--port "${ADMIN_PORT:-8001}" &
|
--port "${ADMIN_PORT:-8001}" &
|
||||||
ADMIN_PID=$!
|
ADMIN_PID=$!
|
||||||
|
|
||||||
|
|||||||
+17
-9
@@ -80,6 +80,7 @@ function SettingsSection({ password }) {
|
|||||||
const [settings, setSettings] = useState(null);
|
const [settings, setSettings] = useState(null);
|
||||||
const [availableModels, setAvailableModels] = useState([]);
|
const [availableModels, setAvailableModels] = useState([]);
|
||||||
const [modelsLoading, setModelsLoading] = useState(false);
|
const [modelsLoading, setModelsLoading] = useState(false);
|
||||||
|
const [ollamaReachable, setOllamaReachable] = useState(true);
|
||||||
const [proxyEndpoint, setProxyEndpoint] = useState(null);
|
const [proxyEndpoint, setProxyEndpoint] = useState(null);
|
||||||
const [saved, setSaved] = useState(false);
|
const [saved, setSaved] = useState(false);
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
@@ -91,12 +92,14 @@ function SettingsSection({ password }) {
|
|||||||
headers: authHeaders(password),
|
headers: authHeaders(password),
|
||||||
params: url ? { url } : {},
|
params: url ? { url } : {},
|
||||||
});
|
});
|
||||||
const models = res.data.models;
|
const { models, reachable } = res.data;
|
||||||
|
setOllamaReachable(reachable);
|
||||||
setAvailableModels(models);
|
setAvailableModels(models);
|
||||||
if (models.length > 0 && !models.includes(currentModel)) {
|
if (models.length > 0 && !models.includes(currentModel)) {
|
||||||
setSettings(s => ({ ...s, default_model: models[0] }));
|
setSettings(s => ({ ...s, default_model: models[0] }));
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
|
setOllamaReachable(false);
|
||||||
setAvailableModels([]);
|
setAvailableModels([]);
|
||||||
} finally {
|
} finally {
|
||||||
setModelsLoading(false);
|
setModelsLoading(false);
|
||||||
@@ -144,14 +147,19 @@ function SettingsSection({ password }) {
|
|||||||
</div>
|
</div>
|
||||||
<div className="settings-row">
|
<div className="settings-row">
|
||||||
<label>Ollama-Endpunkt</label>
|
<label>Ollama-Endpunkt</label>
|
||||||
<input
|
<div className="settings-input-wrap">
|
||||||
type="url"
|
<input
|
||||||
value={settings.ollama_url}
|
type="url"
|
||||||
onChange={(e) => setSettings({ ...settings, ollama_url: e.target.value })}
|
value={settings.ollama_url}
|
||||||
onBlur={(e) => fetchModels(e.target.value, settings.default_model)}
|
onChange={(e) => setSettings({ ...settings, ollama_url: e.target.value })}
|
||||||
placeholder="http://localhost:11434"
|
onBlur={(e) => fetchModels(e.target.value, settings.default_model)}
|
||||||
required
|
placeholder="http://localhost:11434"
|
||||||
/>
|
required
|
||||||
|
/>
|
||||||
|
{!ollamaReachable && !modelsLoading && (
|
||||||
|
<div className="warning">⚠ Ollama nicht erreichbar unter {settings.ollama_url}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="settings-row">
|
<div className="settings-row">
|
||||||
<label>Standard-Modell</label>
|
<label>Standard-Modell</label>
|
||||||
|
|||||||
@@ -194,6 +194,31 @@ tr:hover {
|
|||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.settings-input-wrap {
|
||||||
|
flex: 1;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.settings-input-wrap input {
|
||||||
|
width: 100%;
|
||||||
|
padding: 8px 10px;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
border-radius: 4px;
|
||||||
|
font-size: 14px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
.warning {
|
||||||
|
color: #b8520a;
|
||||||
|
background: #fff3e0;
|
||||||
|
border: 1px solid #e67e22;
|
||||||
|
border-radius: 4px;
|
||||||
|
padding: 6px 10px;
|
||||||
|
font-size: 13px;
|
||||||
|
}
|
||||||
|
|
||||||
.settings-form button {
|
.settings-form button {
|
||||||
align-self: flex-start;
|
align-self: flex-start;
|
||||||
padding: 8px 20px;
|
padding: 8px 20px;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ export default defineConfig({
|
|||||||
plugins: [react()],
|
plugins: [react()],
|
||||||
clearScreen: false,
|
clearScreen: false,
|
||||||
server: {
|
server: {
|
||||||
|
open: true,
|
||||||
proxy: {
|
proxy: {
|
||||||
'/api/api-keys': 'http://localhost:8001',
|
'/api/api-keys': 'http://localhost:8001',
|
||||||
'/api/settings': 'http://localhost:8001',
|
'/api/settings': 'http://localhost:8001',
|
||||||
|
|||||||
+3
-2
@@ -40,13 +40,14 @@ def main():
|
|||||||
|
|
||||||
proxy_host = os.environ.get('PROXY_HOST', '0.0.0.0')
|
proxy_host = os.environ.get('PROXY_HOST', '0.0.0.0')
|
||||||
proxy_port = os.environ.get('PROXY_PORT', '8000')
|
proxy_port = os.environ.get('PROXY_PORT', '8000')
|
||||||
|
admin_host = os.environ.get('ADMIN_HOST', '127.0.0.1')
|
||||||
admin_port = os.environ.get('ADMIN_PORT', '8001')
|
admin_port = os.environ.get('ADMIN_PORT', '8001')
|
||||||
|
|
||||||
print('Initialisiere Datenbank...')
|
print('Initialisiere Datenbank...')
|
||||||
subprocess.run([str(python), 'init_db.py'], cwd=backend, check=True)
|
subprocess.run([str(python), 'init_db.py'], cwd=backend, check=True)
|
||||||
|
|
||||||
print(f'Starte Proxy → http://{proxy_host}:{proxy_port}')
|
print(f'Starte Proxy → http://{proxy_host}:{proxy_port}')
|
||||||
print(f'Starte Admin-API → http://127.0.0.1:{admin_port}')
|
print(f'Starte Admin-API → http://{admin_host}:{admin_port}')
|
||||||
print('Starte Frontend → http://localhost:5173')
|
print('Starte Frontend → http://localhost:5173')
|
||||||
|
|
||||||
env = {**os.environ, 'PYTHONUNBUFFERED': '1'}
|
env = {**os.environ, 'PYTHONUNBUFFERED': '1'}
|
||||||
@@ -59,7 +60,7 @@ def main():
|
|||||||
), 'Proxy ', '34'), # blau
|
), 'Proxy ', '34'), # blau
|
||||||
(subprocess.Popen(
|
(subprocess.Popen(
|
||||||
[str(python), '-m', 'uvicorn', 'admin:app', '--reload',
|
[str(python), '-m', 'uvicorn', 'admin:app', '--reload',
|
||||||
'--host', '127.0.0.1', '--port', admin_port],
|
'--host', admin_host, '--port', admin_port],
|
||||||
cwd=backend, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env,
|
cwd=backend, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env,
|
||||||
), 'Admin ', '33'), # gelb
|
), 'Admin ', '33'), # gelb
|
||||||
(subprocess.Popen(
|
(subprocess.Popen(
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ fi
|
|||||||
|
|
||||||
PROXY_HOST=${PROXY_HOST:-0.0.0.0}
|
PROXY_HOST=${PROXY_HOST:-0.0.0.0}
|
||||||
PROXY_PORT=${PROXY_PORT:-8000}
|
PROXY_PORT=${PROXY_PORT:-8000}
|
||||||
|
ADMIN_HOST=${ADMIN_HOST:-127.0.0.1}
|
||||||
ADMIN_PORT=${ADMIN_PORT:-8001}
|
ADMIN_PORT=${ADMIN_PORT:-8001}
|
||||||
FRONTEND_PORT=5173
|
FRONTEND_PORT=5173
|
||||||
|
|
||||||
@@ -60,9 +61,8 @@ cd backend
|
|||||||
python3 -m uvicorn main:app --reload --host "$PROXY_HOST" --port "$PROXY_PORT" &
|
python3 -m uvicorn main:app --reload --host "$PROXY_HOST" --port "$PROXY_PORT" &
|
||||||
PIDS+=($!)
|
PIDS+=($!)
|
||||||
|
|
||||||
# Admin-API immer nur lokal erreichbar (Host nicht konfigurierbar)
|
echo "Starte Admin-API auf ${ADMIN_HOST}:${ADMIN_PORT}..."
|
||||||
echo "Starte Admin-API auf 127.0.0.1:${ADMIN_PORT}..."
|
python3 -m uvicorn admin:app --reload --host "$ADMIN_HOST" --port "$ADMIN_PORT" &
|
||||||
python3 -m uvicorn admin:app --reload --host 127.0.0.1 --port "$ADMIN_PORT" &
|
|
||||||
PIDS+=($!)
|
PIDS+=($!)
|
||||||
cd ..
|
cd ..
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ PIDS+=($!)
|
|||||||
cd ..
|
cd ..
|
||||||
|
|
||||||
echo "Backend läuft (Port $PROXY_PORT)"
|
echo "Backend läuft (Port $PROXY_PORT)"
|
||||||
echo "Admin-API läuft (Port $ADMIN_PORT, nur lokal)"
|
echo "Admin-API läuft (${ADMIN_HOST}:${ADMIN_PORT})"
|
||||||
echo "Admin-Oberfläche: http://localhost:$FRONTEND_PORT"
|
echo "Admin-Oberfläche: http://localhost:$FRONTEND_PORT"
|
||||||
|
|
||||||
wait
|
wait
|
||||||
Reference in new issue
Block a user