Tier-1 AI inference, packaged as a self-contained, cloud-portable unit. Fronts Ollama (chat, embeddings, vision) and PaddleOCR with a durable, cached gateway so admin/user AI requests are never lost, runs highly-available on the Razer box via Kubernetes, and lifts to cloud by changing one URL.
app (Next.js) ──HTTP──▶ ai-gateway (k8s Deployment, 2+ replicas)
imports @jeffi/ai-platform-sdk │
knows ONLY $AI_GATEWAY_URL ├─ LLM cache: KV (Redis) + semantic (pgvector)
├─ durable queue (BullMQ on Redis, AOF): retry + DLQ
├─ Ollama (GPU, models by hint)
└─ PaddleOCR (OCR)
- sdk/ — what the app imports.
aiChat() / embed() / vision() / health(), same signatures as the oldsrc/lib/ai-client.ts, so the app swap is signature-preserving. The SDK talks HTTP to the gateway and waits for the result; callers stay synchronous. - gateway/ — the always-on service. Two-tier cache in front of a durable queue in front of the providers. All host/model/provider knowledge lives here (moved out of the app).
- deploy/ — kustomize:
base+overlays/razer(k3s, single GPU box) +overlays/cloud(managed Redis/pgvector, cloud GPU pool). This directory IS the cloud migration.
Every call is enqueued to Redis (AOF) before any model work. A job survives an Ollama pod restart
(rescheduled) and a gateway pod restart (another replica drains the queue). The HTTP handler waits
on the job, so the caller still awaits one response. Exhausted retries → dead-letter → clean error
(never a hang).
- KV (Redis): exact-match on hash(model + messages + params). Cheapest, checked first.
- Semantic (pgvector
ai_cache): embed the prompt, ANN lookup, return on cosine ≥ per-hint threshold. Only for cacheable hints; agent/tool-calls and NL→SQL bypass it.noCacheforces fresh. - Engine-level KV/prompt reuse via Ollama
OLLAMA_KEEP_ALIVE(model stays warm). True paged-KV is a later vLLM swap — provider interface already isolates it.
One-time node setup (Ubuntu 22.04, NVIDIA driver already installed):
sudo apt-get install -y nvidia-container-toolkit # NVIDIA's apt repo
curl -sfL https://get.k3s.io | sudo sh -s - server --disable traefik --default-runtime nvidia
The bundled Traefik stays off because the host nginx owns :80. Ollama currently stays the host's
systemd service, because other GPU work on the box stops and starts it; the gateway targets the
node's :11434 and the in-cluster ollama Deployment is kept at 0. To move Ollama into the
cluster, stop and mask the systemd unit and set the Deployment to 1: it runs with hostNetwork as
the ollama user over /usr/share/ollama/.ollama, so models are reused and <host>:11434 callers
keep working. There is no registry: images are built on the box straight into k3s containerd and
run with imagePullPolicy: Never.
Build and roll out (from ai-platform/ in an up-to-date checkout on the box). The tag never
changes (:latest, imagePullPolicy: Never), so a rollout restart is what picks up a new build:
pgrep buildkitd || sudo /opt/buildkit/bin/buildkitd --oci-worker=false --containerd-worker=true \
--containerd-worker-addr=/run/k3s/containerd/containerd.sock --containerd-worker-namespace=k8s.io &
sudo /opt/buildkit/bin/buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=gateway \
--output type=image,name=docker.io/jeffi/ai-gateway:latest,unpack=true
sudo /opt/buildkit/bin/buildctl build --frontend dockerfile.v0 --local context=. --local dockerfile=paddleocr \
--output type=image,name=docker.io/jeffi/paddleocr:latest,unpack=true
kubectl apply -k deploy/overlays/razer # ConfigMap + manifests
kubectl -n ai-platform rollout restart deploy/ai-gateway deploy/paddleocr # new images + new ConfigMap values
kubectl -n ai-platform rollout status deploy/ai-gateway
kubectl -n ai-platform rollout status deploy/paddleocr # ready = PP-OCRv5 loaded
kubectl -n ai-platform logs -l app=ai-gateway --tail=50 | grep -E 'listening|ollama'
curl -s localhost:8080/readyz # 200 {"ok":true,...,"ollama":{"active":...}}
OLLAMA_BASE_URLS is a comma-separated list (the old single OLLAMA_BASE_URL still works). The
gateway probes them in order (GET /api/tags), uses the first healthy one, re-probes every
OLLAMA_PROBE_INTERVAL_MS (and returns to an earlier URL once it recovers), and fails a request
over to the next URL on a connection error. It logs ollama active: <url> on every change and
ollama UNREACHABLE: <reason> when none answer. On the Razer the list is the k3s cni0 bridge
(10.42.0.1, the host as seen from pods) then the Tailscale IP; never use the hostname, which once
resolved to a stale address and took every AI feature down while /healthz stayed green.
GET /healthz: liveness only (process up); used by both k8s probes.GET /readyz(or/healthz?deep=1): truthful dependency check, 503 +reasonwhen Redis or every Ollama URL is unreachable. Cached for one probe interval so it never hammers Ollama.- While Ollama is known down,
/v1/chat(cache misses),/v1/embedand failed/v1/visionanswer 503 immediately instead of queueing, so the app falls back to direct Ollama without waiting. - The app's
ai_healthcron (every 5 min) runs a real chat through the gateway plus a direct Ollama check and mails the platform admin once per incident, then once on recovery.
The gateway is published on <host>:8080 (ServiceLB).
With AI_GATEWAY_TOKEN set in ai-gateway-secret, every route except /healthz and /readyz (including
/v1/files/:id) needs Authorization: Bearer <token>, else 401. Unset = open. The app sends AI_GATEWAY_TOKEN
from its own env (Secrets Manager jeffi/production + jeffi/store), the SDK reads the same variable, and the
release script reads it from the Secret. To rotate, set the gateway to new,old, roll the app to new, then
drop old.
POST /v1/ocr {data, mime_type?} reads a document with PaddleOCR only (never a model, so the same file always
reads the same): data is bare base64 up to 12 MB, mime_type one of image/png|jpeg|webp or application/pdf
(default: sniffed from the bytes, else PNG; a PDF is read up to 10 pages). 200 {ok: true, text, lines: [{text, score}], meanScore, pages, model: 'PP-OCRv5', provider: 'paddleocr'}, with text: '' when nothing is legible;
400 {ok: false, error} for a bad request, 503 when the sidecar is down or fails (PADDLE_OCR_TIMEOUT_MS, 120 s).
/v1/vision still tries PaddleOCR first and falls back to the Ollama vision model. /readyz and /v1/providers
report the sidecar (ocr); it never fails readiness.
The sidecar is paddleocr/ (FastAPI on :8866, CPU image with the models baked in). requirements.txt pins the
whole tree (regenerate from requirements.in with the command at its top). Locally:
pip install -r paddleocr/requirements.txt && python paddleocr/ocr_service.py (Python 3.11+), then point
PADDLE_OCR_URL at http://localhost:8866.
The single 12 GB GPU fits Ollama's models OR an ARC training run, not both, so training frees the
card by stopping/starting the host ollama service. deploy/overlays/razer/host/ keeps the AI
service always up regardless: a systemd timer (ollama-gpu-guard.timer, every 60s) runs
ollama-gpu-guard.sh, which puts Ollama on the GPU when the card is free and pins it to CPU
(CUDA_VISIBLE_DEVICES=) whenever any non-Ollama process is using the GPU. Ollama never goes down;
it is only slower while training holds the card.
The same installer adds the k3s IP guard (k3s-ip-guard.timer, every 60s): the box is on home Wi-Fi
with a DHCP address and k3s keeps the node IP it started with, so after an address change pods lose the
API and CoreDNS stops learning new Services (a new Service never resolves). When the node IP and the
box's address disagree on two checks in a row, the guard restarts k3s and then CoreDNS (at most once per
30 minutes). sudo k3s-ip-guard.sh --check prints both addresses. Install both (idempotent, needs sudo
on the node):
deploy/overlays/razer/host/install-guard.sh
Routes (contract in docs/plans/AI_MEDIA_PLATFORM_PLAN.md; SDK: generateImage, generateVideo, videoJob):
POST /v1/images/generateruns through the durable queue (one attempt; the router itself falls through providers). Order fromAI_IMAGE_PROVIDERS(defaultrazer,google,replicate); unconfigured providers are skipped, a failing one falls through to the next, the answering one is returned asprovider.quality: premiumtries google first,drafttries razer first,providerpins one. Size is clamped to 256..1536 in steps of 64,nto 1..4. An identical request in the samenamespaceis a free KV hit (cache: 'kv') unlessseedis set.POST /v1/video/generatestarts an async job (state in Redisai:video:<jobId>, 24 h TTL); a poller advances open jobs everyAI_VIDEO_POLL_MS, andGET /v1/jobs/:idreads (and nudges) it.POST /v1/images/stock{query, orientation?, minWidth?, perPage?}searches free photo libraries inAI_STOCK_PROVIDERSorder (defaultpexels,pixabay; keysPEXELS_API_KEY,PIXABAY_API_KEY, a library without its key is skipped). The first library with a hit at leastminWidthwide answers{results: [{provider, id, url, width, height, alt, photographer, photographerUrl, pageUrl}], provider}; no key, no hits or failures answer 200 withresults: []. Hits are cached 24 h in Redis per provider, query and orientation. Pixabay: photos only, safe search, AI-tagged hits dropped,largeImageURLonly (1280 px; larger needs Pixabay's approval). The app copies the file to its own bucket (never hotlinks) and credits it (docs/plans/STOCK_IMAGES.md).GET /v1/files/:idserves generated bytes forAI_FILES_TTL_SECONDS(1 h). URLs are relative unlessAI_PUBLIC_URLis set (the SDK resolves relative ones). The app must copy them to its own storage.usage.costInris platform cost fromgateway/src/pricing.ts, overridable withAI_PRICES_JSON({"provider":{"model or prefix or *":{"image":n,"video_second":n,"token_1k_in":n,"token_1k_out":n}}}). Razer and Ollama carry a small amortised compute cost./v1/chatand/v1/embedreturnusage: { promptTokens, completionTokens, costInr }(cache hits cost 0).
Providers:
| name | env | images | video |
|---|---|---|---|
| razer | SD_WEBUI_URL (A1111 --api), SD_MODEL |
/sdapi/v1/txt2img, img2img with a reference |
- |
GOOGLE_AI_API_KEY |
gemini-3.1-flash-image (gemini-3-pro-image for premium) via /v1beta/interactions; optional Imagen :predict fallback via GOOGLE_IMAGEN_MODEL |
veo-3.1-fast-generate-preview, fallback veo-3.0-generate-001; 4/6/8 s, 16:9 or 9:16, 720p |
|
| replicate | REPLICATE_API_TOKEN |
black-forest-labs/flux-schnell (draft), flux-dev (standard), flux-1.1-pro (premium) |
wan-video/wan-2.2-t2v-fast (wan-2.2-i2v-fast with an image) |
transparent: true on an image request adds an isolate-the-subject hint to the prompt, then cuts each image out with
REPLICATE_BG_REMOVAL_MODEL (default bria/remove-background) on Replicate, whichever provider drew it. Without a
Replicate token such requests are rejected (400) rather than returned opaque.
Tests: npm test (vitest, all network mocked).
In cloud, Ollama runs as its own always-on GPU pod (the base Deployment on a GPU node pool), so
the Razer GPU guard is not used and nothing stops the model server. Deploy deploy/overlays/cloud (managed Redis + pgvector, cloud GPU node pool, cloud ingress) and
repoint AI_GATEWAY_URL in the app. No app redeploy beyond the URL; no code change.
The app's src/lib/ai-client.ts, rag.ts embed, and admin-agent/vision.ts call the SDK. The app
env shrinks to AI_GATEWAY_URL (+ AI_PROVIDER for the openai escape hatch). See
docs/the app repo for the ai_cache schema (in admin-service/database/tables/ai.sql, applied to the local db).