diff --git "a/app.py" "b/app.py"
--- "a/app.py"
+++ "b/app.py"
@@ -1,331 +1,2411 @@
+"""
+RyzGateway - BYOK OpenAI-Compatible AI Gateway
+Single-file FastAPI app for HuggingFace Spaces deployment.
+
+Model routing:
+ - provider/model → direct passthrough to that provider
+ - generic-name → routed pool (round-robin, skip rate-limited)
+
+Rate limit handling:
+ - Auto-detects 429s and marks endpoints as cooled down
+ - Respects RPM / RPD / RPS limits configured per provider
+ - Smart selection skips exhausted endpoints
+"""
+
import os
-import sqlite3
+import re
+import time
+import uuid
import json
-from typing import Optional, List, Dict
+import asyncio
+import logging
+from pathlib import Path
+from typing import Any, Dict, List, Optional, Literal
+from contextlib import asynccontextmanager
+from collections import defaultdict, deque
+
import httpx
-from cryptography.fernet import Fernet
+from fastapi import FastAPI, HTTPException, Header, Request, Depends
+from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse
+from pydantic import BaseModel, Field
+
+# ── Logging ────────────────────────────────────────────────────────────────────
+
+logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
+log = logging.getLogger("ryzgateway")
+
+# ── Persistence (JSON file, reloaded on each read) ─────────────────────────────
+
+DATA_FILE = Path(os.getenv("DATA_FILE", "/data/gateway.json"))
+
+def _default_data() -> dict:
+ return {
+ "api_keys": {}, # key_id → {key, name, created_at}
+ "providers": {}, # provider_id → {name, base_url, api_key, rate_type, rate_limit, notes}
+ "pool_models": {}, # pool_id → {name, members: [{provider_id, model_name, weight}], strategy}
+ }
+
+def load_data() -> dict:
+ try:
+ if DATA_FILE.exists():
+ return json.loads(DATA_FILE.read_text())
+ except Exception as e:
+ log.error(f"Failed to load data: {e}")
+ return _default_data()
+
+def save_data(data: dict):
+ try:
+ DATA_FILE.parent.mkdir(parents=True, exist_ok=True)
+ DATA_FILE.write_text(json.dumps(data, indent=2))
+ except Exception as e:
+ log.error(f"Failed to save data: {e}")
+
+# ── In-memory rate limit state ─────────────────────────────────────────────────
+# keyed by (provider_id, model_name)
+
+class RateLimitState:
+ def __init__(self):
+ self._lock = asyncio.Lock()
+ # cooldown_until: float timestamp
+ self._cooldown: Dict[str, float] = {}
+ # request timestamps for RPM/RPD/RPS tracking: deque of floats
+ self._timestamps: Dict[str, deque] = defaultdict(lambda: deque(maxlen=10000))
+ # round-robin index per pool
+ self._rr_index: Dict[str, int] = defaultdict(int)
+
+ def key(self, provider_id: str, model_name: str) -> str:
+ return f"{provider_id}:{model_name}"
+
+ async def mark_rate_limited(self, provider_id: str, model_name: str, cooldown_secs: float = 60.0):
+ async with self._lock:
+ k = self.key(provider_id, model_name)
+ self._cooldown[k] = time.time() + cooldown_secs
+ log.warning(f"Rate limited: {k} — cooling for {cooldown_secs}s")
+
+ async def is_available(self, provider_id: str, model_name: str, provider_cfg: dict) -> bool:
+ async with self._lock:
+ k = self.key(provider_id, model_name)
+ now = time.time()
-from fastapi import FastAPI, Request, Response, Header, HTTPException, Form
-from fastapi.responses import JSONResponse, HTMLResponse, RedirectResponse
+ # Check hard cooldown (from 429)
+ if self._cooldown.get(k, 0) > now:
+ return False
-app = FastAPI(title="Multi-Model API Proxy & Router")
+ rate_type = provider_cfg.get("rate_type", "none")
+ rate_limit = provider_cfg.get("rate_limit", 0)
+ if rate_type == "none" or not rate_limit:
+ return True
-# ====================================================
-# Directory & Encryption Setup
-# ====================================================
-DATA_DIR = "/app/data"
-os.makedirs(DATA_DIR, exist_ok=True)
-DB_FILE = os.path.join(DATA_DIR, "backends.db")
+ window = {"rpm": 60, "rps": 1, "rpd": 86400}.get(rate_type, 60)
+ cutoff = now - window
+ dq = self._timestamps[k]
+ while dq and dq[0] < cutoff:
+ dq.popleft()
+ return len(dq) < rate_limit
-# Encryption key for securing URLs and Keys at rest
-SECRET_KEY = os.getenv("ENCRYPTION_KEY", Fernet.generate_key().decode())
-cipher = Fernet(SECRET_KEY.encode())
+ async def record_request(self, provider_id: str, model_name: str):
+ async with self._lock:
+ k = self.key(provider_id, model_name)
+ self._timestamps[k].append(time.time())
+ def get_cooldown_remaining(self, provider_id: str, model_name: str) -> float:
+ k = self.key(provider_id, model_name)
+ remaining = self._cooldown.get(k, 0) - time.time()
+ return max(0.0, remaining)
-def encrypt_val(value: str) -> str:
- if not value:
- return ""
- return cipher.encrypt(value.encode()).decode()
+ async def next_rr(self, pool_id: str, count: int) -> int:
+ async with self._lock:
+ idx = self._rr_index[pool_id] % count
+ self._rr_index[pool_id] = (idx + 1) % count
+ return idx
+rl_state = RateLimitState()
-def decrypt_val(value: str) -> str:
- if not value:
- return ""
+# ── Auth ───────────────────────────────────────────────────────────────────────
+
+ADMIN_KEY = os.getenv("ADMIN_KEY", "admin-changeme")
+
+async def require_admin(authorization: Optional[str] = Header(None)):
+ if not authorization or not authorization.startswith("Bearer "):
+ raise HTTPException(status_code=401, detail="Missing Authorization header")
+ token = authorization[7:]
+ if token != ADMIN_KEY:
+ raise HTTPException(status_code=403, detail="Invalid admin key")
+ return token
+
+async def require_api_key(authorization: Optional[str] = Header(None)):
+ if not authorization or not authorization.startswith("Bearer "):
+ raise HTTPException(status_code=401, detail="Missing Authorization header")
+ token = authorization[7:]
+ # Admin key always works
+ if token == ADMIN_KEY:
+ return token
+ data = load_data()
+ for k in data["api_keys"].values():
+ if k["key"] == token:
+ return token
+ raise HTTPException(status_code=403, detail="Invalid API key")
+
+# ── Routing logic ──────────────────────────────────────────────────────────────
+
+async def resolve_endpoint(model_str: str, data: dict):
+ """
+ Returns (provider_cfg, actual_model_name) or raises HTTPException.
+ Handles:
+ - "provider_id/model_name" → direct provider lookup
+ - "pool_name" → pool routing
+ """
+ # Direct: contains a slash → treat as provider_id/model
+ if "/" in model_str:
+ parts = model_str.split("/", 1)
+ provider_id, model_name = parts[0], parts[1]
+ provider = data["providers"].get(provider_id)
+ if not provider:
+ raise HTTPException(status_code=404, detail=f"Provider '{provider_id}' not found")
+ return provider, model_name, provider_id
+
+ # Pool lookup
+ pool = None
+ pool_id = None
+ for pid, p in data["pool_models"].items():
+ if p["name"] == model_str or pid == model_str:
+ pool = p
+ pool_id = pid
+ break
+
+ if not pool:
+ raise HTTPException(status_code=404, detail=f"Model or pool '{model_str}' not found. Use 'provider_id/model_name' or a configured pool name.")
+
+ members = pool.get("members", [])
+ if not members:
+ raise HTTPException(status_code=503, detail=f"Pool '{model_str}' has no members")
+
+ strategy = pool.get("strategy", "round_robin")
+
+ if strategy == "round_robin":
+ start = await rl_state.next_rr(pool_id, len(members))
+ for i in range(len(members)):
+ m = members[(start + i) % len(members)]
+ provider = data["providers"].get(m["provider_id"])
+ if not provider:
+ continue
+ if await rl_state.is_available(m["provider_id"], m["model_name"], provider):
+ return provider, m["model_name"], m["provider_id"]
+
+ elif strategy == "priority":
+ for m in members:
+ provider = data["providers"].get(m["provider_id"])
+ if not provider:
+ continue
+ if await rl_state.is_available(m["provider_id"], m["model_name"], provider):
+ return provider, m["model_name"], m["provider_id"]
+
+ elif strategy == "weighted":
+ import random
+ available = []
+ for m in members:
+ provider = data["providers"].get(m["provider_id"])
+ if not provider:
+ continue
+ if await rl_state.is_available(m["provider_id"], m["model_name"], provider):
+ available.append((m, provider))
+ if available:
+ weights = [m[0].get("weight", 1) for m in available]
+ chosen_m, chosen_p = random.choices(available, weights=weights, k=1)[0]
+ return chosen_p, chosen_m["model_name"], chosen_m["provider_id"]
+
+ raise HTTPException(status_code=429, detail=f"All endpoints in pool '{model_str}' are currently rate-limited. Try again shortly.")
+
+# ── Proxy logic ────────────────────────────────────────────────────────────────
+
+TIMEOUT = httpx.Timeout(320.0, connect=30.0, read=300.0, write=30.0)
+
+def _extract_retry_after(response: httpx.Response) -> float:
+ ra = response.headers.get("retry-after", "")
try:
- return cipher.decrypt(value.encode()).decode()
+ return float(ra)
except Exception:
- return value
-
-# ====================================================
-# Database Initialization
-# ====================================================
-def init_db():
- conn = sqlite3.connect(DB_FILE)
- cursor = conn.cursor()
-
- # Table for API backends
- cursor.execute("""
- CREATE TABLE IF NOT EXISTS backends (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- url TEXT UNIQUE NOT NULL,
- api_key TEXT
- )
- """)
-
- # Table for mapping models to backends
- cursor.execute("""
- CREATE TABLE IF NOT EXISTS backend_models (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- backend_id INTEGER NOT NULL,
- model_id TEXT NOT NULL,
- FOREIGN KEY (backend_id) REFERENCES backends(id) ON DELETE CASCADE,
- UNIQUE(backend_id, model_id)
- )
- """)
-
- conn.commit()
- conn.close()
-
-init_db()
-
-# Persistent HTTP Client
-client = httpx.AsyncClient(timeout=600)
-
-# Tracker for Round-Robin routing per model
-model_counters: Dict[str, int] = {}
-
-# ====================================================
-# Database Helpers
-# ====================================================
-def get_all_backends():
- """Retrieve decrypted backends with their associated models."""
- conn = sqlite3.connect(DB_FILE)
- cursor = conn.cursor()
- cursor.execute("SELECT id, url, api_key FROM backends")
- rows = cursor.fetchall()
-
- backends = []
- for row_id, enc_url, enc_key in rows:
- cursor.execute("SELECT model_id FROM backend_models WHERE backend_id = ?", (row_id,))
- models = [m[0] for m in cursor.fetchall()]
- backends.append({
- "id": row_id,
- "url": decrypt_val(enc_url),
- "api_key": decrypt_val(enc_key),
- "models": models
+ return 60.0
+
+async def proxy_request(provider: dict, model_name: str, provider_id: str, body: dict, stream: bool):
+ base_url = provider["base_url"].rstrip("/")
+ api_key = provider["api_key"]
+ headers = {
+ "Authorization": f"Bearer {api_key}",
+ "Content-Type": "application/json",
+ }
+ body = {**body, "model": model_name}
+ url = f"{base_url}/chat/completions"
+
+ await rl_state.record_request(provider_id, model_name)
+
+ if stream:
+ async def stream_gen():
+ async with httpx.AsyncClient(timeout=TIMEOUT) as client:
+ async with client.stream("POST", url, json=body, headers=headers) as resp:
+ if resp.status_code == 429:
+ cooldown = _extract_retry_after(resp)
+ await rl_state.mark_rate_limited(provider_id, model_name, cooldown)
+ yield f"data: {json.dumps({'error': 'rate_limited', 'provider': provider_id})}\n\n"
+ return
+ async for chunk in resp.aiter_bytes():
+ yield chunk
+ return StreamingResponse(stream_gen(), media_type="text/event-stream")
+
+ async with httpx.AsyncClient(timeout=TIMEOUT) as client:
+ resp = await client.post(url, json=body, headers=headers)
+
+ if resp.status_code == 429:
+ cooldown = _extract_retry_after(resp)
+ await rl_state.mark_rate_limited(provider_id, model_name, cooldown)
+ raise HTTPException(status_code=429, detail=f"Upstream rate limit hit on {provider_id}/{model_name}. Retry after {cooldown}s.")
+
+ if resp.status_code >= 500:
+ raise HTTPException(status_code=502, detail=f"Upstream error from {provider_id}: {resp.status_code}")
+
+ if resp.status_code >= 400:
+ try:
+ detail = resp.json()
+ except Exception:
+ detail = resp.text
+ raise HTTPException(status_code=resp.status_code, detail=detail)
+
+ return JSONResponse(content=resp.json())
+
+# ── App ────────────────────────────────────────────────────────────────────────
+
+@asynccontextmanager
+async def lifespan(app: FastAPI):
+ log.info("RyzGateway starting up")
+ if not DATA_FILE.exists():
+ save_data(_default_data())
+ log.info(f"Initialized empty data at {DATA_FILE}")
+ yield
+ log.info("RyzGateway shutting down")
+
+app = FastAPI(title="RyzGateway", lifespan=lifespan)
+
+# ── OpenAI-compatible endpoints ────────────────────────────────────────────────
+
+class ChatMessage(BaseModel):
+ role: str
+ content: Any
+
+class ChatRequest(BaseModel):
+ model: str
+ messages: List[ChatMessage]
+ stream: Optional[bool] = False
+ temperature: Optional[float] = None
+ max_tokens: Optional[int] = None
+ top_p: Optional[float] = None
+ # Pass through any extra fields
+ model_config = {"extra": "allow"}
+
+@app.post("/v1/chat/completions")
+async def chat_completions(request: ChatRequest, _key: str = Depends(require_api_key)):
+ data = load_data()
+ provider, model_name, provider_id = await resolve_endpoint(request.model, data)
+ body = request.model_dump(exclude_none=True, exclude={"model"})
+ # Include any extra fields passed through
+ body.update(request.model_extra or {})
+ return await proxy_request(provider, model_name, provider_id, body, request.stream or False)
+
+@app.get("/v1/models")
+async def list_models(_key: str = Depends(require_api_key)):
+ data = load_data()
+ now = int(time.time())
+ models = []
+
+ # Direct provider models (listed as provider_id/model if provider has known models)
+ for pid, p in data["providers"].items():
+ for m in p.get("known_models", []):
+ models.append({
+ "id": f"{pid}/{m}",
+ "object": "model",
+ "created": now,
+ "owned_by": pid,
+ })
+
+ # Pool models
+ for pool_id, pool in data["pool_models"].items():
+ models.append({
+ "id": pool["name"],
+ "object": "model",
+ "created": now,
+ "owned_by": "pool",
})
-
- conn.close()
- return backends
+ return {"object": "list", "data": models}
+
+# ── Admin API ──────────────────────────────────────────────────────────────────
+
+# --- API Keys ---
+
+@app.get("/admin/api-keys")
+async def get_api_keys(_=Depends(require_admin)):
+ data = load_data()
+ return list(data["api_keys"].values())
+
+@app.post("/admin/api-keys")
+async def create_api_key(body: dict, _=Depends(require_admin)):
+ data = load_data()
+ kid = str(uuid.uuid4())[:8]
+ key = "ryz-" + str(uuid.uuid4()).replace("-", "")
+ data["api_keys"][kid] = {
+ "id": kid,
+ "key": key,
+ "name": body.get("name", "Unnamed Key"),
+ "created_at": int(time.time()),
+ }
+ save_data(data)
+ return data["api_keys"][kid]
+
+@app.delete("/admin/api-keys/{kid}")
+async def delete_api_key(kid: str, _=Depends(require_admin)):
+ data = load_data()
+ if kid not in data["api_keys"]:
+ raise HTTPException(status_code=404, detail="Key not found")
+ del data["api_keys"][kid]
+ save_data(data)
+ return {"deleted": True}
+
+# --- Providers ---
+
+@app.get("/admin/providers")
+async def get_providers(_=Depends(require_admin)):
+ data = load_data()
+ # Return providers with masked API keys and live rate limit state
+ result = []
+ for pid, p in data["providers"].items():
+ entry = {**p, "id": pid}
+ raw = entry.get("api_key", "")
+ entry["api_key_masked"] = raw[:6] + "..." + raw[-4:] if len(raw) > 10 else "****"
+ # Don't send actual key to frontend
+ entry.pop("api_key", None)
+ # Add live cooldown info per known model
+ cooldowns = {}
+ for m in p.get("known_models", []):
+ cd = rl_state.get_cooldown_remaining(pid, m)
+ if cd > 0:
+ cooldowns[m] = round(cd, 1)
+ entry["active_cooldowns"] = cooldowns
+ result.append(entry)
+ return result
+
+@app.post("/admin/providers")
+async def create_provider(body: dict, _=Depends(require_admin)):
+ data = load_data()
+ pid = body.get("id") or re.sub(r"[^a-z0-9_-]", "-", body.get("name", "provider").lower())[:32]
+ if pid in data["providers"]:
+ raise HTTPException(status_code=409, detail=f"Provider ID '{pid}' already exists")
+ data["providers"][pid] = {
+ "name": body.get("name", pid),
+ "base_url": body.get("base_url", ""),
+ "api_key": body.get("api_key", ""),
+ "rate_type": body.get("rate_type", "none"),
+ "rate_limit": int(body.get("rate_limit", 0)),
+ "known_models": body.get("known_models", []),
+ "notes": body.get("notes", ""),
+ }
+ save_data(data)
+ return {"id": pid, **data["providers"][pid], "api_key": "[hidden]"}
+
+@app.put("/admin/providers/{pid}")
+async def update_provider(pid: str, body: dict, _=Depends(require_admin)):
+ data = load_data()
+ if pid not in data["providers"]:
+ raise HTTPException(status_code=404, detail="Provider not found")
+ p = data["providers"][pid]
+ for field in ["name", "base_url", "rate_type", "notes", "known_models"]:
+ if field in body:
+ p[field] = body[field]
+ if "rate_limit" in body:
+ p["rate_limit"] = int(body["rate_limit"])
+ if "api_key" in body and body["api_key"] and not body["api_key"].startswith("****"):
+ p["api_key"] = body["api_key"]
+ save_data(data)
+ return {"id": pid, **p, "api_key": "[hidden]"}
+
+@app.delete("/admin/providers/{pid}")
+async def delete_provider(pid: str, _=Depends(require_admin)):
+ data = load_data()
+ if pid not in data["providers"]:
+ raise HTTPException(status_code=404, detail="Provider not found")
+ del data["providers"][pid]
+ save_data(data)
+ return {"deleted": True}
+
+# --- Pool Models ---
+
+@app.get("/admin/pools")
+async def get_pools(_=Depends(require_admin)):
+ data = load_data()
+ result = []
+ for pool_id, pool in data["pool_models"].items():
+ entry = {**pool, "id": pool_id}
+ # Annotate members with live availability
+ annotated = []
+ for m in pool.get("members", []):
+ provider = data["providers"].get(m["provider_id"], {})
+ available = await rl_state.is_available(m["provider_id"], m["model_name"], provider)
+ cooldown = rl_state.get_cooldown_remaining(m["provider_id"], m["model_name"])
+ annotated.append({**m, "available": available, "cooldown_remaining": round(cooldown, 1)})
+ entry["members"] = annotated
+ result.append(entry)
+ return result
+
+@app.post("/admin/pools")
+async def create_pool(body: dict, _=Depends(require_admin)):
+ data = load_data()
+ pool_id = body.get("id") or re.sub(r"[^a-z0-9_-]", "-", body.get("name", "pool").lower())[:32]
+ if pool_id in data["pool_models"]:
+ raise HTTPException(status_code=409, detail=f"Pool ID '{pool_id}' already exists")
+ data["pool_models"][pool_id] = {
+ "name": body.get("name", pool_id),
+ "strategy": body.get("strategy", "round_robin"),
+ "members": body.get("members", []),
+ }
+ save_data(data)
+ return {"id": pool_id, **data["pool_models"][pool_id]}
+
+@app.put("/admin/pools/{pool_id}")
+async def update_pool(pool_id: str, body: dict, _=Depends(require_admin)):
+ data = load_data()
+ if pool_id not in data["pool_models"]:
+ raise HTTPException(status_code=404, detail="Pool not found")
+ p = data["pool_models"][pool_id]
+ for field in ["name", "strategy", "members"]:
+ if field in body:
+ p[field] = body[field]
+ save_data(data)
+ return {"id": pool_id, **p}
+
+@app.delete("/admin/pools/{pool_id}")
+async def delete_pool(pool_id: str, _=Depends(require_admin)):
+ data = load_data()
+ if pool_id not in data["pool_models"]:
+ raise HTTPException(status_code=404, detail="Pool not found")
+ del data["pool_models"][pool_id]
+ save_data(data)
+ return {"deleted": True}
+
+# --- Status ---
+
+@app.get("/admin/status")
+async def get_status(_=Depends(require_admin)):
+ data = load_data()
+ return {
+ "providers": len(data["providers"]),
+ "pools": len(data["pool_models"]),
+ "api_keys": len(data["api_keys"]),
+ }
+
+# ── Dashboard (embedded HTML) ──────────────────────────────────────────────────
+
+DASHBOARD_HTML = r"""
+
+
+
+
+RyzGateway
+
+
+
+
+
+
+
RyzGateway
+
Enter your admin key to access the dashboard.
+
+
+
+
+
+
Invalid key.
+
+
+
+
+
+
+
+
+
+
+
+
Dashboard
+
Gateway status and routing health
+
+
+
—
Providers
+
—
Model Pools
+
—
API Keys
+
+
+
Provider health
+
No providers configured
+
+
+
+
+
+
+
Providers
+
API endpoints with rate limit configuration
+
+
+
+
+
+
+ Provider ID is auto-generated from the name and used in model routing:
+ Use provider-id/exact-model-name in your API calls to route directly to this provider.
+
+
+
+
+
+
Provider
Base URL
Rate limit
Models
Status
+
+
Loading...
+
+
+
+
+
+
+
+
+
Model Pools
+
Generic model names that route across multiple providers
+
+
+
+
+
+
+ Pool names are generic model names exposed in /v1/models. The gateway routes requests using your chosen strategy and automatically skips rate-limited endpoints.
+
+
Loading...
+
+
+
+
+
+
API Keys
+
Keys issued to clients for /v1/ access
+
+
+
+
+
+
+
+
+
Name
Key
Created
+
Loading...
+
+
+
+
+
+
+
+
+
Usage docs
+
How to use RyzGateway in your apps
+
+
+
Base URL
+
+
Point any OpenAI-compatible client at this URL.
+
+
+
Model name formats
+
+
provider-id/exact-model-name — Direct passthrough. Requests go to that provider using the exact model string, no pool routing.
+
pool-name — Pool routing. Gateway picks the best available member based on your pool strategy (round-robin, priority, weighted).
+
+
+
+
+
+"""
+
+@app.get("/", response_class=HTMLResponse)
+async def dashboard():
+ return DASHBOARD_HTML
+
+if __name__ == "__main__":
+ import uvicorn
+ uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=False)"""
+RyzGateway - BYOK OpenAI-Compatible AI Gateway
+Single-file FastAPI app for HuggingFace Spaces deployment.
+
+Model routing:
+ - provider/model → direct passthrough to that provider
+ - generic-name → routed pool (round-robin, skip rate-limited)
+
+Rate limit handling:
+ - Auto-detects 429s and marks endpoints as cooled down
+ - Respects RPM / RPD / RPS limits configured per provider
+ - Smart selection skips exhausted endpoints
+"""
+
+import os
+import re
+import time
+import uuid
+import json
+import asyncio
+import logging
+from pathlib import Path
+from typing import Any, Dict, List, Optional, Literal
+from contextlib import asynccontextmanager
+from collections import defaultdict, deque
+
+import httpx
+from fastapi import FastAPI, HTTPException, Header, Request, Depends
+from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse
+from pydantic import BaseModel, Field
+
+# ── Logging ────────────────────────────────────────────────────────────────────
+
+logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
+log = logging.getLogger("ryzgateway")
-async def fetch_and_store_models(backend_id: int, url: str, api_key: str):
- """Query /v1/models on target API and save detected model IDs to DB."""
- headers = {}
- if api_key:
- headers["Authorization"] = f"Bearer {api_key}"
-
+# ── Persistence (JSON file, reloaded on each read) ─────────────────────────────
+
+DATA_FILE = Path(os.getenv("DATA_FILE", "/data/gateway.json"))
+
+def _default_data() -> dict:
+ return {
+ "api_keys": {}, # key_id → {key, name, created_at}
+ "providers": {}, # provider_id → {name, base_url, api_key, rate_type, rate_limit, notes}
+ "pool_models": {}, # pool_id → {name, members: [{provider_id, model_name, weight}], strategy}
+ }
+
+def load_data() -> dict:
try:
- r = await client.get(f"{url}/models", headers=headers, timeout=10.0)
- if r.status_code == 200:
- data = r.json()
- model_list = data.get("data", [])
-
- conn = sqlite3.connect(DB_FILE)
- cursor = conn.cursor()
- for m in model_list:
- m_id = m.get("id") if isinstance(m, dict) else str(m)
- if m_id:
- cursor.execute(
- "INSERT OR IGNORE INTO backend_models (backend_id, model_id) VALUES (?, ?)",
- (backend_id, m_id)
- )
- conn.commit()
- conn.close()
+ if DATA_FILE.exists():
+ return json.loads(DATA_FILE.read_text())
except Exception as e:
- print(f"Failed to fetch models for {url}: {e}")
-
-# ====================================================
-# Client Proxy Authentication
-# ====================================================
-VALID_API_KEY = "sk-yermom"
-
-def verify_proxy_key(authorization: Optional[str] = Header(None)):
- """Validates request incoming to this proxy server."""
- if not authorization:
- return True # Allows requests with no key
-
- token = authorization.replace("Bearer ", "").strip()
- if token == "" or token == VALID_API_KEY:
- return True
-
- raise HTTPException(status_code=401, detail="Unauthorized: Invalid Proxy API Key")
-
-# ====================================================
-# Web Admin UI
-# ====================================================
-@app.get("/", response_class=HTMLResponse)
-async def admin_dashboard():
- backends = get_all_backends()
-
- list_items = ""
- for b in backends:
- key_status = "🔑 Key Set" if b["api_key"] else "🔓 No Key"
- models_badge = ", ".join(b["models"]) if b["models"] else "No models loaded"
-
- list_items += f"""
-
-
-
- {b['url']}
- {key_status}
-
-
-
-
- Models: {models_badge}
-
-
- """
-
- html_content = f"""
-
-
-
- API Model Router Admin
-
-
-
-
Multi-Model Router Dashboard
-
Manage target APIs, keys, and model auto-discovery.
-
-
-
Add Backend API
-
-
+ log.error(f"Failed to load data: {e}")
+ return _default_data()
+
+def save_data(data: dict):
+ try:
+ DATA_FILE.parent.mkdir(parents=True, exist_ok=True)
+ DATA_FILE.write_text(json.dumps(data, indent=2))
+ except Exception as e:
+ log.error(f"Failed to save data: {e}")
+
+# ── In-memory rate limit state ─────────────────────────────────────────────────
+# keyed by (provider_id, model_name)
+
+class RateLimitState:
+ def __init__(self):
+ self._lock = asyncio.Lock()
+ # cooldown_until: float timestamp
+ self._cooldown: Dict[str, float] = {}
+ # request timestamps for RPM/RPD/RPS tracking: deque of floats
+ self._timestamps: Dict[str, deque] = defaultdict(lambda: deque(maxlen=10000))
+ # round-robin index per pool
+ self._rr_index: Dict[str, int] = defaultdict(int)
+
+ def key(self, provider_id: str, model_name: str) -> str:
+ return f"{provider_id}:{model_name}"
+
+ async def mark_rate_limited(self, provider_id: str, model_name: str, cooldown_secs: float = 60.0):
+ async with self._lock:
+ k = self.key(provider_id, model_name)
+ self._cooldown[k] = time.time() + cooldown_secs
+ log.warning(f"Rate limited: {k} — cooling for {cooldown_secs}s")
+
+ async def is_available(self, provider_id: str, model_name: str, provider_cfg: dict) -> bool:
+ async with self._lock:
+ k = self.key(provider_id, model_name)
+ now = time.time()
+
+ # Check hard cooldown (from 429)
+ if self._cooldown.get(k, 0) > now:
+ return False
+
+ rate_type = provider_cfg.get("rate_type", "none")
+ rate_limit = provider_cfg.get("rate_limit", 0)
+ if rate_type == "none" or not rate_limit:
+ return True
-
Active Backends ({len(backends)} total)
-
- {list_items if list_items else "
No backends added yet.
"}
-
-
-
+ window = {"rpm": 60, "rps": 1, "rpd": 86400}.get(rate_type, 60)
+ cutoff = now - window
+ dq = self._timestamps[k]
+ while dq and dq[0] < cutoff:
+ dq.popleft()
+ return len(dq) < rate_limit
+
+ async def record_request(self, provider_id: str, model_name: str):
+ async with self._lock:
+ k = self.key(provider_id, model_name)
+ self._timestamps[k].append(time.time())
+
+ def get_cooldown_remaining(self, provider_id: str, model_name: str) -> float:
+ k = self.key(provider_id, model_name)
+ remaining = self._cooldown.get(k, 0) - time.time()
+ return max(0.0, remaining)
+
+ async def next_rr(self, pool_id: str, count: int) -> int:
+ async with self._lock:
+ idx = self._rr_index[pool_id] % count
+ self._rr_index[pool_id] = (idx + 1) % count
+ return idx
+
+rl_state = RateLimitState()
+
+# ── Auth ───────────────────────────────────────────────────────────────────────
+
+ADMIN_KEY = os.getenv("ADMIN_KEY", "admin-changeme")
+
+async def require_admin(authorization: Optional[str] = Header(None)):
+ if not authorization or not authorization.startswith("Bearer "):
+ raise HTTPException(status_code=401, detail="Missing Authorization header")
+ token = authorization[7:]
+ if token != ADMIN_KEY:
+ raise HTTPException(status_code=403, detail="Invalid admin key")
+ return token
+
+async def require_api_key(authorization: Optional[str] = Header(None)):
+ if not authorization or not authorization.startswith("Bearer "):
+ raise HTTPException(status_code=401, detail="Missing Authorization header")
+ token = authorization[7:]
+ # Admin key always works
+ if token == ADMIN_KEY:
+ return token
+ data = load_data()
+ for k in data["api_keys"].values():
+ if k["key"] == token:
+ return token
+ raise HTTPException(status_code=403, detail="Invalid API key")
+
+# ── Routing logic ──────────────────────────────────────────────────────────────
+
+async def resolve_endpoint(model_str: str, data: dict):
"""
- return HTMLResponse(content=html_content)
-
-
-@app.post("/admin/add")
-async def add_backend(url: str = Form(...), api_key: str = Form("")):
- clean_url = url.strip().rstrip("/")
- clean_key = api_key.strip()
-
- if clean_url:
- enc_url = encrypt_val(clean_url)
- enc_key = encrypt_val(clean_key)
-
- conn = sqlite3.connect(DB_FILE)
- cursor = conn.cursor()
- try:
- cursor.execute("INSERT INTO backends (url, api_key) VALUES (?, ?)", (enc_url, enc_key))
- backend_id = cursor.lastrowid
- conn.commit()
-
- # Instantly load and store available models from the added backend
- await fetch_and_store_models(backend_id, clean_url, clean_key)
- except sqlite3.IntegrityError:
- pass # Already exists
- finally:
- conn.close()
-
- return RedirectResponse(url="/", status_code=303)
-
-
-@app.post("/admin/delete")
-async def delete_backend(backend_id: int = Form(...)):
- conn = sqlite3.connect(DB_FILE)
- cursor = conn.cursor()
- cursor.execute("DELETE FROM backends WHERE id = ?", (backend_id,))
- cursor.execute("DELETE FROM backend_models WHERE backend_id = ?", (backend_id,))
- conn.commit()
- conn.close()
- return RedirectResponse(url="/", status_code=303)
-
-# ====================================================
-# OpenAI Compatible Endpoints
-# ====================================================
+ Returns (provider_cfg, actual_model_name) or raises HTTPException.
+ Handles:
+ - "provider_id/model_name" → direct provider lookup
+ - "pool_name" → pool routing
+ """
+ # Direct: contains a slash → treat as provider_id/model
+ if "/" in model_str:
+ parts = model_str.split("/", 1)
+ provider_id, model_name = parts[0], parts[1]
+ provider = data["providers"].get(provider_id)
+ if not provider:
+ raise HTTPException(status_code=404, detail=f"Provider '{provider_id}' not found")
+ return provider, model_name, provider_id
-@app.get("/v1/models")
-async def models(request: Request):
- verify_proxy_key(request.headers.get("Authorization"))
-
- backends = get_all_backends()
- all_models = set()
-
- for b in backends:
- for m in b["models"]:
- all_models.add(m)
-
- # Format into OpenAI /v1/models standard response
- data = [{"id": m, "object": "model", "owned_by": "router"} for m in sorted(all_models)]
- return JSONResponse({"object": "list", "data": data})
+ # Pool lookup
+ pool = None
+ pool_id = None
+ for pid, p in data["pool_models"].items():
+ if p["name"] == model_str or pid == model_str:
+ pool = p
+ pool_id = pid
+ break
+ if not pool:
+ raise HTTPException(status_code=404, detail=f"Model or pool '{model_str}' not found. Use 'provider_id/model_name' or a configured pool name.")
-@app.post("/v1/chat/completions")
-async def chat(request: Request):
- verify_proxy_key(request.headers.get("Authorization"))
-
+ members = pool.get("members", [])
+ if not members:
+ raise HTTPException(status_code=503, detail=f"Pool '{model_str}' has no members")
+
+ strategy = pool.get("strategy", "round_robin")
+
+ if strategy == "round_robin":
+ start = await rl_state.next_rr(pool_id, len(members))
+ for i in range(len(members)):
+ m = members[(start + i) % len(members)]
+ provider = data["providers"].get(m["provider_id"])
+ if not provider:
+ continue
+ if await rl_state.is_available(m["provider_id"], m["model_name"], provider):
+ return provider, m["model_name"], m["provider_id"]
+
+ elif strategy == "priority":
+ for m in members:
+ provider = data["providers"].get(m["provider_id"])
+ if not provider:
+ continue
+ if await rl_state.is_available(m["provider_id"], m["model_name"], provider):
+ return provider, m["model_name"], m["provider_id"]
+
+ elif strategy == "weighted":
+ import random
+ available = []
+ for m in members:
+ provider = data["providers"].get(m["provider_id"])
+ if not provider:
+ continue
+ if await rl_state.is_available(m["provider_id"], m["model_name"], provider):
+ available.append((m, provider))
+ if available:
+ weights = [m[0].get("weight", 1) for m in available]
+ chosen_m, chosen_p = random.choices(available, weights=weights, k=1)[0]
+ return chosen_p, chosen_m["model_name"], chosen_m["provider_id"]
+
+ raise HTTPException(status_code=429, detail=f"All endpoints in pool '{model_str}' are currently rate-limited. Try again shortly.")
+
+# ── Proxy logic ────────────────────────────────────────────────────────────────
+
+TIMEOUT = httpx.Timeout(320.0, connect=30.0, read=300.0, write=30.0)
+
+def _extract_retry_after(response: httpx.Response) -> float:
+ ra = response.headers.get("retry-after", "")
try:
- body_bytes = await request.body()
- body_json = json.loads(body_bytes)
+ return float(ra)
except Exception:
- return JSONResponse({"error": "Invalid JSON body"}, status_code=400)
-
- requested_model = body_json.get("model")
- backends = get_all_backends()
-
- # Filter backends that support the requested model
- eligible_backends = [b for b in backends if requested_model in b["models"]]
-
- # Fallback: If model isn't mapped, try all registered backends
- if not eligible_backends:
- eligible_backends = backends
-
- if not eligible_backends:
- return JSONResponse({"error": "No backends configured or available"}, status_code=503)
-
- # Round-Robin selection for the requested model
- idx = model_counters.get(requested_model, 0) % len(eligible_backends)
- model_counters[requested_model] = idx + 1
-
- # Attempt forwarding to eligible backends
- for i in range(len(eligible_backends)):
- selected_backend = eligible_backends[(idx + i) % len(eligible_backends)]
- backend_url = selected_backend["url"]
- backend_key = selected_backend["api_key"]
-
- # Prepare outgoing request headers
- out_headers = dict(request.headers)
- out_headers.pop("host", None)
- out_headers.pop("content-length", None)
-
- # Insert backend API key if configured
- if backend_key:
- out_headers["Authorization"] = f"Bearer {backend_key}"
+ return 60.0
+
+async def proxy_request(provider: dict, model_name: str, provider_id: str, body: dict, stream: bool):
+ base_url = provider["base_url"].rstrip("/")
+ api_key = provider["api_key"]
+ headers = {
+ "Authorization": f"Bearer {api_key}",
+ "Content-Type": "application/json",
+ }
+ body = {**body, "model": model_name}
+ url = f"{base_url}/chat/completions"
+
+ await rl_state.record_request(provider_id, model_name)
+ if stream:
+ async def stream_gen():
+ async with httpx.AsyncClient(timeout=TIMEOUT) as client:
+ async with client.stream("POST", url, json=body, headers=headers) as resp:
+ if resp.status_code == 429:
+ cooldown = _extract_retry_after(resp)
+ await rl_state.mark_rate_limited(provider_id, model_name, cooldown)
+ yield f"data: {json.dumps({'error': 'rate_limited', 'provider': provider_id})}\n\n"
+ return
+ async for chunk in resp.aiter_bytes():
+ yield chunk
+ return StreamingResponse(stream_gen(), media_type="text/event-stream")
+
+ async with httpx.AsyncClient(timeout=TIMEOUT) as client:
+ resp = await client.post(url, json=body, headers=headers)
+
+ if resp.status_code == 429:
+ cooldown = _extract_retry_after(resp)
+ await rl_state.mark_rate_limited(provider_id, model_name, cooldown)
+ raise HTTPException(status_code=429, detail=f"Upstream rate limit hit on {provider_id}/{model_name}. Retry after {cooldown}s.")
+
+ if resp.status_code >= 500:
+ raise HTTPException(status_code=502, detail=f"Upstream error from {provider_id}: {resp.status_code}")
+
+ if resp.status_code >= 400:
try:
- r = await client.request(
- method="POST",
- url=f"{backend_url}/chat/completions",
- headers=out_headers,
- content=body_bytes,
- params=request.query_params,
- )
-
- return Response(
- content=r.content,
- status_code=r.status_code,
- headers=dict(r.headers),
- )
- except Exception as e:
- print(f"Error proxying to {backend_url}: {e}")
- continue
-
- return JSONResponse({"error": "All backend servers failed to respond"}, status_code=503)
\ No newline at end of file
+ detail = resp.json()
+ except Exception:
+ detail = resp.text
+ raise HTTPException(status_code=resp.status_code, detail=detail)
+
+ return JSONResponse(content=resp.json())
+
+# ── App ────────────────────────────────────────────────────────────────────────
+
+@asynccontextmanager
+async def lifespan(app: FastAPI):
+ log.info("RyzGateway starting up")
+ if not DATA_FILE.exists():
+ save_data(_default_data())
+ log.info(f"Initialized empty data at {DATA_FILE}")
+ yield
+ log.info("RyzGateway shutting down")
+
+app = FastAPI(title="RyzGateway", lifespan=lifespan)
+
+# ── OpenAI-compatible endpoints ────────────────────────────────────────────────
+
+class ChatMessage(BaseModel):
+ role: str
+ content: Any
+
+class ChatRequest(BaseModel):
+ model: str
+ messages: List[ChatMessage]
+ stream: Optional[bool] = False
+ temperature: Optional[float] = None
+ max_tokens: Optional[int] = None
+ top_p: Optional[float] = None
+ # Pass through any extra fields
+ model_config = {"extra": "allow"}
+
+@app.post("/v1/chat/completions")
+async def chat_completions(request: ChatRequest, _key: str = Depends(require_api_key)):
+ data = load_data()
+ provider, model_name, provider_id = await resolve_endpoint(request.model, data)
+ body = request.model_dump(exclude_none=True, exclude={"model"})
+ # Include any extra fields passed through
+ body.update(request.model_extra or {})
+ return await proxy_request(provider, model_name, provider_id, body, request.stream or False)
+
+@app.get("/v1/models")
+async def list_models(_key: str = Depends(require_api_key)):
+ data = load_data()
+ now = int(time.time())
+ models = []
+
+ # Direct provider models (listed as provider_id/model if provider has known models)
+ for pid, p in data["providers"].items():
+ for m in p.get("known_models", []):
+ models.append({
+ "id": f"{pid}/{m}",
+ "object": "model",
+ "created": now,
+ "owned_by": pid,
+ })
+
+ # Pool models
+ for pool_id, pool in data["pool_models"].items():
+ models.append({
+ "id": pool["name"],
+ "object": "model",
+ "created": now,
+ "owned_by": "pool",
+ })
+
+ return {"object": "list", "data": models}
+
+# ── Admin API ──────────────────────────────────────────────────────────────────
+
+# --- API Keys ---
+
+@app.get("/admin/api-keys")
+async def get_api_keys(_=Depends(require_admin)):
+ data = load_data()
+ return list(data["api_keys"].values())
+
+@app.post("/admin/api-keys")
+async def create_api_key(body: dict, _=Depends(require_admin)):
+ data = load_data()
+ kid = str(uuid.uuid4())[:8]
+ key = "ryz-" + str(uuid.uuid4()).replace("-", "")
+ data["api_keys"][kid] = {
+ "id": kid,
+ "key": key,
+ "name": body.get("name", "Unnamed Key"),
+ "created_at": int(time.time()),
+ }
+ save_data(data)
+ return data["api_keys"][kid]
+
+@app.delete("/admin/api-keys/{kid}")
+async def delete_api_key(kid: str, _=Depends(require_admin)):
+ data = load_data()
+ if kid not in data["api_keys"]:
+ raise HTTPException(status_code=404, detail="Key not found")
+ del data["api_keys"][kid]
+ save_data(data)
+ return {"deleted": True}
+
+# --- Providers ---
+
+@app.get("/admin/providers")
+async def get_providers(_=Depends(require_admin)):
+ data = load_data()
+ # Return providers with masked API keys and live rate limit state
+ result = []
+ for pid, p in data["providers"].items():
+ entry = {**p, "id": pid}
+ raw = entry.get("api_key", "")
+ entry["api_key_masked"] = raw[:6] + "..." + raw[-4:] if len(raw) > 10 else "****"
+ # Don't send actual key to frontend
+ entry.pop("api_key", None)
+ # Add live cooldown info per known model
+ cooldowns = {}
+ for m in p.get("known_models", []):
+ cd = rl_state.get_cooldown_remaining(pid, m)
+ if cd > 0:
+ cooldowns[m] = round(cd, 1)
+ entry["active_cooldowns"] = cooldowns
+ result.append(entry)
+ return result
+
+@app.post("/admin/providers")
+async def create_provider(body: dict, _=Depends(require_admin)):
+ data = load_data()
+ pid = body.get("id") or re.sub(r"[^a-z0-9_-]", "-", body.get("name", "provider").lower())[:32]
+ if pid in data["providers"]:
+ raise HTTPException(status_code=409, detail=f"Provider ID '{pid}' already exists")
+ data["providers"][pid] = {
+ "name": body.get("name", pid),
+ "base_url": body.get("base_url", ""),
+ "api_key": body.get("api_key", ""),
+ "rate_type": body.get("rate_type", "none"),
+ "rate_limit": int(body.get("rate_limit", 0)),
+ "known_models": body.get("known_models", []),
+ "notes": body.get("notes", ""),
+ }
+ save_data(data)
+ return {"id": pid, **data["providers"][pid], "api_key": "[hidden]"}
+
+@app.put("/admin/providers/{pid}")
+async def update_provider(pid: str, body: dict, _=Depends(require_admin)):
+ data = load_data()
+ if pid not in data["providers"]:
+ raise HTTPException(status_code=404, detail="Provider not found")
+ p = data["providers"][pid]
+ for field in ["name", "base_url", "rate_type", "notes", "known_models"]:
+ if field in body:
+ p[field] = body[field]
+ if "rate_limit" in body:
+ p["rate_limit"] = int(body["rate_limit"])
+ if "api_key" in body and body["api_key"] and not body["api_key"].startswith("****"):
+ p["api_key"] = body["api_key"]
+ save_data(data)
+ return {"id": pid, **p, "api_key": "[hidden]"}
+
+@app.delete("/admin/providers/{pid}")
+async def delete_provider(pid: str, _=Depends(require_admin)):
+ data = load_data()
+ if pid not in data["providers"]:
+ raise HTTPException(status_code=404, detail="Provider not found")
+ del data["providers"][pid]
+ save_data(data)
+ return {"deleted": True}
+
+# --- Pool Models ---
+
+@app.get("/admin/pools")
+async def get_pools(_=Depends(require_admin)):
+ data = load_data()
+ result = []
+ for pool_id, pool in data["pool_models"].items():
+ entry = {**pool, "id": pool_id}
+ # Annotate members with live availability
+ annotated = []
+ for m in pool.get("members", []):
+ provider = data["providers"].get(m["provider_id"], {})
+ available = await rl_state.is_available(m["provider_id"], m["model_name"], provider)
+ cooldown = rl_state.get_cooldown_remaining(m["provider_id"], m["model_name"])
+ annotated.append({**m, "available": available, "cooldown_remaining": round(cooldown, 1)})
+ entry["members"] = annotated
+ result.append(entry)
+ return result
+
+@app.post("/admin/pools")
+async def create_pool(body: dict, _=Depends(require_admin)):
+ data = load_data()
+ pool_id = body.get("id") or re.sub(r"[^a-z0-9_-]", "-", body.get("name", "pool").lower())[:32]
+ if pool_id in data["pool_models"]:
+ raise HTTPException(status_code=409, detail=f"Pool ID '{pool_id}' already exists")
+ data["pool_models"][pool_id] = {
+ "name": body.get("name", pool_id),
+ "strategy": body.get("strategy", "round_robin"),
+ "members": body.get("members", []),
+ }
+ save_data(data)
+ return {"id": pool_id, **data["pool_models"][pool_id]}
+
+@app.put("/admin/pools/{pool_id}")
+async def update_pool(pool_id: str, body: dict, _=Depends(require_admin)):
+ data = load_data()
+ if pool_id not in data["pool_models"]:
+ raise HTTPException(status_code=404, detail="Pool not found")
+ p = data["pool_models"][pool_id]
+ for field in ["name", "strategy", "members"]:
+ if field in body:
+ p[field] = body[field]
+ save_data(data)
+ return {"id": pool_id, **p}
+
+@app.delete("/admin/pools/{pool_id}")
+async def delete_pool(pool_id: str, _=Depends(require_admin)):
+ data = load_data()
+ if pool_id not in data["pool_models"]:
+ raise HTTPException(status_code=404, detail="Pool not found")
+ del data["pool_models"][pool_id]
+ save_data(data)
+ return {"deleted": True}
+
+# --- Status ---
+
+@app.get("/admin/status")
+async def get_status(_=Depends(require_admin)):
+ data = load_data()
+ return {
+ "providers": len(data["providers"]),
+ "pools": len(data["pool_models"]),
+ "api_keys": len(data["api_keys"]),
+ }
+
+# ── Dashboard (embedded HTML) ──────────────────────────────────────────────────
+
+DASHBOARD_HTML = r"""
+
+
+
+
+RyzGateway
+
+
+
+
+
+
+
RyzGateway
+
Enter your admin key to access the dashboard.
+
+
+
+
+
+
Invalid key.
+
+
+
+
+
+
+
+
+
+
+
+
Dashboard
+
Gateway status and routing health
+
+
+
—
Providers
+
—
Model Pools
+
—
API Keys
+
+
+
Provider health
+
No providers configured
+
+
+
+
+
+
+
Providers
+
API endpoints with rate limit configuration
+
+
+
+
+
+
+ Provider ID is auto-generated from the name and used in model routing:
+ Use provider-id/exact-model-name in your API calls to route directly to this provider.
+
+
+
+
+
+
Provider
Base URL
Rate limit
Models
Status
+
+
Loading...
+
+
+
+
+
+
+
+
+
Model Pools
+
Generic model names that route across multiple providers
+
+
+
+
+
+
+ Pool names are generic model names exposed in /v1/models. The gateway routes requests using your chosen strategy and automatically skips rate-limited endpoints.
+
+
Loading...
+
+
+
+
+
+
API Keys
+
Keys issued to clients for /v1/ access
+
+
+
+
+
+
+
+
+
Name
Key
Created
+
Loading...
+
+
+
+
+
+
+
+
+
Usage docs
+
How to use RyzGateway in your apps
+
+
+
Base URL
+
+
Point any OpenAI-compatible client at this URL.
+
+
+
Model name formats
+
+
provider-id/exact-model-name — Direct passthrough. Requests go to that provider using the exact model string, no pool routing.
+
pool-name — Pool routing. Gateway picks the best available member based on your pool strategy (round-robin, priority, weighted).