2025-08-26 18:19:43 +02:00
|
|
|
|
"""
|
|
|
|
|
|
title: NOMYO Router - an Ollama Proxy with Endpoint:Model aware routing
|
|
|
|
|
|
author: alpha-nerd-nomyo
|
|
|
|
|
|
author_url: https://github.com/nomyo-ai
|
2025-11-28 14:59:29 +01:00
|
|
|
|
version: 0.5
|
2025-08-26 18:19:43 +02:00
|
|
|
|
license: AGPL
|
|
|
|
|
|
"""
|
|
|
|
|
|
# -------------------------------------------------------------
|
2026-01-14 09:28:02 +01:00
|
|
|
|
import orjson, time, asyncio, yaml, ollama, openai, os, re, aiohttp, ssl, random, base64, io, enhance, secrets
|
2025-11-18 11:16:21 +01:00
|
|
|
|
from datetime import datetime, timezone
|
2025-08-26 18:19:43 +02:00
|
|
|
|
from pathlib import Path
|
2026-01-05 17:16:31 +01:00
|
|
|
|
|
|
|
|
|
|
# Directory containing static files (relative to this script)
|
|
|
|
|
|
STATIC_DIR = Path(__file__).parent / "static"
|
2025-09-05 12:11:31 +02:00
|
|
|
|
from typing import Dict, Set, List, Optional
|
2026-01-14 09:28:02 +01:00
|
|
|
|
from urllib.parse import urlparse, parse_qsl, urlencode
|
2025-08-26 18:19:43 +02:00
|
|
|
|
from fastapi import FastAPI, Request, HTTPException
|
2025-09-05 12:11:31 +02:00
|
|
|
|
from fastapi_sse import sse_handler
|
2025-08-30 00:12:56 +02:00
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
2025-09-11 09:46:19 +02:00
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
2025-08-30 12:43:35 +02:00
|
|
|
|
from starlette.responses import StreamingResponse, JSONResponse, Response, HTMLResponse, RedirectResponse
|
2025-08-26 18:19:43 +02:00
|
|
|
|
from pydantic import Field
|
|
|
|
|
|
from pydantic_settings import BaseSettings
|
|
|
|
|
|
from collections import defaultdict
|
2025-09-24 11:46:38 +02:00
|
|
|
|
from PIL import Image
|
2025-09-01 13:38:49 +02:00
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
# In‑memory caches
|
|
|
|
|
|
# ------------------------------------------------------------------
|
2025-09-05 12:11:31 +02:00
|
|
|
|
# Successful results are cached for 300s
|
2025-09-01 13:38:49 +02:00
|
|
|
|
_models_cache: dict[str, tuple[Set[str], float]] = {}
|
2025-11-17 14:40:24 +01:00
|
|
|
|
_loaded_models_cache: dict[str, tuple[Set[str], float]] = {}
|
2025-09-05 12:11:31 +02:00
|
|
|
|
# Transient errors are cached for 1s – the key stays until the
|
2025-09-01 13:38:49 +02:00
|
|
|
|
# timeout expires, after which the endpoint will be queried again.
|
|
|
|
|
|
_error_cache: dict[str, float] = {}
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
2025-09-05 12:11:31 +02:00
|
|
|
|
# ------------------------------------------------------------------
|
2025-11-10 15:37:46 +01:00
|
|
|
|
# Queues
|
2025-09-05 12:11:31 +02:00
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
_subscribers: Set[asyncio.Queue] = set()
|
|
|
|
|
|
_subscribers_lock = asyncio.Lock()
|
2025-11-10 15:37:46 +01:00
|
|
|
|
token_queue: asyncio.Queue[tuple[str, str, int, int]] = asyncio.Queue()
|
2025-09-05 12:11:31 +02:00
|
|
|
|
|
2026-01-14 09:28:02 +01:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
# Secret handling
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
def _mask_secrets(text: str) -> str:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Mask common API key patterns to avoid leaking secrets in logs or error payloads.
|
|
|
|
|
|
"""
|
|
|
|
|
|
if not text:
|
|
|
|
|
|
return text
|
|
|
|
|
|
# OpenAI-style keys (sk-...) and generic "api key" mentions
|
|
|
|
|
|
text = re.sub(r"sk-[A-Za-z0-9]{4}[A-Za-z0-9_-]*", "sk-***redacted***", text)
|
|
|
|
|
|
text = re.sub(r"(?i)(api[-_ ]key\\s*[:=]\\s*)([^\\s]+)", r"\\1***redacted***", text)
|
|
|
|
|
|
return text
|
|
|
|
|
|
|
2025-09-10 10:21:49 +02:00
|
|
|
|
# ------------------------------------------------------------------
|
2025-11-13 10:13:10 +01:00
|
|
|
|
# Globals
|
2025-09-10 10:21:49 +02:00
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
app_state = {
|
|
|
|
|
|
"session": None,
|
|
|
|
|
|
"connector": None,
|
|
|
|
|
|
}
|
2025-11-13 10:13:10 +01:00
|
|
|
|
token_worker_task: asyncio.Task | None = None
|
2025-11-18 11:16:21 +01:00
|
|
|
|
flush_task: asyncio.Task | None = None
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
# Token Count Buffer (for write-behind pattern)
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
# Structure: {endpoint: {model: (input_tokens, output_tokens)}}
|
2025-12-02 12:18:23 +01:00
|
|
|
|
token_buffer: dict[str, dict[str, tuple[int, int]]] = defaultdict(lambda: defaultdict(lambda: (0, 0)))
|
2025-11-18 11:16:21 +01:00
|
|
|
|
# Time series buffer with timestamp
|
|
|
|
|
|
time_series_buffer: list[dict[str, int | str]] = []
|
2026-01-05 17:16:31 +01:00
|
|
|
|
# Lock to protect buffer access from race conditions
|
|
|
|
|
|
buffer_lock = asyncio.Lock()
|
2025-11-18 11:16:21 +01:00
|
|
|
|
|
|
|
|
|
|
# Configuration for periodic flushing
|
|
|
|
|
|
FLUSH_INTERVAL = 10 # seconds
|
2025-09-10 10:21:49 +02:00
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
# 1. Configuration loader
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
class Config(BaseSettings):
|
|
|
|
|
|
# List of Ollama endpoints
|
|
|
|
|
|
endpoints: list[str] = Field(
|
|
|
|
|
|
default_factory=lambda: [
|
|
|
|
|
|
"http://localhost:11434",
|
|
|
|
|
|
]
|
|
|
|
|
|
)
|
|
|
|
|
|
# Max concurrent connections per endpoint‑model pair, see OLLAMA_NUM_PARALLEL
|
|
|
|
|
|
max_concurrent_connections: int = 1
|
|
|
|
|
|
|
2025-09-05 12:11:31 +02:00
|
|
|
|
api_keys: Dict[str, str] = Field(default_factory=dict)
|
2026-01-14 09:28:02 +01:00
|
|
|
|
# Optional router-level API key used to gate access to this service and dashboard
|
|
|
|
|
|
router_api_key: Optional[str] = Field(default=None, env="NOMYO_ROUTER_API_KEY")
|
2025-09-05 12:11:31 +02:00
|
|
|
|
|
2025-11-18 11:16:21 +01:00
|
|
|
|
# Database configuration
|
|
|
|
|
|
db_path: str = Field(default=os.getenv("NOMYO_ROUTER_DB_PATH", "token_counts.db"))
|
|
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
class Config:
|
|
|
|
|
|
# Load from `config.yaml` first, then from env variables
|
2025-09-05 12:11:31 +02:00
|
|
|
|
env_prefix = "NOMYO_ROUTER_"
|
2025-08-26 18:19:43 +02:00
|
|
|
|
yaml_file = Path("config.yaml") # relative to cwd
|
|
|
|
|
|
|
2025-09-05 12:11:31 +02:00
|
|
|
|
@classmethod
|
|
|
|
|
|
def _expand_env_refs(cls, obj):
|
|
|
|
|
|
"""Recursively replace `${VAR}` with os.getenv('VAR')."""
|
|
|
|
|
|
if isinstance(obj, dict):
|
|
|
|
|
|
return {k: cls._expand_env_refs(v) for k, v in obj.items()}
|
|
|
|
|
|
if isinstance(obj, list):
|
|
|
|
|
|
return [cls._expand_env_refs(v) for v in obj]
|
|
|
|
|
|
if isinstance(obj, str):
|
|
|
|
|
|
# Only expand if it is exactly ${VAR}
|
|
|
|
|
|
m = re.fullmatch(r"\$\{([A-Za-z_][A-Za-z0-9_]*)\}", obj)
|
|
|
|
|
|
if m:
|
|
|
|
|
|
return os.getenv(m.group(1), "")
|
|
|
|
|
|
return obj
|
|
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
@classmethod
|
|
|
|
|
|
def from_yaml(cls, path: Path) -> "Config":
|
|
|
|
|
|
"""Load the YAML file and create the Config instance."""
|
|
|
|
|
|
if path.exists():
|
|
|
|
|
|
with path.open("r", encoding="utf-8") as fp:
|
|
|
|
|
|
data = yaml.safe_load(fp) or {}
|
2025-09-05 12:11:31 +02:00
|
|
|
|
cleaned = cls._expand_env_refs(data)
|
2026-01-14 09:28:02 +01:00
|
|
|
|
if isinstance(cleaned, dict):
|
|
|
|
|
|
# Accept hyphenated config key and map it to the field name
|
|
|
|
|
|
key_aliases = [
|
|
|
|
|
|
# canonical field name
|
|
|
|
|
|
"router_api_key",
|
|
|
|
|
|
# lowercase, hyphen/underscore variants
|
|
|
|
|
|
"nomyo-router-api-key",
|
|
|
|
|
|
"nomyo_router_api_key",
|
|
|
|
|
|
"nomyo-router_api_key",
|
|
|
|
|
|
"nomyo_router-api_key",
|
|
|
|
|
|
# uppercase env-style variants
|
|
|
|
|
|
"NOMYO-ROUTER_API_KEY",
|
|
|
|
|
|
"NOMYO_ROUTER_API_KEY",
|
|
|
|
|
|
]
|
|
|
|
|
|
for alias in key_aliases:
|
|
|
|
|
|
if alias in cleaned:
|
|
|
|
|
|
cleaned["router_api_key"] = cleaned.get("router_api_key", cleaned.pop(alias))
|
|
|
|
|
|
break
|
|
|
|
|
|
# If not present in YAML (or empty), fall back to env var explicitly
|
|
|
|
|
|
if not cleaned.get("router_api_key"):
|
|
|
|
|
|
env_key = os.getenv("NOMYO_ROUTER_API_KEY")
|
|
|
|
|
|
if env_key:
|
|
|
|
|
|
cleaned["router_api_key"] = env_key
|
2025-09-05 12:11:31 +02:00
|
|
|
|
return cls(**cleaned)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
return cls()
|
|
|
|
|
|
|
2025-11-07 13:59:16 +01:00
|
|
|
|
def _config_path_from_env() -> Path:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Resolve the configuration file path. Defaults to `config.yaml`
|
|
|
|
|
|
in the current working directory unless NOMYO_ROUTER_CONFIG_PATH
|
|
|
|
|
|
is set.
|
|
|
|
|
|
"""
|
|
|
|
|
|
candidate = os.getenv("NOMYO_ROUTER_CONFIG_PATH")
|
|
|
|
|
|
if candidate:
|
|
|
|
|
|
return Path(candidate).expanduser()
|
|
|
|
|
|
return Path("config.yaml")
|
|
|
|
|
|
|
2025-11-18 11:16:21 +01:00
|
|
|
|
from db import TokenDatabase
|
|
|
|
|
|
|
2025-11-07 13:59:16 +01:00
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# Create the global config object – it will be overwritten on startup
|
2025-11-07 13:59:16 +01:00
|
|
|
|
config = Config.from_yaml(_config_path_from_env())
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
# 2. FastAPI application
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
app = FastAPI()
|
2025-09-05 12:11:31 +02:00
|
|
|
|
sse_handler.app = app
|
2025-09-11 09:46:19 +02:00
|
|
|
|
app.add_middleware(
|
|
|
|
|
|
CORSMiddleware,
|
|
|
|
|
|
allow_origins=["*"],
|
|
|
|
|
|
allow_credentials=True,
|
|
|
|
|
|
allow_methods=["GET", "POST", "DELETE"],
|
|
|
|
|
|
allow_headers=["Authorization", "Content-Type"],
|
|
|
|
|
|
)
|
2025-09-21 16:20:36 +02:00
|
|
|
|
default_headers={
|
2025-10-28 11:08:52 +01:00
|
|
|
|
"HTTP-Referer": "https://nomyo.ai",
|
|
|
|
|
|
"X-Title": "NOMYO Router",
|
|
|
|
|
|
}
|
2025-09-21 16:20:36 +02:00
|
|
|
|
|
2026-01-14 09:28:02 +01:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
# Router-level authentication (optional)
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
def _extract_router_api_key(request: Request) -> Optional[str]:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Extract the provided router API key from the Authorization header or `api_key`
|
|
|
|
|
|
query parameter. The middleware uses this to gate access to API routes when
|
|
|
|
|
|
a router_api_key is configured.
|
|
|
|
|
|
"""
|
|
|
|
|
|
auth_header = request.headers.get("Authorization")
|
|
|
|
|
|
if auth_header and auth_header.lower().startswith("bearer "):
|
|
|
|
|
|
return auth_header.split(" ", 1)[1].strip()
|
|
|
|
|
|
query_key = request.query_params.get("api_key")
|
|
|
|
|
|
if query_key:
|
|
|
|
|
|
return query_key
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _strip_api_key_from_scope(request: Request) -> None:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Remove api_key from the ASGI scope query string to avoid leaking it in logs.
|
|
|
|
|
|
"""
|
|
|
|
|
|
scope = request.scope
|
|
|
|
|
|
raw_qs = scope.get("query_string", b"")
|
|
|
|
|
|
if not raw_qs:
|
|
|
|
|
|
return
|
|
|
|
|
|
params = parse_qsl(raw_qs.decode("utf-8"), keep_blank_values=True)
|
|
|
|
|
|
filtered = [(k, v) for (k, v) in params if k != "api_key"]
|
|
|
|
|
|
scope["query_string"] = urlencode(filtered).encode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.middleware("http")
|
|
|
|
|
|
async def enforce_router_api_key(request: Request, call_next):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Enforce the optional NOMYO Router API key for all non-static requests.
|
|
|
|
|
|
When `config.router_api_key` is set, clients must supply the key either in
|
|
|
|
|
|
the Authorization header (`Bearer <key>`) or as `api_key` query parameter.
|
|
|
|
|
|
"""
|
|
|
|
|
|
expected_key = config.router_api_key
|
|
|
|
|
|
if not expected_key or request.method == "OPTIONS":
|
|
|
|
|
|
return await call_next(request)
|
|
|
|
|
|
|
|
|
|
|
|
path = request.url.path
|
|
|
|
|
|
if path.startswith("/static") or path in {"/", "/favicon.ico"}:
|
|
|
|
|
|
return await call_next(request)
|
|
|
|
|
|
|
|
|
|
|
|
provided_key = _extract_router_api_key(request)
|
|
|
|
|
|
# Strip the api_key query param from scope so access logs do not leak it
|
|
|
|
|
|
_strip_api_key_from_scope(request)
|
|
|
|
|
|
if provided_key is None:
|
|
|
|
|
|
return JSONResponse(
|
|
|
|
|
|
content={"detail": "Missing NOMYO Router API key"},
|
|
|
|
|
|
status_code=401,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if not secrets.compare_digest(str(provided_key), str(expected_key)):
|
|
|
|
|
|
return JSONResponse(
|
|
|
|
|
|
content={"detail": "Invalid NOMYO Router API key"},
|
|
|
|
|
|
status_code=403,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return await call_next(request)
|
|
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
# 3. Global state: per‑endpoint per‑model active connection counters
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
usage_counts: Dict[str, Dict[str, int]] = defaultdict(lambda: defaultdict(int))
|
2025-11-04 17:55:19 +01:00
|
|
|
|
token_usage_counts: Dict[str, Dict[str, int]] = defaultdict(lambda: defaultdict(int))
|
2025-08-26 18:19:43 +02:00
|
|
|
|
usage_lock = asyncio.Lock() # protects access to usage_counts
|
2025-11-10 15:37:46 +01:00
|
|
|
|
token_usage_lock = asyncio.Lock()
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
2025-11-18 11:16:21 +01:00
|
|
|
|
# Database instance
|
|
|
|
|
|
db: "TokenDatabase" = None
|
|
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
# 4. Helperfunctions
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-09-01 13:38:49 +02:00
|
|
|
|
def _is_fresh(cached_at: float, ttl: int) -> bool:
|
|
|
|
|
|
return (time.time() - cached_at) < ttl
|
|
|
|
|
|
|
2025-09-09 17:08:00 +02:00
|
|
|
|
async def _ensure_success(resp: aiohttp.ClientResponse) -> None:
|
|
|
|
|
|
if resp.status >= 400:
|
|
|
|
|
|
text = await resp.text()
|
2026-01-14 09:28:02 +01:00
|
|
|
|
raise HTTPException(status_code=resp.status, detail=_mask_secrets(text))
|
2025-10-30 09:06:21 +01:00
|
|
|
|
|
2025-11-07 13:59:16 +01:00
|
|
|
|
def _format_connection_issue(url: str, error: Exception) -> str:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Provide a human-friendly error string for connection failures so operators
|
|
|
|
|
|
know which endpoint and address failed from inside the container.
|
|
|
|
|
|
"""
|
|
|
|
|
|
parsed = urlparse(url)
|
|
|
|
|
|
host_hint = parsed.hostname or ""
|
|
|
|
|
|
port_hint = parsed.port or ""
|
|
|
|
|
|
|
|
|
|
|
|
if isinstance(error, aiohttp.ClientConnectorError):
|
|
|
|
|
|
resolved_host = getattr(error, "host", host_hint) or host_hint or "?"
|
|
|
|
|
|
resolved_port = getattr(error, "port", port_hint) or port_hint or "?"
|
|
|
|
|
|
parts = [
|
|
|
|
|
|
f"Failed to connect to {url} (resolved: {resolved_host}:{resolved_port}).",
|
|
|
|
|
|
"Ensure the endpoint address is reachable from within the container.",
|
|
|
|
|
|
]
|
|
|
|
|
|
if resolved_host in {"localhost", "127.0.0.1"}:
|
|
|
|
|
|
parts.append(
|
|
|
|
|
|
"Inside Docker, 'localhost' refers to the container itself; use "
|
|
|
|
|
|
"'host.docker.internal' or a Docker network alias if the service "
|
|
|
|
|
|
"runs on the host machine."
|
|
|
|
|
|
)
|
|
|
|
|
|
os_error = getattr(error, "os_error", None)
|
|
|
|
|
|
if isinstance(os_error, OSError):
|
|
|
|
|
|
errno = getattr(os_error, "errno", None)
|
|
|
|
|
|
strerror = os_error.strerror or str(os_error)
|
|
|
|
|
|
if errno is not None or strerror:
|
|
|
|
|
|
parts.append(f"OS error [{errno}]: {strerror}.")
|
|
|
|
|
|
elif os_error:
|
|
|
|
|
|
parts.append(f"OS error: {os_error}.")
|
|
|
|
|
|
parts.append(f"Original error: {error}.")
|
|
|
|
|
|
return " ".join(parts)
|
|
|
|
|
|
|
|
|
|
|
|
if isinstance(error, asyncio.TimeoutError):
|
|
|
|
|
|
return (
|
|
|
|
|
|
f"Timed out waiting for {url}. "
|
|
|
|
|
|
"The remote endpoint may be offline or slow to respond."
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return f"Error while contacting {url}: {error}"
|
2025-11-18 11:16:21 +01:00
|
|
|
|
|
2025-10-30 09:06:21 +01:00
|
|
|
|
def is_ext_openai_endpoint(endpoint: str) -> bool:
|
|
|
|
|
|
if "/v1" not in endpoint:
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
base_endpoint = endpoint.replace('/v1', '')
|
|
|
|
|
|
if base_endpoint in config.endpoints:
|
|
|
|
|
|
return False # It's Ollama's /v1
|
|
|
|
|
|
|
|
|
|
|
|
# Check for default Ollama port
|
|
|
|
|
|
if ':11434' in endpoint:
|
|
|
|
|
|
return False # It's Ollama
|
|
|
|
|
|
|
|
|
|
|
|
return True # It's an external OpenAI endpoint
|
2025-09-01 11:07:07 +02:00
|
|
|
|
|
2025-11-10 15:37:46 +01:00
|
|
|
|
async def token_worker() -> None:
|
2026-01-05 17:16:31 +01:00
|
|
|
|
try:
|
|
|
|
|
|
while True:
|
|
|
|
|
|
endpoint, model, prompt, comp = await token_queue.get()
|
|
|
|
|
|
# Accumulate counts in memory buffer (protected by lock)
|
|
|
|
|
|
async with buffer_lock:
|
|
|
|
|
|
token_buffer[endpoint][model] = (
|
|
|
|
|
|
token_buffer[endpoint].get(model, (0, 0))[0] + prompt,
|
|
|
|
|
|
token_buffer[endpoint].get(model, (0, 0))[1] + comp
|
|
|
|
|
|
)
|
2025-11-18 11:16:21 +01:00
|
|
|
|
|
2026-01-05 17:16:31 +01:00
|
|
|
|
# Add to time series buffer with timestamp (UTC)
|
|
|
|
|
|
now = datetime.now(tz=timezone.utc)
|
|
|
|
|
|
timestamp = int(datetime(now.year, now.month, now.day, now.hour, now.minute, tzinfo=timezone.utc).timestamp())
|
|
|
|
|
|
time_series_buffer.append({
|
|
|
|
|
|
'endpoint': endpoint,
|
|
|
|
|
|
'model': model,
|
|
|
|
|
|
'input_tokens': prompt,
|
|
|
|
|
|
'output_tokens': comp,
|
|
|
|
|
|
'total_tokens': prompt + comp,
|
|
|
|
|
|
'timestamp': timestamp
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
# Update in-memory counts for immediate reporting
|
|
|
|
|
|
async with token_usage_lock:
|
|
|
|
|
|
token_usage_counts[endpoint][model] += (prompt + comp)
|
|
|
|
|
|
await publish_snapshot()
|
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
|
# Gracefully handle task cancellation during shutdown
|
|
|
|
|
|
print("[token_worker] Task cancelled, processing remaining queue items...")
|
|
|
|
|
|
# Process any remaining items in the queue before exiting
|
|
|
|
|
|
while not token_queue.empty():
|
|
|
|
|
|
try:
|
|
|
|
|
|
endpoint, model, prompt, comp = token_queue.get_nowait()
|
|
|
|
|
|
async with buffer_lock:
|
|
|
|
|
|
token_buffer[endpoint][model] = (
|
|
|
|
|
|
token_buffer[endpoint].get(model, (0, 0))[0] + prompt,
|
|
|
|
|
|
token_buffer[endpoint].get(model, (0, 0))[1] + comp
|
|
|
|
|
|
)
|
|
|
|
|
|
now = datetime.now(tz=timezone.utc)
|
|
|
|
|
|
timestamp = int(datetime(now.year, now.month, now.day, now.hour, now.minute, tzinfo=timezone.utc).timestamp())
|
|
|
|
|
|
time_series_buffer.append({
|
|
|
|
|
|
'endpoint': endpoint,
|
|
|
|
|
|
'model': model,
|
|
|
|
|
|
'input_tokens': prompt,
|
|
|
|
|
|
'output_tokens': comp,
|
|
|
|
|
|
'total_tokens': prompt + comp,
|
|
|
|
|
|
'timestamp': timestamp
|
|
|
|
|
|
})
|
|
|
|
|
|
async with token_usage_lock:
|
|
|
|
|
|
token_usage_counts[endpoint][model] += (prompt + comp)
|
|
|
|
|
|
await publish_snapshot()
|
|
|
|
|
|
except asyncio.QueueEmpty:
|
|
|
|
|
|
break
|
|
|
|
|
|
print("[token_worker] Task cancelled, remaining items processed.")
|
|
|
|
|
|
raise
|
2025-11-18 11:16:21 +01:00
|
|
|
|
|
|
|
|
|
|
async def flush_buffer() -> None:
|
|
|
|
|
|
"""Periodically flush accumulated token counts to the database."""
|
2026-01-05 17:16:31 +01:00
|
|
|
|
try:
|
|
|
|
|
|
while True:
|
|
|
|
|
|
await asyncio.sleep(FLUSH_INTERVAL)
|
|
|
|
|
|
|
|
|
|
|
|
# Flush token counts and time series (protected by lock)
|
|
|
|
|
|
async with buffer_lock:
|
|
|
|
|
|
if token_buffer:
|
|
|
|
|
|
# Copy buffer before releasing lock for DB operation
|
|
|
|
|
|
buffer_copy = {ep: dict(models) for ep, models in token_buffer.items()}
|
|
|
|
|
|
token_buffer.clear()
|
|
|
|
|
|
else:
|
|
|
|
|
|
buffer_copy = None
|
2025-11-18 11:16:21 +01:00
|
|
|
|
|
2026-01-05 17:16:31 +01:00
|
|
|
|
if time_series_buffer:
|
|
|
|
|
|
ts_copy = list(time_series_buffer)
|
|
|
|
|
|
time_series_buffer.clear()
|
|
|
|
|
|
else:
|
|
|
|
|
|
ts_copy = None
|
|
|
|
|
|
|
|
|
|
|
|
# Perform DB operations outside the lock to avoid blocking
|
|
|
|
|
|
if buffer_copy:
|
|
|
|
|
|
await db.update_batched_counts(buffer_copy)
|
|
|
|
|
|
if ts_copy:
|
|
|
|
|
|
await db.add_batched_time_series(ts_copy)
|
|
|
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
|
# Gracefully handle task cancellation during shutdown
|
|
|
|
|
|
print("[flush_buffer] Task cancelled, flushing remaining buffers...")
|
|
|
|
|
|
# Flush any remaining data before exiting
|
|
|
|
|
|
try:
|
|
|
|
|
|
async with buffer_lock:
|
|
|
|
|
|
if token_buffer:
|
|
|
|
|
|
buffer_copy = {ep: dict(models) for ep, models in token_buffer.items()}
|
|
|
|
|
|
token_buffer.clear()
|
|
|
|
|
|
else:
|
|
|
|
|
|
buffer_copy = None
|
|
|
|
|
|
if time_series_buffer:
|
|
|
|
|
|
ts_copy = list(time_series_buffer)
|
|
|
|
|
|
time_series_buffer.clear()
|
|
|
|
|
|
else:
|
|
|
|
|
|
ts_copy = None
|
|
|
|
|
|
if buffer_copy:
|
|
|
|
|
|
await db.update_batched_counts(buffer_copy)
|
|
|
|
|
|
if ts_copy:
|
|
|
|
|
|
await db.add_batched_time_series(ts_copy)
|
|
|
|
|
|
print("[flush_buffer] Task cancelled, remaining buffers flushed.")
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
print(f"[flush_buffer] Error during shutdown flush: {e}")
|
|
|
|
|
|
raise
|
2025-11-04 17:55:19 +01:00
|
|
|
|
|
2025-12-02 12:18:23 +01:00
|
|
|
|
async def flush_remaining_buffers() -> None:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Flush any in-memory buffers to the database on shutdown.
|
|
|
|
|
|
This is designed to be safely invoked during shutdown and should not raise.
|
|
|
|
|
|
"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
flushed_entries = 0
|
2026-01-05 17:16:31 +01:00
|
|
|
|
async with buffer_lock:
|
|
|
|
|
|
if token_buffer:
|
|
|
|
|
|
buffer_copy = {ep: dict(models) for ep, models in token_buffer.items()}
|
|
|
|
|
|
flushed_entries += sum(len(v) for v in token_buffer.values())
|
|
|
|
|
|
token_buffer.clear()
|
|
|
|
|
|
else:
|
|
|
|
|
|
buffer_copy = None
|
|
|
|
|
|
if time_series_buffer:
|
|
|
|
|
|
ts_copy = list(time_series_buffer)
|
|
|
|
|
|
flushed_entries += len(time_series_buffer)
|
|
|
|
|
|
time_series_buffer.clear()
|
|
|
|
|
|
else:
|
|
|
|
|
|
ts_copy = None
|
|
|
|
|
|
# Perform DB operations outside the lock
|
|
|
|
|
|
if buffer_copy:
|
|
|
|
|
|
await db.update_batched_counts(buffer_copy)
|
|
|
|
|
|
if ts_copy:
|
|
|
|
|
|
await db.add_batched_time_series(ts_copy)
|
2025-12-02 12:18:23 +01:00
|
|
|
|
if flushed_entries:
|
|
|
|
|
|
print(f"[shutdown] Flushed {flushed_entries} in-memory entries to DB on shutdown.")
|
|
|
|
|
|
else:
|
|
|
|
|
|
print("[shutdown] No in-memory entries to flush on shutdown.")
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
# Do not raise during shutdown – log and continue teardown
|
|
|
|
|
|
print(f"[shutdown] Error flushing remaining buffers: {e}")
|
|
|
|
|
|
|
2025-09-13 16:57:09 +02:00
|
|
|
|
class fetch:
|
|
|
|
|
|
async def available_models(endpoint: str, api_key: Optional[str] = None) -> Set[str]:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Query <endpoint>/api/tags and return a set of all model names that the
|
|
|
|
|
|
endpoint *advertises* (i.e. is capable of serving). This endpoint lists
|
|
|
|
|
|
every model that is installed on the Ollama instance, regardless of
|
|
|
|
|
|
whether the model is currently loaded into memory.
|
|
|
|
|
|
|
|
|
|
|
|
If the request fails (e.g. timeout, 5xx, or malformed response), an empty
|
|
|
|
|
|
set is returned.
|
|
|
|
|
|
"""
|
|
|
|
|
|
headers = None
|
|
|
|
|
|
if api_key is not None:
|
|
|
|
|
|
headers = {"Authorization": "Bearer " + api_key}
|
|
|
|
|
|
|
|
|
|
|
|
if endpoint in _models_cache:
|
|
|
|
|
|
models, cached_at = _models_cache[endpoint]
|
|
|
|
|
|
if _is_fresh(cached_at, 300):
|
|
|
|
|
|
return models
|
|
|
|
|
|
else:
|
|
|
|
|
|
# stale entry – drop it
|
|
|
|
|
|
del _models_cache[endpoint]
|
2025-08-29 13:13:25 +02:00
|
|
|
|
|
2025-09-13 16:57:09 +02:00
|
|
|
|
if endpoint in _error_cache:
|
2025-11-17 14:40:24 +01:00
|
|
|
|
if _is_fresh(_error_cache[endpoint], 10):
|
2025-09-13 16:57:09 +02:00
|
|
|
|
# Still within the short error TTL – pretend nothing is available
|
|
|
|
|
|
return set()
|
|
|
|
|
|
else:
|
|
|
|
|
|
# Error expired – remove it
|
|
|
|
|
|
del _error_cache[endpoint]
|
2025-09-05 12:11:31 +02:00
|
|
|
|
|
2025-09-13 16:57:09 +02:00
|
|
|
|
if "/v1" in endpoint:
|
|
|
|
|
|
endpoint_url = f"{endpoint}/models"
|
|
|
|
|
|
key = "data"
|
2025-09-01 13:38:49 +02:00
|
|
|
|
else:
|
2025-09-13 16:57:09 +02:00
|
|
|
|
endpoint_url = f"{endpoint}/api/tags"
|
|
|
|
|
|
key = "models"
|
|
|
|
|
|
client: aiohttp.ClientSession = app_state["session"]
|
|
|
|
|
|
try:
|
|
|
|
|
|
async with client.get(endpoint_url, headers=headers) as resp:
|
|
|
|
|
|
await _ensure_success(resp)
|
|
|
|
|
|
data = await resp.json()
|
|
|
|
|
|
|
|
|
|
|
|
items = data.get(key, [])
|
|
|
|
|
|
models = {item.get("id") or item.get("name") for item in items if item.get("id") or item.get("name")}
|
|
|
|
|
|
|
|
|
|
|
|
if models:
|
|
|
|
|
|
_models_cache[endpoint] = (models, time.time())
|
|
|
|
|
|
return models
|
|
|
|
|
|
else:
|
|
|
|
|
|
# Empty list – treat as “no models”, but still cache for 300s
|
|
|
|
|
|
_models_cache[endpoint] = (models, time.time())
|
|
|
|
|
|
return models
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
# Treat any error as if the endpoint offers no models
|
2025-11-07 13:59:16 +01:00
|
|
|
|
message = _format_connection_issue(endpoint_url, e)
|
|
|
|
|
|
print(f"[fetch.available_models] {message}")
|
2025-09-13 16:57:09 +02:00
|
|
|
|
_error_cache[endpoint] = time.time()
|
2025-09-01 13:38:49 +02:00
|
|
|
|
return set()
|
|
|
|
|
|
|
2025-09-09 17:08:00 +02:00
|
|
|
|
|
2025-09-13 16:57:09 +02:00
|
|
|
|
async def loaded_models(endpoint: str) -> Set[str]:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Query <endpoint>/api/ps and return a set of model names that are currently
|
|
|
|
|
|
loaded on that endpoint. If the request fails (e.g. timeout, 5xx), an empty
|
|
|
|
|
|
set is returned.
|
|
|
|
|
|
"""
|
2025-11-10 15:37:46 +01:00
|
|
|
|
if is_ext_openai_endpoint(endpoint):
|
|
|
|
|
|
return set()
|
2025-11-17 14:40:24 +01:00
|
|
|
|
if endpoint in _loaded_models_cache:
|
|
|
|
|
|
models, cached_at = _loaded_models_cache[endpoint]
|
|
|
|
|
|
if _is_fresh(cached_at, 30):
|
|
|
|
|
|
return models
|
|
|
|
|
|
else:
|
|
|
|
|
|
# stale entry – drop it
|
|
|
|
|
|
del _loaded_models_cache[endpoint]
|
|
|
|
|
|
|
|
|
|
|
|
if endpoint in _error_cache:
|
|
|
|
|
|
if _is_fresh(_error_cache[endpoint], 10):
|
|
|
|
|
|
return set()
|
|
|
|
|
|
else:
|
|
|
|
|
|
del _error_cache[endpoint]
|
|
|
|
|
|
client: aiohttp.ClientSession = app_state["session"]
|
2025-09-13 16:57:09 +02:00
|
|
|
|
try:
|
|
|
|
|
|
async with client.get(f"{endpoint}/api/ps") as resp:
|
|
|
|
|
|
await _ensure_success(resp)
|
|
|
|
|
|
data = await resp.json()
|
|
|
|
|
|
# The response format is:
|
|
|
|
|
|
# {"models": [{"name": "model1"}, {"name": "model2"}]}
|
|
|
|
|
|
models = {m.get("name") for m in data.get("models", []) if m.get("name")}
|
2025-11-17 14:40:24 +01:00
|
|
|
|
_loaded_models_cache[endpoint] = (models, time.time())
|
2025-09-13 16:57:09 +02:00
|
|
|
|
return models
|
2025-11-07 13:59:16 +01:00
|
|
|
|
except Exception as e:
|
2025-09-13 16:57:09 +02:00
|
|
|
|
# If anything goes wrong we simply assume the endpoint has no models
|
2025-11-07 13:59:16 +01:00
|
|
|
|
message = _format_connection_issue(f"{endpoint}/api/ps", e)
|
|
|
|
|
|
print(f"[fetch.loaded_models] {message}")
|
2025-09-13 16:57:09 +02:00
|
|
|
|
return set()
|
2025-08-29 13:13:25 +02:00
|
|
|
|
|
2025-09-13 16:57:09 +02:00
|
|
|
|
async def endpoint_details(endpoint: str, route: str, detail: str, api_key: Optional[str] = None) -> List[dict]:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Query <endpoint>/<route> to fetch <detail> and return a List of dicts with details
|
|
|
|
|
|
for the corresponding Ollama endpoint. If the request fails we respond with "N/A" for detail.
|
|
|
|
|
|
"""
|
|
|
|
|
|
client: aiohttp.ClientSession = app_state["session"]
|
|
|
|
|
|
headers = None
|
|
|
|
|
|
if api_key is not None:
|
|
|
|
|
|
headers = {"Authorization": "Bearer " + api_key}
|
|
|
|
|
|
|
2025-11-07 13:59:16 +01:00
|
|
|
|
request_url = f"{endpoint}{route}"
|
2025-09-13 16:57:09 +02:00
|
|
|
|
try:
|
2025-11-07 13:59:16 +01:00
|
|
|
|
async with client.get(request_url, headers=headers) as resp:
|
2025-09-13 16:57:09 +02:00
|
|
|
|
await _ensure_success(resp)
|
|
|
|
|
|
data = await resp.json()
|
|
|
|
|
|
detail = data.get(detail, [])
|
|
|
|
|
|
return detail
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
# If anything goes wrong we cannot reply details
|
2025-11-07 13:59:16 +01:00
|
|
|
|
message = _format_connection_issue(request_url, e)
|
|
|
|
|
|
print(f"[fetch.endpoint_details] {message}")
|
2025-09-13 16:57:09 +02:00
|
|
|
|
return []
|
2025-09-01 11:07:07 +02:00
|
|
|
|
|
2025-09-13 18:11:05 +02:00
|
|
|
|
def ep2base(ep):
|
|
|
|
|
|
if "/v1" in ep:
|
|
|
|
|
|
base_url = ep
|
|
|
|
|
|
else:
|
|
|
|
|
|
base_url = ep+"/v1"
|
|
|
|
|
|
return base_url
|
2025-08-30 00:12:56 +02:00
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
def dedupe_on_keys(dicts, key_fields):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Helper function to deduplicate endpoint details based on given dict keys.
|
|
|
|
|
|
"""
|
|
|
|
|
|
seen = set()
|
|
|
|
|
|
out = []
|
|
|
|
|
|
for d in dicts:
|
|
|
|
|
|
# Build a tuple of the values for the chosen keys
|
|
|
|
|
|
key = tuple(d.get(k) for k in key_fields)
|
|
|
|
|
|
if key not in seen:
|
|
|
|
|
|
seen.add(key)
|
|
|
|
|
|
out.append(d)
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
2025-08-29 13:13:25 +02:00
|
|
|
|
async def increment_usage(endpoint: str, model: str) -> None:
|
|
|
|
|
|
async with usage_lock:
|
|
|
|
|
|
usage_counts[endpoint][model] += 1
|
2025-09-05 12:11:31 +02:00
|
|
|
|
await publish_snapshot()
|
2025-08-29 13:13:25 +02:00
|
|
|
|
|
|
|
|
|
|
async def decrement_usage(endpoint: str, model: str) -> None:
|
|
|
|
|
|
async with usage_lock:
|
|
|
|
|
|
# Avoid negative counts
|
|
|
|
|
|
current = usage_counts[endpoint].get(model, 0)
|
|
|
|
|
|
if current > 0:
|
|
|
|
|
|
usage_counts[endpoint][model] = current - 1
|
|
|
|
|
|
# Optionally, clean up zero entries
|
|
|
|
|
|
if usage_counts[endpoint].get(model, 0) == 0:
|
|
|
|
|
|
usage_counts[endpoint].pop(model, None)
|
2025-09-05 12:11:31 +02:00
|
|
|
|
#if not usage_counts[endpoint]:
|
|
|
|
|
|
# usage_counts.pop(endpoint, None)
|
|
|
|
|
|
await publish_snapshot()
|
|
|
|
|
|
|
2025-12-15 10:35:56 +01:00
|
|
|
|
async def _make_chat_request(endpoint: str, model: str, messages: list, tools=None, stream: bool = False, think: bool = False, format=None, options=None, keep_alive: str = None) -> ollama.ChatResponse:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Helper function to make a chat request to a specific endpoint.
|
|
|
|
|
|
Handles endpoint selection, client creation, usage tracking, and request execution.
|
|
|
|
|
|
"""
|
|
|
|
|
|
is_openai_endpoint = "/v1" in endpoint
|
|
|
|
|
|
if is_openai_endpoint:
|
|
|
|
|
|
if ":latest" in model:
|
|
|
|
|
|
model = model.split(":latest")[0]
|
|
|
|
|
|
if messages:
|
|
|
|
|
|
messages = transform_images_to_data_urls(messages)
|
|
|
|
|
|
params = {
|
|
|
|
|
|
"messages": messages,
|
|
|
|
|
|
"model": model,
|
|
|
|
|
|
}
|
|
|
|
|
|
optional_params = {
|
|
|
|
|
|
"tools": tools,
|
|
|
|
|
|
"stream": stream,
|
|
|
|
|
|
"stream_options": {"include_usage": True} if stream else None,
|
|
|
|
|
|
"max_tokens": options.get("num_predict") if options and "num_predict" in options else None,
|
|
|
|
|
|
"frequency_penalty": options.get("frequency_penalty") if options and "frequency_penalty" in options else None,
|
|
|
|
|
|
"presence_penalty": options.get("presence_penalty") if options and "presence_penalty" in options else None,
|
|
|
|
|
|
"seed": options.get("seed") if options and "seed" in options else None,
|
|
|
|
|
|
"stop": options.get("stop") if options and "stop" in options else None,
|
|
|
|
|
|
"top_p": options.get("top_p") if options and "top_p" in options else None,
|
|
|
|
|
|
"temperature": options.get("temperature") if options and "temperature" in options else None,
|
|
|
|
|
|
"response_format": {"type": "json_schema", "json_schema": format} if format is not None else None
|
|
|
|
|
|
}
|
|
|
|
|
|
params.update({k: v for k, v in optional_params.items() if v is not None})
|
|
|
|
|
|
oclient = openai.AsyncOpenAI(base_url=endpoint, default_headers=default_headers, api_key=config.api_keys[endpoint])
|
|
|
|
|
|
else:
|
|
|
|
|
|
client = ollama.AsyncClient(host=endpoint)
|
|
|
|
|
|
|
|
|
|
|
|
await increment_usage(endpoint, model)
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
if is_openai_endpoint:
|
|
|
|
|
|
start_ts = time.perf_counter()
|
|
|
|
|
|
response = await oclient.chat.completions.create(**params)
|
|
|
|
|
|
if stream:
|
|
|
|
|
|
# For streaming, we need to collect all chunks
|
|
|
|
|
|
chunks = []
|
|
|
|
|
|
async for chunk in response:
|
|
|
|
|
|
chunks.append(chunk)
|
|
|
|
|
|
if chunk.usage is not None:
|
|
|
|
|
|
prompt_tok = chunk.usage.prompt_tokens or 0
|
|
|
|
|
|
comp_tok = chunk.usage.completion_tokens or 0
|
|
|
|
|
|
if prompt_tok != 0 or comp_tok != 0:
|
|
|
|
|
|
await token_queue.put((endpoint, model, prompt_tok, comp_tok))
|
|
|
|
|
|
# Convert to Ollama format
|
|
|
|
|
|
if chunks:
|
|
|
|
|
|
response = rechunk.openai_chat_completion2ollama(chunks[-1], stream, start_ts)
|
|
|
|
|
|
else:
|
|
|
|
|
|
prompt_tok = response.usage.prompt_tokens or 0
|
|
|
|
|
|
comp_tok = response.usage.completion_tokens or 0
|
|
|
|
|
|
if prompt_tok != 0 or comp_tok != 0:
|
|
|
|
|
|
await token_queue.put((endpoint, model, prompt_tok, comp_tok))
|
|
|
|
|
|
response = rechunk.openai_chat_completion2ollama(response, stream, start_ts)
|
|
|
|
|
|
else:
|
|
|
|
|
|
response = await client.chat(model=model, messages=messages, tools=tools, stream=stream, think=think, format=format, options=options, keep_alive=keep_alive)
|
|
|
|
|
|
if stream:
|
|
|
|
|
|
# For streaming, collect all chunks
|
|
|
|
|
|
chunks = []
|
|
|
|
|
|
async for chunk in response:
|
|
|
|
|
|
chunks.append(chunk)
|
|
|
|
|
|
prompt_tok = chunk.prompt_eval_count or 0
|
|
|
|
|
|
comp_tok = chunk.eval_count or 0
|
|
|
|
|
|
if prompt_tok != 0 or comp_tok != 0:
|
|
|
|
|
|
await token_queue.put((endpoint, model, prompt_tok, comp_tok))
|
|
|
|
|
|
if chunks:
|
|
|
|
|
|
response = chunks[-1]
|
|
|
|
|
|
else:
|
|
|
|
|
|
prompt_tok = response.prompt_eval_count or 0
|
|
|
|
|
|
comp_tok = response.eval_count or 0
|
|
|
|
|
|
if prompt_tok != 0 or comp_tok != 0:
|
|
|
|
|
|
await token_queue.put((endpoint, model, prompt_tok, comp_tok))
|
|
|
|
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
finally:
|
|
|
|
|
|
await decrement_usage(endpoint, model)
|
|
|
|
|
|
|
|
|
|
|
|
def get_last_user_content(messages):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Given a list of dicts (e.g., messages from an API),
|
|
|
|
|
|
return the 'content' of the last dict whose 'role' is 'user'.
|
|
|
|
|
|
If no such dict exists, return None.
|
|
|
|
|
|
"""
|
|
|
|
|
|
# Reverse iterate so we stop at the first match
|
|
|
|
|
|
for msg in reversed(messages):
|
|
|
|
|
|
if msg.get("role") == "user":
|
|
|
|
|
|
return msg.get("content")
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
async def _make_moe_requests(model: str, messages: list, tools=None, think: bool = False, format=None, options=None, keep_alive: str = None) -> ollama.ChatResponse:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Helper function to make MOE (Multiple Opinions Ensemble) requests.
|
|
|
|
|
|
Generates 3 responses, 3 critiques, and returns the final selected response.
|
|
|
|
|
|
"""
|
|
|
|
|
|
query = get_last_user_content(messages)
|
|
|
|
|
|
if not query:
|
|
|
|
|
|
raise ValueError("No user query found in messages")
|
|
|
|
|
|
|
|
|
|
|
|
if options is None:
|
|
|
|
|
|
options = {}
|
|
|
|
|
|
options["temperature"] = 1
|
|
|
|
|
|
|
|
|
|
|
|
moe_reqs = []
|
|
|
|
|
|
|
|
|
|
|
|
# Generate 3 responses
|
|
|
|
|
|
response1_endpoint = await choose_endpoint(model)
|
|
|
|
|
|
response1_task = asyncio.create_task(_make_chat_request(response1_endpoint, model, messages, tools, stream=False, think=think, format=format, options=options, keep_alive=keep_alive))
|
|
|
|
|
|
await asyncio.sleep(0.01) # Small delay to allow usage count to update
|
|
|
|
|
|
|
|
|
|
|
|
response2_endpoint = await choose_endpoint(model)
|
|
|
|
|
|
response2_task = asyncio.create_task(_make_chat_request(response2_endpoint, model, messages, tools, stream=False, think=think, format=format, options=options, keep_alive=keep_alive))
|
|
|
|
|
|
await asyncio.sleep(0.01) # Small delay to allow usage count to update
|
|
|
|
|
|
|
|
|
|
|
|
response3_endpoint = await choose_endpoint(model)
|
|
|
|
|
|
response3_task = asyncio.create_task(_make_chat_request(response3_endpoint, model, messages, tools, stream=False, think=think, format=format, options=options, keep_alive=keep_alive))
|
|
|
|
|
|
await asyncio.sleep(0.01) # Small delay to allow usage count to update
|
|
|
|
|
|
|
|
|
|
|
|
responses = await asyncio.gather(response1_task, response2_task, response3_task)
|
|
|
|
|
|
|
|
|
|
|
|
for n, r in enumerate(responses):
|
|
|
|
|
|
moe_req = enhance.moe(query, n, r.message.content)
|
|
|
|
|
|
moe_reqs.append(moe_req)
|
|
|
|
|
|
|
|
|
|
|
|
# Generate 3 critiques
|
|
|
|
|
|
critique1_endpoint = await choose_endpoint(model)
|
|
|
|
|
|
critique1_task = asyncio.create_task(_make_chat_request(critique1_endpoint, model, [{"role": "user", "content": moe_reqs[0]}], tools, stream=False, think=think, format=format, options=options, keep_alive=keep_alive))
|
|
|
|
|
|
await asyncio.sleep(0.01) # Small delay to allow usage count to update
|
|
|
|
|
|
|
|
|
|
|
|
critique2_endpoint = await choose_endpoint(model)
|
|
|
|
|
|
critique2_task = asyncio.create_task(_make_chat_request(critique2_endpoint, model, [{"role": "user", "content": moe_reqs[1]}], tools, stream=False, think=think, format=format, options=options, keep_alive=keep_alive))
|
|
|
|
|
|
await asyncio.sleep(0.01) # Small delay to allow usage count to update
|
|
|
|
|
|
|
|
|
|
|
|
critique3_endpoint = await choose_endpoint(model)
|
|
|
|
|
|
critique3_task = asyncio.create_task(_make_chat_request(critique3_endpoint, model, [{"role": "user", "content": moe_reqs[2]}], tools, stream=False, think=think, format=format, options=options, keep_alive=keep_alive))
|
|
|
|
|
|
await asyncio.sleep(0.01) # Small delay to allow usage count to update
|
|
|
|
|
|
|
|
|
|
|
|
critiques = await asyncio.gather(critique1_task, critique2_task, critique3_task)
|
|
|
|
|
|
|
|
|
|
|
|
# Select final response
|
|
|
|
|
|
m = enhance.moe_select_candidate(query, critiques)
|
|
|
|
|
|
|
|
|
|
|
|
# Generate final response
|
|
|
|
|
|
final_endpoint = await choose_endpoint(model)
|
|
|
|
|
|
return await _make_chat_request(final_endpoint, model, [{"role": "user", "content": m}], tools, stream=False, think=think, format=format, options=options, keep_alive=keep_alive)
|
|
|
|
|
|
|
2025-09-13 11:24:28 +02:00
|
|
|
|
def iso8601_ns():
|
2025-10-30 10:17:18 +01:00
|
|
|
|
ns = time.time_ns()
|
|
|
|
|
|
sec, ns_rem = divmod(ns, 1_000_000_000)
|
2025-11-18 11:16:21 +01:00
|
|
|
|
dt = datetime.fromtimestamp(sec, tz=timezone.utc)
|
2025-10-30 10:17:18 +01:00
|
|
|
|
return (
|
|
|
|
|
|
f"{dt.year:04d}-{dt.month:02d}-{dt.day:02d}T"
|
|
|
|
|
|
f"{dt.hour:02d}:{dt.minute:02d}:{dt.second:02d}."
|
|
|
|
|
|
f"{ns_rem:09d}Z"
|
2025-09-13 11:24:28 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-09-23 17:33:15 +02:00
|
|
|
|
def is_base64(image_string):
|
|
|
|
|
|
try:
|
|
|
|
|
|
if isinstance(image_string, str) and base64.b64encode(base64.b64decode(image_string)) == image_string.encode():
|
|
|
|
|
|
return True
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
2025-09-24 11:46:38 +02:00
|
|
|
|
def resize_image_if_needed(image_data):
|
|
|
|
|
|
try:
|
2025-09-24 18:10:17 +02:00
|
|
|
|
# Check if already data-url
|
|
|
|
|
|
if image_data.startswith("data:"):
|
|
|
|
|
|
try:
|
|
|
|
|
|
header, image_data = image_data.split(",", 1)
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
pass
|
2025-09-24 11:46:38 +02:00
|
|
|
|
# Decode the base64 image data
|
|
|
|
|
|
image_bytes = base64.b64decode(image_data)
|
|
|
|
|
|
image = Image.open(io.BytesIO(image_bytes))
|
2025-09-24 18:10:17 +02:00
|
|
|
|
if image.mode not in ("RGB", "L"):
|
|
|
|
|
|
image = image.convert("RGB")
|
2025-09-24 11:46:38 +02:00
|
|
|
|
|
|
|
|
|
|
# Get current size
|
|
|
|
|
|
width, height = image.size
|
|
|
|
|
|
|
|
|
|
|
|
# Calculate the new dimensions while maintaining aspect ratio
|
|
|
|
|
|
if width > 512 or height > 512:
|
|
|
|
|
|
aspect_ratio = width / height
|
|
|
|
|
|
if aspect_ratio > 1: # Width is larger
|
|
|
|
|
|
new_width = 512
|
|
|
|
|
|
new_height = int(512 / aspect_ratio)
|
|
|
|
|
|
else: # Height is larger
|
|
|
|
|
|
new_height = 512
|
|
|
|
|
|
new_width = int(512 * aspect_ratio)
|
|
|
|
|
|
|
2025-10-28 11:08:52 +01:00
|
|
|
|
image = image.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
2025-09-24 11:46:38 +02:00
|
|
|
|
|
|
|
|
|
|
# Encode the resized image back to base64
|
|
|
|
|
|
buffered = io.BytesIO()
|
|
|
|
|
|
image.save(buffered, format="PNG")
|
|
|
|
|
|
resized_image_data = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
|
|
|
|
|
return resized_image_data
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
print(f"Error processing image: {e}")
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
2025-09-23 17:33:15 +02:00
|
|
|
|
def transform_images_to_data_urls(message_list):
|
|
|
|
|
|
for message in message_list:
|
|
|
|
|
|
if "images" in message:
|
|
|
|
|
|
images = message.pop("images")
|
|
|
|
|
|
if not isinstance(images, list):
|
|
|
|
|
|
continue
|
|
|
|
|
|
new_content = []
|
|
|
|
|
|
for image in images: #TODO: quality downsize if images are too big to fit into model context window size
|
|
|
|
|
|
if not is_base64(image):
|
|
|
|
|
|
raise ValueError(f"Image string is not a valid base64 encoded string.")
|
2025-09-24 11:46:38 +02:00
|
|
|
|
resized_image = resize_image_if_needed(image)
|
|
|
|
|
|
if resized_image:
|
|
|
|
|
|
data_url = f"data:image/png;base64,{resized_image}"
|
|
|
|
|
|
#new_content.append({
|
|
|
|
|
|
# "type": "text",
|
|
|
|
|
|
# "text": ""
|
|
|
|
|
|
#})
|
|
|
|
|
|
new_content.append({
|
|
|
|
|
|
"type": "image_url",
|
|
|
|
|
|
"image_url": {
|
|
|
|
|
|
"url": data_url
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
2025-09-23 17:33:15 +02:00
|
|
|
|
message["content"] = new_content
|
|
|
|
|
|
|
|
|
|
|
|
return message_list
|
|
|
|
|
|
|
2025-09-13 11:24:28 +02:00
|
|
|
|
class rechunk:
|
2025-09-22 09:30:27 +02:00
|
|
|
|
def openai_chat_completion2ollama(chunk: dict, stream: bool, start_ts: float) -> ollama.ChatResponse:
|
2025-11-10 15:37:46 +01:00
|
|
|
|
now = time.perf_counter()
|
2025-09-23 12:51:37 +02:00
|
|
|
|
if chunk.choices == [] and chunk.usage is not None:
|
|
|
|
|
|
return ollama.ChatResponse(
|
|
|
|
|
|
model=chunk.model,
|
|
|
|
|
|
created_at=iso8601_ns(),
|
|
|
|
|
|
done=True,
|
|
|
|
|
|
done_reason='stop',
|
2025-11-10 15:37:46 +01:00
|
|
|
|
total_duration=int((now - start_ts) * 1_000_000_000),
|
2025-09-23 12:51:37 +02:00
|
|
|
|
load_duration=100000,
|
|
|
|
|
|
prompt_eval_count=int(chunk.usage.prompt_tokens),
|
2025-11-10 15:37:46 +01:00
|
|
|
|
prompt_eval_duration=int((now - start_ts) * 1_000_000_000 * (chunk.usage.prompt_tokens / chunk.usage.completion_tokens / 100)),
|
2025-09-23 12:51:37 +02:00
|
|
|
|
eval_count=int(chunk.usage.completion_tokens),
|
2025-11-10 15:37:46 +01:00
|
|
|
|
eval_duration=int((now - start_ts) * 1_000_000_000),
|
2025-09-23 12:51:37 +02:00
|
|
|
|
message={"role": "assistant"}
|
|
|
|
|
|
)
|
2025-09-22 09:30:27 +02:00
|
|
|
|
with_thinking = chunk.choices[0] if chunk.choices[0] else None
|
2025-09-13 12:28:42 +02:00
|
|
|
|
if stream == True:
|
2025-09-22 19:01:14 +02:00
|
|
|
|
thinking = getattr(with_thinking.delta, "reasoning", None) if with_thinking else None
|
2025-09-21 16:33:43 +02:00
|
|
|
|
role = chunk.choices[0].delta.role or "assistant"
|
2025-09-23 12:51:37 +02:00
|
|
|
|
content = chunk.choices[0].delta.content or ''
|
2025-09-13 12:28:42 +02:00
|
|
|
|
else:
|
2025-09-22 19:01:14 +02:00
|
|
|
|
thinking = getattr(with_thinking, "reasoning", None) if with_thinking else None
|
2025-09-21 16:33:43 +02:00
|
|
|
|
role = chunk.choices[0].message.role or "assistant"
|
2025-09-23 12:51:37 +02:00
|
|
|
|
content = chunk.choices[0].message.content or ''
|
2025-09-21 16:33:43 +02:00
|
|
|
|
assistant_msg = ollama.Message(
|
|
|
|
|
|
role=role,
|
|
|
|
|
|
content=content,
|
2025-09-22 09:30:27 +02:00
|
|
|
|
thinking=thinking,
|
2025-09-21 16:33:43 +02:00
|
|
|
|
images=None,
|
|
|
|
|
|
tool_name=None,
|
|
|
|
|
|
tool_calls=None)
|
|
|
|
|
|
rechunk = ollama.ChatResponse(
|
|
|
|
|
|
model=chunk.model,
|
|
|
|
|
|
created_at=iso8601_ns(),
|
2025-09-23 12:51:37 +02:00
|
|
|
|
done=True if chunk.usage is not None else False,
|
|
|
|
|
|
done_reason=chunk.choices[0].finish_reason, #if chunk.choices[0].finish_reason is not None else None,
|
2025-11-10 15:37:46 +01:00
|
|
|
|
total_duration=int((now - start_ts) * 1_000_000_000) if chunk.usage is not None else 0,
|
2025-09-21 16:33:43 +02:00
|
|
|
|
load_duration=100000,
|
2025-09-22 19:01:14 +02:00
|
|
|
|
prompt_eval_count=int(chunk.usage.prompt_tokens) if chunk.usage is not None else 0,
|
2025-11-10 15:37:46 +01:00
|
|
|
|
prompt_eval_duration=int((now - start_ts) * 1_000_000_000 * (chunk.usage.prompt_tokens / chunk.usage.completion_tokens / 100)) if chunk.usage is not None and chunk.usage.completion_tokens != 0 else 0,
|
2025-09-22 19:01:14 +02:00
|
|
|
|
eval_count=int(chunk.usage.completion_tokens) if chunk.usage is not None else 0,
|
2025-11-10 15:37:46 +01:00
|
|
|
|
eval_duration=int((now - start_ts) * 1_000_000_000) if chunk.usage is not None else 0,
|
2025-09-21 16:33:43 +02:00
|
|
|
|
message=assistant_msg)
|
2025-09-13 12:38:13 +02:00
|
|
|
|
return rechunk
|
2025-09-13 16:57:09 +02:00
|
|
|
|
|
2025-09-22 09:30:27 +02:00
|
|
|
|
def openai_completion2ollama(chunk: dict, stream: bool, start_ts: float) -> ollama.GenerateResponse:
|
2025-11-10 15:37:46 +01:00
|
|
|
|
now = time.perf_counter()
|
2025-09-17 11:40:48 +02:00
|
|
|
|
with_thinking = chunk.choices[0] if chunk.choices[0] else None
|
|
|
|
|
|
thinking = getattr(with_thinking, "reasoning", None) if with_thinking else None
|
2025-09-21 16:33:43 +02:00
|
|
|
|
rechunk = ollama.GenerateResponse(
|
|
|
|
|
|
model=chunk.model,
|
|
|
|
|
|
created_at=iso8601_ns(),
|
2025-09-23 12:51:37 +02:00
|
|
|
|
done=True if chunk.usage is not None else False,
|
2025-09-21 16:33:43 +02:00
|
|
|
|
done_reason=chunk.choices[0].finish_reason,
|
2025-11-10 15:37:46 +01:00
|
|
|
|
total_duration=int((now - start_ts) * 1_000_000_000) if chunk.usage is not None else 0,
|
2025-09-22 09:30:27 +02:00
|
|
|
|
load_duration=10000,
|
2025-09-23 12:51:37 +02:00
|
|
|
|
prompt_eval_count=int(chunk.usage.prompt_tokens) if chunk.usage is not None else 0,
|
2025-11-10 15:37:46 +01:00
|
|
|
|
prompt_eval_duration=int((now - start_ts) * 1_000_000_000 * (chunk.usage.prompt_tokens / chunk.usage.completion_tokens / 100)) if chunk.usage is not None and chunk.usage.completion_tokens != 0 else 0,
|
2025-09-23 12:51:37 +02:00
|
|
|
|
eval_count=int(chunk.usage.completion_tokens) if chunk.usage is not None else 0,
|
2025-11-10 15:37:46 +01:00
|
|
|
|
eval_duration=int((now - start_ts) * 1_000_000_000) if chunk.usage is not None else 0,
|
2025-09-23 12:51:37 +02:00
|
|
|
|
response=chunk.choices[0].text or '',
|
2025-09-22 09:30:27 +02:00
|
|
|
|
thinking=thinking)
|
2025-09-13 16:57:09 +02:00
|
|
|
|
return rechunk
|
2025-09-15 11:47:55 +02:00
|
|
|
|
|
2025-09-22 09:30:27 +02:00
|
|
|
|
def openai_embeddings2ollama(chunk: dict) -> ollama.EmbeddingsResponse:
|
2025-09-21 16:20:36 +02:00
|
|
|
|
rechunk = ollama.EmbeddingsResponse(embedding=chunk.data[0].embedding)
|
2025-09-15 11:47:55 +02:00
|
|
|
|
return rechunk
|
2025-09-13 11:24:28 +02:00
|
|
|
|
|
2025-09-22 09:30:27 +02:00
|
|
|
|
def openai_embed2ollama(chunk: dict, model: str) -> ollama.EmbedResponse:
|
2025-09-21 16:33:43 +02:00
|
|
|
|
rechunk = ollama.EmbedResponse(
|
|
|
|
|
|
model=model,
|
|
|
|
|
|
created_at=iso8601_ns(),
|
|
|
|
|
|
done=None,
|
|
|
|
|
|
done_reason=None,
|
|
|
|
|
|
total_duration=None,
|
|
|
|
|
|
load_duration=None,
|
|
|
|
|
|
prompt_eval_count=None,
|
|
|
|
|
|
prompt_eval_duration=None,
|
|
|
|
|
|
eval_count=None,
|
|
|
|
|
|
eval_duration=None,
|
|
|
|
|
|
embeddings=[chunk.data[0].embedding])
|
2025-09-15 11:47:55 +02:00
|
|
|
|
return rechunk
|
2025-09-22 14:04:19 +02:00
|
|
|
|
|
2025-09-05 12:11:31 +02:00
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
# SSE Helpser
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
async def publish_snapshot():
|
2025-09-19 16:38:48 +02:00
|
|
|
|
async with usage_lock:
|
2025-11-10 15:37:46 +01:00
|
|
|
|
snapshot = orjson.dumps({"usage_counts": usage_counts,
|
2025-11-04 17:55:19 +01:00
|
|
|
|
"token_usage_counts": token_usage_counts,
|
2025-11-10 15:37:46 +01:00
|
|
|
|
}, option=orjson.OPT_SORT_KEYS).decode("utf-8")
|
2025-09-05 12:11:31 +02:00
|
|
|
|
async with _subscribers_lock:
|
|
|
|
|
|
for q in _subscribers:
|
|
|
|
|
|
# If the queue is full, drop the message to avoid back‑pressure.
|
|
|
|
|
|
if q.full():
|
2025-09-19 16:38:48 +02:00
|
|
|
|
try:
|
|
|
|
|
|
await q.get()
|
|
|
|
|
|
except asyncio.QueueEmpty:
|
|
|
|
|
|
pass
|
2025-09-05 12:11:31 +02:00
|
|
|
|
await q.put(snapshot)
|
|
|
|
|
|
|
2025-09-12 09:44:56 +02:00
|
|
|
|
async def close_all_sse_queues():
|
|
|
|
|
|
for q in list(_subscribers):
|
|
|
|
|
|
# sentinel value that the generator will recognise
|
|
|
|
|
|
await q.put(None)
|
|
|
|
|
|
|
2025-09-05 12:11:31 +02:00
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
# Subscriber helpers
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
async def subscribe() -> asyncio.Queue:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Returns a new Queue that will receive every snapshot.
|
|
|
|
|
|
"""
|
|
|
|
|
|
q: asyncio.Queue = asyncio.Queue(maxsize=10)
|
|
|
|
|
|
async with _subscribers_lock:
|
|
|
|
|
|
_subscribers.add(q)
|
|
|
|
|
|
return q
|
|
|
|
|
|
|
|
|
|
|
|
async def unsubscribe(q: asyncio.Queue):
|
|
|
|
|
|
async with _subscribers_lock:
|
|
|
|
|
|
_subscribers.discard(q)
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
# Convenience wrapper – returns the current snapshot (for the proxy)
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
async def get_usage_counts() -> Dict:
|
|
|
|
|
|
return dict(usage_counts) # shallow copy
|
2025-08-29 13:13:25 +02:00
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
# 5. Endpoint selection logic (respecting the configurable limit)
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
async def choose_endpoint(model: str) -> str:
|
|
|
|
|
|
"""
|
2025-08-29 13:13:25 +02:00
|
|
|
|
Determine which endpoint to use for the given model while respecting
|
|
|
|
|
|
the `max_concurrent_connections` per endpoint‑model pair **and**
|
|
|
|
|
|
ensuring that the chosen endpoint actually *advertises* the model.
|
|
|
|
|
|
|
|
|
|
|
|
The selection algorithm:
|
|
|
|
|
|
|
|
|
|
|
|
1️⃣ Query every endpoint for its advertised models (`/api/tags`).
|
|
|
|
|
|
2️⃣ Build a list of endpoints that contain the requested model.
|
|
|
|
|
|
3️⃣ For those endpoints, find those that have the model loaded
|
2025-09-18 18:49:11 +02:00
|
|
|
|
(`/api/ps`) *and* still have a free slot.
|
2025-08-29 13:13:25 +02:00
|
|
|
|
4️⃣ If none are both loaded and free, fall back to any endpoint
|
2025-09-18 18:49:11 +02:00
|
|
|
|
from the filtered list that simply has a free slot and randomly
|
|
|
|
|
|
select one.
|
2025-08-29 13:13:25 +02:00
|
|
|
|
5️⃣ If all are saturated, pick any endpoint from the filtered list
|
2025-09-18 18:49:11 +02:00
|
|
|
|
(the request will queue on that endpoint).
|
2025-08-29 13:13:25 +02:00
|
|
|
|
6️⃣ If no endpoint advertises the model at all, raise an error.
|
2025-08-26 18:19:43 +02:00
|
|
|
|
"""
|
2025-08-29 13:13:25 +02:00
|
|
|
|
# 1️⃣ Gather advertised‑model sets for all endpoints concurrently
|
2025-09-13 16:57:09 +02:00
|
|
|
|
tag_tasks = [fetch.available_models(ep) for ep in config.endpoints if "/v1" not in ep]
|
|
|
|
|
|
tag_tasks += [fetch.available_models(ep, config.api_keys[ep]) for ep in config.endpoints if "/v1" in ep]
|
2025-08-29 13:13:25 +02:00
|
|
|
|
advertised_sets = await asyncio.gather(*tag_tasks)
|
|
|
|
|
|
|
|
|
|
|
|
# 2️⃣ Filter endpoints that advertise the requested model
|
|
|
|
|
|
candidate_endpoints = [
|
|
|
|
|
|
ep for ep, models in zip(config.endpoints, advertised_sets)
|
|
|
|
|
|
if model in models
|
|
|
|
|
|
]
|
2025-09-01 11:07:07 +02:00
|
|
|
|
|
2025-12-14 17:58:45 +01:00
|
|
|
|
# 6️⃣
|
2025-08-29 13:13:25 +02:00
|
|
|
|
if not candidate_endpoints:
|
2025-09-15 17:39:15 +02:00
|
|
|
|
if ":latest" in model: #ollama naming convention not applicable to openai
|
2025-10-30 09:06:21 +01:00
|
|
|
|
model_without_latest = model.split(":latest")[0]
|
|
|
|
|
|
candidate_endpoints = [
|
|
|
|
|
|
ep for ep, models in zip(config.endpoints, advertised_sets)
|
|
|
|
|
|
if model_without_latest in models and is_ext_openai_endpoint(ep)
|
|
|
|
|
|
]
|
|
|
|
|
|
if not candidate_endpoints:
|
2025-12-14 17:58:45 +01:00
|
|
|
|
# Only add :latest suffix if model doesn't already have a version suffix
|
|
|
|
|
|
if ":" not in model:
|
|
|
|
|
|
model = model + ":latest"
|
2025-09-15 17:39:15 +02:00
|
|
|
|
candidate_endpoints = [
|
|
|
|
|
|
ep for ep, models in zip(config.endpoints, advertised_sets)
|
|
|
|
|
|
if model in models
|
|
|
|
|
|
]
|
|
|
|
|
|
if not candidate_endpoints:
|
|
|
|
|
|
raise RuntimeError(
|
|
|
|
|
|
f"None of the configured endpoints ({', '.join(config.endpoints)}) "
|
|
|
|
|
|
f"advertise the model '{model}'."
|
|
|
|
|
|
)
|
2025-08-29 13:13:25 +02:00
|
|
|
|
# 3️⃣ Among the candidates, find those that have the model *loaded*
|
|
|
|
|
|
# (concurrently, but only for the filtered list)
|
2025-09-13 16:57:09 +02:00
|
|
|
|
load_tasks = [fetch.loaded_models(ep) for ep in candidate_endpoints]
|
2025-08-29 13:13:25 +02:00
|
|
|
|
loaded_sets = await asyncio.gather(*load_tasks)
|
2025-09-10 18:42:24 +02:00
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
async with usage_lock:
|
2025-09-01 09:30:23 +02:00
|
|
|
|
# Helper: get current usage count for (endpoint, model)
|
|
|
|
|
|
def current_usage(ep: str) -> int:
|
|
|
|
|
|
return usage_counts.get(ep, {}).get(model, 0)
|
|
|
|
|
|
|
2025-08-29 13:13:25 +02:00
|
|
|
|
# 3️⃣ Endpoints that have the model loaded *and* a free slot
|
2025-08-26 18:19:43 +02:00
|
|
|
|
loaded_and_free = [
|
2025-08-29 13:13:25 +02:00
|
|
|
|
ep for ep, models in zip(candidate_endpoints, loaded_sets)
|
2025-09-10 18:42:24 +02:00
|
|
|
|
if model in models and usage_counts.get(ep, {}).get(model, 0) < config.max_concurrent_connections
|
2025-08-26 18:19:43 +02:00
|
|
|
|
]
|
2025-09-01 09:30:23 +02:00
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
if loaded_and_free:
|
2025-09-01 09:30:23 +02:00
|
|
|
|
ep = min(loaded_and_free, key=current_usage)
|
|
|
|
|
|
return ep
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
2025-08-29 13:13:25 +02:00
|
|
|
|
# 4️⃣ Endpoints among the candidates that simply have a free slot
|
2025-08-26 18:19:43 +02:00
|
|
|
|
endpoints_with_free_slot = [
|
2025-08-29 13:13:25 +02:00
|
|
|
|
ep for ep in candidate_endpoints
|
2025-09-10 18:42:24 +02:00
|
|
|
|
if usage_counts.get(ep, {}).get(model, 0) < config.max_concurrent_connections
|
2025-08-26 18:19:43 +02:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
if endpoints_with_free_slot:
|
2026-01-05 17:16:31 +01:00
|
|
|
|
#return random.choice(endpoints_with_free_slot)
|
|
|
|
|
|
endpoints_with_free_slot.sort(key=lambda ep: sum(usage_counts.get(ep, {}).values()))
|
|
|
|
|
|
return endpoints_with_free_slot[0]
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
2025-09-01 11:07:07 +02:00
|
|
|
|
# 5️⃣ All candidate endpoints are saturated – pick one with lowest usages count (will queue)
|
2025-09-01 09:30:23 +02:00
|
|
|
|
ep = min(candidate_endpoints, key=current_usage)
|
|
|
|
|
|
return ep
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
# 6. API route – Generate
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.post("/api/generate")
|
|
|
|
|
|
async def proxy(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Proxy a generate request to Ollama and stream the response back to the client.
|
|
|
|
|
|
"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
body_bytes = await request.body()
|
2025-11-10 15:37:46 +01:00
|
|
|
|
payload = orjson.loads(body_bytes.decode("utf-8"))
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
|
|
|
|
|
model = payload.get("model")
|
|
|
|
|
|
prompt = payload.get("prompt")
|
|
|
|
|
|
suffix = payload.get("suffix")
|
|
|
|
|
|
system = payload.get("system")
|
|
|
|
|
|
template = payload.get("template")
|
|
|
|
|
|
context = payload.get("context")
|
|
|
|
|
|
stream = payload.get("stream")
|
|
|
|
|
|
think = payload.get("think")
|
|
|
|
|
|
raw = payload.get("raw")
|
2025-09-11 18:53:23 +02:00
|
|
|
|
_format = payload.get("format")
|
2025-08-26 18:19:43 +02:00
|
|
|
|
images = payload.get("images")
|
|
|
|
|
|
options = payload.get("options")
|
|
|
|
|
|
keep_alive = payload.get("keep_alive")
|
2025-09-22 14:04:19 +02:00
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
if not model:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'model'"
|
|
|
|
|
|
)
|
|
|
|
|
|
if not prompt:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'prompt'"
|
|
|
|
|
|
)
|
2025-11-10 15:37:46 +01:00
|
|
|
|
except orjson.JSONDecodeError as e:
|
2025-10-03 10:04:50 +02:00
|
|
|
|
error_msg = f"Invalid JSON format in request body: {str(e)}. Please ensure the request is properly formatted."
|
|
|
|
|
|
raise HTTPException(status_code=400, detail=error_msg) from e
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
2025-09-13 16:57:09 +02:00
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
endpoint = await choose_endpoint(model)
|
2025-09-13 16:57:09 +02:00
|
|
|
|
is_openai_endpoint = "/v1" in endpoint
|
|
|
|
|
|
if is_openai_endpoint:
|
2025-09-15 17:39:15 +02:00
|
|
|
|
if ":latest" in model:
|
2025-09-15 19:12:00 +02:00
|
|
|
|
model = model.split(":latest")
|
2025-09-15 17:39:15 +02:00
|
|
|
|
model = model[0]
|
2025-09-13 16:57:09 +02:00
|
|
|
|
params = {
|
|
|
|
|
|
"prompt": prompt,
|
|
|
|
|
|
"model": model,
|
|
|
|
|
|
}
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
2025-09-13 16:57:09 +02:00
|
|
|
|
optional_params = {
|
|
|
|
|
|
"stream": stream,
|
2025-09-22 14:04:19 +02:00
|
|
|
|
"max_tokens": options.get("num_predict") if options and "num_predict" in options else None,
|
|
|
|
|
|
"frequency_penalty": options.get("frequency_penalty") if options and "frequency_penalty" in options else None,
|
|
|
|
|
|
"presence_penalty": options.get("presence_penalty") if options and "presence_penalty" in options else None,
|
|
|
|
|
|
"seed": options.get("seed") if options and "seed" in options else None,
|
|
|
|
|
|
"stop": options.get("stop") if options and "stop" in options else None,
|
|
|
|
|
|
"top_p": options.get("top_p") if options and "top_p" in options else None,
|
|
|
|
|
|
"temperature": options.get("temperature") if options and "temperature" in options else None,
|
2025-10-28 11:08:52 +01:00
|
|
|
|
"suffix": suffix,
|
2025-09-22 14:04:19 +02:00
|
|
|
|
}
|
2025-09-13 16:57:09 +02:00
|
|
|
|
params.update({k: v for k, v in optional_params.items() if v is not None})
|
2025-09-21 16:20:36 +02:00
|
|
|
|
oclient = openai.AsyncOpenAI(base_url=endpoint, default_headers=default_headers, api_key=config.api_keys[endpoint])
|
2025-09-13 16:57:09 +02:00
|
|
|
|
else:
|
|
|
|
|
|
client = ollama.AsyncClient(host=endpoint)
|
2025-09-15 17:39:15 +02:00
|
|
|
|
await increment_usage(endpoint, model)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
|
|
|
|
|
# 4. Async generator that streams data and decrements the counter
|
|
|
|
|
|
async def stream_generate_response():
|
|
|
|
|
|
try:
|
2025-09-13 16:57:09 +02:00
|
|
|
|
if is_openai_endpoint:
|
|
|
|
|
|
start_ts = time.perf_counter()
|
|
|
|
|
|
async_gen = await oclient.completions.create(**params)
|
|
|
|
|
|
else:
|
|
|
|
|
|
async_gen = await client.generate(model=model, prompt=prompt, suffix=suffix, system=system, template=template, context=context, stream=stream, think=think, raw=raw, format=_format, images=images, options=options, keep_alive=keep_alive)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
if stream == True:
|
|
|
|
|
|
async for chunk in async_gen:
|
2025-09-13 16:57:09 +02:00
|
|
|
|
if is_openai_endpoint:
|
|
|
|
|
|
chunk = rechunk.openai_completion2ollama(chunk, stream, start_ts)
|
2025-11-04 17:55:19 +01:00
|
|
|
|
prompt_tok = chunk.prompt_eval_count or 0
|
|
|
|
|
|
comp_tok = chunk.eval_count or 0
|
2025-11-18 19:02:36 +01:00
|
|
|
|
if prompt_tok != 0 or comp_tok != 0:
|
|
|
|
|
|
await token_queue.put((endpoint, model, prompt_tok, comp_tok))
|
2025-08-26 18:19:43 +02:00
|
|
|
|
if hasattr(chunk, "model_dump_json"):
|
|
|
|
|
|
json_line = chunk.model_dump_json()
|
|
|
|
|
|
else:
|
2025-11-10 15:37:46 +01:00
|
|
|
|
json_line = orjson.dumps(chunk)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
yield json_line.encode("utf-8") + b"\n"
|
|
|
|
|
|
else:
|
2025-09-13 16:57:09 +02:00
|
|
|
|
if is_openai_endpoint:
|
|
|
|
|
|
response = rechunk.openai_completion2ollama(async_gen, stream, start_ts)
|
2025-09-22 09:30:27 +02:00
|
|
|
|
response = response.model_dump_json()
|
2025-09-13 16:57:09 +02:00
|
|
|
|
else:
|
|
|
|
|
|
response = async_gen.model_dump_json()
|
2025-11-04 17:55:19 +01:00
|
|
|
|
prompt_tok = async_gen.prompt_eval_count or 0
|
|
|
|
|
|
comp_tok = async_gen.eval_count or 0
|
2025-11-18 19:02:36 +01:00
|
|
|
|
if prompt_tok != 0 or comp_tok != 0:
|
|
|
|
|
|
await token_queue.put((endpoint, model, prompt_tok, comp_tok))
|
2025-08-26 18:19:43 +02:00
|
|
|
|
json_line = (
|
2025-09-13 16:57:09 +02:00
|
|
|
|
response
|
2025-08-26 18:19:43 +02:00
|
|
|
|
if hasattr(async_gen, "model_dump_json")
|
2025-11-10 15:37:46 +01:00
|
|
|
|
else orjson.dumps(async_gen)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
)
|
|
|
|
|
|
yield json_line.encode("utf-8") + b"\n"
|
|
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
# Ensure counter is decremented even if an exception occurs
|
|
|
|
|
|
await decrement_usage(endpoint, model)
|
|
|
|
|
|
|
|
|
|
|
|
# 5. Return a StreamingResponse backed by the generator
|
|
|
|
|
|
return StreamingResponse(
|
|
|
|
|
|
stream_generate_response(),
|
|
|
|
|
|
media_type="application/json",
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
# 7. API route – Chat
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.post("/api/chat")
|
|
|
|
|
|
async def chat_proxy(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Proxy a chat request to Ollama and stream the endpoint reply.
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 1. Parse and validate request
|
|
|
|
|
|
try:
|
|
|
|
|
|
body_bytes = await request.body()
|
2025-11-10 15:37:46 +01:00
|
|
|
|
payload = orjson.loads(body_bytes.decode("utf-8"))
|
2025-09-22 14:04:19 +02:00
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
model = payload.get("model")
|
|
|
|
|
|
messages = payload.get("messages")
|
|
|
|
|
|
tools = payload.get("tools")
|
|
|
|
|
|
stream = payload.get("stream")
|
|
|
|
|
|
think = payload.get("think")
|
2025-09-21 16:20:36 +02:00
|
|
|
|
_format = payload.get("format")
|
2025-08-26 18:19:43 +02:00
|
|
|
|
keep_alive = payload.get("keep_alive")
|
2025-09-21 16:20:36 +02:00
|
|
|
|
options = payload.get("options")
|
2025-09-23 17:33:15 +02:00
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
if not model:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'model'"
|
|
|
|
|
|
)
|
|
|
|
|
|
if not isinstance(messages, list):
|
|
|
|
|
|
raise HTTPException(
|
2025-09-13 11:24:28 +02:00
|
|
|
|
status_code=400, detail="Missing or invalid 'messages' field (must be a list)"
|
2025-08-26 18:19:43 +02:00
|
|
|
|
)
|
2025-09-21 16:20:36 +02:00
|
|
|
|
if options is not None and not isinstance(options, dict):
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="`options` must be a JSON object"
|
|
|
|
|
|
)
|
2025-11-10 15:37:46 +01:00
|
|
|
|
except orjson.JSONDecodeError as e:
|
2025-08-26 18:19:43 +02:00
|
|
|
|
raise HTTPException(status_code=400, detail=f"Invalid JSON: {e}") from e
|
|
|
|
|
|
|
|
|
|
|
|
# 2. Endpoint logic
|
2025-12-13 11:58:49 +01:00
|
|
|
|
if model.startswith("moe-"):
|
|
|
|
|
|
model = model.split("moe-")[1]
|
|
|
|
|
|
opt = True
|
|
|
|
|
|
else:
|
|
|
|
|
|
opt = False
|
2025-08-26 18:19:43 +02:00
|
|
|
|
endpoint = await choose_endpoint(model)
|
2025-09-13 16:57:09 +02:00
|
|
|
|
is_openai_endpoint = "/v1" in endpoint
|
|
|
|
|
|
if is_openai_endpoint:
|
2025-09-15 17:39:15 +02:00
|
|
|
|
if ":latest" in model:
|
2025-09-15 19:12:00 +02:00
|
|
|
|
model = model.split(":latest")
|
2025-09-15 17:39:15 +02:00
|
|
|
|
model = model[0]
|
2025-09-23 17:33:15 +02:00
|
|
|
|
if messages:
|
|
|
|
|
|
messages = transform_images_to_data_urls(messages)
|
2025-09-13 11:24:28 +02:00
|
|
|
|
params = {
|
|
|
|
|
|
"messages": messages,
|
|
|
|
|
|
"model": model,
|
2025-09-22 14:04:19 +02:00
|
|
|
|
}
|
2025-09-13 11:24:28 +02:00
|
|
|
|
optional_params = {
|
|
|
|
|
|
"tools": tools,
|
|
|
|
|
|
"stream": stream,
|
2025-11-04 17:55:19 +01:00
|
|
|
|
"stream_options": {"include_usage": True} if stream else None,
|
2025-09-22 14:04:19 +02:00
|
|
|
|
"max_tokens": options.get("num_predict") if options and "num_predict" in options else None,
|
|
|
|
|
|
"frequency_penalty": options.get("frequency_penalty") if options and "frequency_penalty" in options else None,
|
|
|
|
|
|
"presence_penalty": options.get("presence_penalty") if options and "presence_penalty" in options else None,
|
|
|
|
|
|
"seed": options.get("seed") if options and "seed" in options else None,
|
|
|
|
|
|
"stop": options.get("stop") if options and "stop" in options else None,
|
|
|
|
|
|
"top_p": options.get("top_p") if options and "top_p" in options else None,
|
|
|
|
|
|
"temperature": options.get("temperature") if options and "temperature" in options else None,
|
|
|
|
|
|
"response_format": {"type": "json_schema", "json_schema": _format} if _format is not None else None
|
|
|
|
|
|
}
|
2025-09-13 11:24:28 +02:00
|
|
|
|
params.update({k: v for k, v in optional_params.items() if v is not None})
|
2025-09-21 16:20:36 +02:00
|
|
|
|
oclient = openai.AsyncOpenAI(base_url=endpoint, default_headers=default_headers, api_key=config.api_keys[endpoint])
|
2025-09-13 11:24:28 +02:00
|
|
|
|
else:
|
|
|
|
|
|
client = ollama.AsyncClient(host=endpoint)
|
2025-09-15 17:39:15 +02:00
|
|
|
|
await increment_usage(endpoint, model)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# 3. Async generator that streams chat data and decrements the counter
|
|
|
|
|
|
async def stream_chat_response():
|
|
|
|
|
|
try:
|
|
|
|
|
|
# The chat method returns a generator of dicts (or GenerateResponse)
|
2025-09-13 12:10:40 +02:00
|
|
|
|
if is_openai_endpoint:
|
|
|
|
|
|
start_ts = time.perf_counter()
|
2025-09-13 11:24:28 +02:00
|
|
|
|
async_gen = await oclient.chat.completions.create(**params)
|
|
|
|
|
|
else:
|
2025-12-13 11:58:49 +01:00
|
|
|
|
if opt == True:
|
2025-12-15 10:35:56 +01:00
|
|
|
|
# Use the dedicated MOE helper function
|
|
|
|
|
|
async_gen = await _make_moe_requests(model, messages, tools, think, _format, options, keep_alive)
|
2025-12-13 11:58:49 +01:00
|
|
|
|
else:
|
|
|
|
|
|
async_gen = await client.chat(model=model, messages=messages, tools=tools, stream=stream, think=think, format=_format, options=options, keep_alive=keep_alive)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
if stream == True:
|
|
|
|
|
|
async for chunk in async_gen:
|
2025-09-13 12:10:40 +02:00
|
|
|
|
if is_openai_endpoint:
|
2025-09-13 12:28:42 +02:00
|
|
|
|
chunk = rechunk.openai_chat_completion2ollama(chunk, stream, start_ts)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# `chunk` can be a dict or a pydantic model – dump to JSON safely
|
2025-11-04 17:55:19 +01:00
|
|
|
|
prompt_tok = chunk.prompt_eval_count or 0
|
|
|
|
|
|
comp_tok = chunk.eval_count or 0
|
2025-11-18 19:02:36 +01:00
|
|
|
|
if prompt_tok != 0 or comp_tok != 0:
|
|
|
|
|
|
await token_queue.put((endpoint, model, prompt_tok, comp_tok))
|
2025-08-26 18:19:43 +02:00
|
|
|
|
if hasattr(chunk, "model_dump_json"):
|
|
|
|
|
|
json_line = chunk.model_dump_json()
|
|
|
|
|
|
else:
|
2025-11-10 15:37:46 +01:00
|
|
|
|
json_line = orjson.dumps(chunk)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
yield json_line.encode("utf-8") + b"\n"
|
|
|
|
|
|
else:
|
2025-09-13 12:28:42 +02:00
|
|
|
|
if is_openai_endpoint:
|
|
|
|
|
|
response = rechunk.openai_chat_completion2ollama(async_gen, stream, start_ts)
|
2025-09-22 09:30:27 +02:00
|
|
|
|
response = response.model_dump_json()
|
2025-09-13 12:28:42 +02:00
|
|
|
|
else:
|
2025-09-13 12:38:13 +02:00
|
|
|
|
response = async_gen.model_dump_json()
|
2025-11-04 17:55:19 +01:00
|
|
|
|
prompt_tok = async_gen.prompt_eval_count or 0
|
|
|
|
|
|
comp_tok = async_gen.eval_count or 0
|
2025-11-18 19:02:36 +01:00
|
|
|
|
if prompt_tok != 0 or comp_tok != 0:
|
|
|
|
|
|
await token_queue.put((endpoint, model, prompt_tok, comp_tok))
|
2025-08-26 18:19:43 +02:00
|
|
|
|
json_line = (
|
2025-09-13 12:28:42 +02:00
|
|
|
|
response
|
2025-09-22 19:01:14 +02:00
|
|
|
|
if hasattr(async_gen, "model_dump_json")
|
2025-11-10 15:37:46 +01:00
|
|
|
|
else orjson.dumps(async_gen)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
)
|
|
|
|
|
|
yield json_line.encode("utf-8") + b"\n"
|
|
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
# Ensure counter is decremented even if an exception occurs
|
|
|
|
|
|
await decrement_usage(endpoint, model)
|
|
|
|
|
|
|
|
|
|
|
|
# 4. Return a StreamingResponse backed by the generator
|
2025-09-22 19:01:14 +02:00
|
|
|
|
media_type = "application/x-ndjson" if stream else "application/json"
|
2025-08-26 18:19:43 +02:00
|
|
|
|
return StreamingResponse(
|
|
|
|
|
|
stream_chat_response(),
|
2025-09-22 19:01:14 +02:00
|
|
|
|
media_type=media_type,
|
2025-08-26 18:19:43 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-08-28 09:40:33 +02:00
|
|
|
|
# 8. API route – Embedding - deprecated
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.post("/api/embeddings")
|
|
|
|
|
|
async def embedding_proxy(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Proxy an embedding request to Ollama and reply with embeddings.
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 1. Parse and validate request
|
|
|
|
|
|
try:
|
|
|
|
|
|
body_bytes = await request.body()
|
2025-11-10 15:37:46 +01:00
|
|
|
|
payload = orjson.loads(body_bytes.decode("utf-8"))
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
|
|
|
|
|
model = payload.get("model")
|
|
|
|
|
|
prompt = payload.get("prompt")
|
|
|
|
|
|
options = payload.get("options")
|
|
|
|
|
|
keep_alive = payload.get("keep_alive")
|
|
|
|
|
|
|
|
|
|
|
|
if not model:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'model'"
|
|
|
|
|
|
)
|
|
|
|
|
|
if not prompt:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'prompt'"
|
|
|
|
|
|
)
|
2025-11-10 15:37:46 +01:00
|
|
|
|
except orjson.JSONDecodeError as e:
|
2025-08-26 18:19:43 +02:00
|
|
|
|
raise HTTPException(status_code=400, detail=f"Invalid JSON: {e}") from e
|
|
|
|
|
|
|
|
|
|
|
|
# 2. Endpoint logic
|
|
|
|
|
|
endpoint = await choose_endpoint(model)
|
2025-09-15 11:47:55 +02:00
|
|
|
|
is_openai_endpoint = "/v1" in endpoint
|
|
|
|
|
|
if is_openai_endpoint:
|
2025-09-15 17:48:17 +02:00
|
|
|
|
if ":latest" in model:
|
2025-09-15 19:12:00 +02:00
|
|
|
|
model = model.split(":latest")
|
2025-09-15 17:48:17 +02:00
|
|
|
|
model = model[0]
|
2025-09-15 11:47:55 +02:00
|
|
|
|
client = openai.AsyncOpenAI(base_url=endpoint, api_key=config.api_keys[endpoint])
|
|
|
|
|
|
else:
|
|
|
|
|
|
client = ollama.AsyncClient(host=endpoint)
|
2025-09-15 17:48:17 +02:00
|
|
|
|
await increment_usage(endpoint, model)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# 3. Async generator that streams embedding data and decrements the counter
|
|
|
|
|
|
async def stream_embedding_response():
|
|
|
|
|
|
try:
|
|
|
|
|
|
# The chat method returns a generator of dicts (or GenerateResponse)
|
2025-09-15 11:47:55 +02:00
|
|
|
|
if is_openai_endpoint:
|
2025-09-21 16:20:36 +02:00
|
|
|
|
async_gen = await client.embeddings.create(input=prompt, model=model)
|
2025-09-15 11:47:55 +02:00
|
|
|
|
async_gen = rechunk.openai_embeddings2ollama(async_gen)
|
|
|
|
|
|
else:
|
|
|
|
|
|
async_gen = await client.embeddings(model=model, prompt=prompt, options=options, keep_alive=keep_alive)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
if hasattr(async_gen, "model_dump_json"):
|
|
|
|
|
|
json_line = async_gen.model_dump_json()
|
|
|
|
|
|
else:
|
2025-11-10 15:37:46 +01:00
|
|
|
|
json_line = orjson.dumps(async_gen)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
yield json_line.encode("utf-8") + b"\n"
|
|
|
|
|
|
finally:
|
|
|
|
|
|
# Ensure counter is decremented even if an exception occurs
|
|
|
|
|
|
await decrement_usage(endpoint, model)
|
|
|
|
|
|
|
|
|
|
|
|
# 5. Return a StreamingResponse backed by the generator
|
|
|
|
|
|
return StreamingResponse(
|
|
|
|
|
|
stream_embedding_response(),
|
|
|
|
|
|
media_type="application/json",
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 00:12:56 +02:00
|
|
|
|
# 9. API route – Embed
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.post("/api/embed")
|
|
|
|
|
|
async def embed_proxy(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Proxy an embed request to Ollama and reply with embeddings.
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 1. Parse and validate request
|
|
|
|
|
|
try:
|
|
|
|
|
|
body_bytes = await request.body()
|
2025-11-10 15:37:46 +01:00
|
|
|
|
payload = orjson.loads(body_bytes.decode("utf-8"))
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
|
|
|
|
|
model = payload.get("model")
|
2025-09-15 11:47:55 +02:00
|
|
|
|
_input = payload.get("input")
|
2025-08-26 18:19:43 +02:00
|
|
|
|
truncate = payload.get("truncate")
|
|
|
|
|
|
options = payload.get("options")
|
|
|
|
|
|
keep_alive = payload.get("keep_alive")
|
|
|
|
|
|
|
|
|
|
|
|
if not model:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'model'"
|
|
|
|
|
|
)
|
2025-09-15 11:47:55 +02:00
|
|
|
|
if not _input:
|
2025-08-26 18:19:43 +02:00
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'input'"
|
|
|
|
|
|
)
|
2025-11-10 15:37:46 +01:00
|
|
|
|
except orjson.JSONDecodeError as e:
|
2025-08-26 18:19:43 +02:00
|
|
|
|
raise HTTPException(status_code=400, detail=f"Invalid JSON: {e}") from e
|
|
|
|
|
|
|
|
|
|
|
|
# 2. Endpoint logic
|
|
|
|
|
|
endpoint = await choose_endpoint(model)
|
2025-11-17 14:40:24 +01:00
|
|
|
|
is_openai_endpoint = is_ext_openai_endpoint(endpoint) #"/v1" in endpoint
|
2025-09-15 11:47:55 +02:00
|
|
|
|
if is_openai_endpoint:
|
2025-09-15 17:48:17 +02:00
|
|
|
|
if ":latest" in model:
|
2025-09-15 19:12:00 +02:00
|
|
|
|
model = model.split(":latest")
|
2025-09-15 17:48:17 +02:00
|
|
|
|
model = model[0]
|
2025-09-15 11:47:55 +02:00
|
|
|
|
client = openai.AsyncOpenAI(base_url=endpoint, api_key=config.api_keys[endpoint])
|
|
|
|
|
|
else:
|
|
|
|
|
|
client = ollama.AsyncClient(host=endpoint)
|
2025-09-15 17:48:17 +02:00
|
|
|
|
await increment_usage(endpoint, model)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# 3. Async generator that streams embed data and decrements the counter
|
|
|
|
|
|
async def stream_embedding_response():
|
|
|
|
|
|
try:
|
|
|
|
|
|
# The chat method returns a generator of dicts (or GenerateResponse)
|
2025-09-15 11:47:55 +02:00
|
|
|
|
if is_openai_endpoint:
|
2025-09-21 16:20:36 +02:00
|
|
|
|
async_gen = await client.embeddings.create(input=_input, model=model)
|
2025-09-15 11:47:55 +02:00
|
|
|
|
async_gen = rechunk.openai_embed2ollama(async_gen, model)
|
|
|
|
|
|
else:
|
|
|
|
|
|
async_gen = await client.embed(model=model, input=_input, truncate=truncate, options=options, keep_alive=keep_alive)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
if hasattr(async_gen, "model_dump_json"):
|
|
|
|
|
|
json_line = async_gen.model_dump_json()
|
|
|
|
|
|
else:
|
2025-11-10 15:37:46 +01:00
|
|
|
|
json_line = orjson.dumps(async_gen)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
yield json_line.encode("utf-8") + b"\n"
|
|
|
|
|
|
finally:
|
|
|
|
|
|
# Ensure counter is decremented even if an exception occurs
|
|
|
|
|
|
await decrement_usage(endpoint, model)
|
|
|
|
|
|
|
|
|
|
|
|
# 4. Return a StreamingResponse backed by the generator
|
|
|
|
|
|
return StreamingResponse(
|
|
|
|
|
|
stream_embedding_response(),
|
|
|
|
|
|
media_type="application/json",
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 00:12:56 +02:00
|
|
|
|
# 10. API route – Create
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.post("/api/create")
|
|
|
|
|
|
async def create_proxy(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Proxy a create request to all Ollama endpoints and reply with deduplicated status.
|
|
|
|
|
|
"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
body_bytes = await request.body()
|
2025-11-10 15:37:46 +01:00
|
|
|
|
payload = orjson.loads(body_bytes.decode("utf-8"))
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
|
|
|
|
|
model = payload.get("model")
|
|
|
|
|
|
quantize = payload.get("quantize")
|
|
|
|
|
|
from_ = payload.get("from")
|
|
|
|
|
|
files = payload.get("files")
|
|
|
|
|
|
adapters = payload.get("adapters")
|
|
|
|
|
|
template = payload.get("template")
|
|
|
|
|
|
license = payload.get("license")
|
|
|
|
|
|
system = payload.get("system")
|
|
|
|
|
|
parameters = payload.get("parameters")
|
|
|
|
|
|
messages = payload.get("messages")
|
|
|
|
|
|
|
|
|
|
|
|
if not model:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'model'"
|
|
|
|
|
|
)
|
|
|
|
|
|
if not from_ and not files:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="You need to provide either from_ or files parameter!"
|
|
|
|
|
|
)
|
2025-11-10 15:37:46 +01:00
|
|
|
|
except orjson.JSONDecodeError as e:
|
2025-08-26 18:19:43 +02:00
|
|
|
|
raise HTTPException(status_code=400, detail=f"Invalid JSON: {e}") from e
|
|
|
|
|
|
|
|
|
|
|
|
status_lists = []
|
|
|
|
|
|
for endpoint in config.endpoints:
|
|
|
|
|
|
client = ollama.AsyncClient(host=endpoint)
|
|
|
|
|
|
create = await client.create(model=model, quantize=quantize, from_=from_, files=files, adapters=adapters, template=template, license=license, system=system, parameters=parameters, messages=messages, stream=False)
|
|
|
|
|
|
status_lists.append(create)
|
|
|
|
|
|
|
|
|
|
|
|
combined_status = []
|
|
|
|
|
|
for status_list in status_lists:
|
|
|
|
|
|
combined_status += status_list
|
|
|
|
|
|
|
|
|
|
|
|
final_status = list(dict.fromkeys(combined_status))
|
|
|
|
|
|
|
|
|
|
|
|
return dict(final_status)
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 00:12:56 +02:00
|
|
|
|
# 11. API route – Show
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.post("/api/show")
|
2025-09-05 12:11:31 +02:00
|
|
|
|
async def show_proxy(request: Request, model: Optional[str] = None):
|
2025-08-26 18:19:43 +02:00
|
|
|
|
"""
|
|
|
|
|
|
Proxy a model show request to Ollama and reply with ShowResponse.
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
body_bytes = await request.body()
|
|
|
|
|
|
|
2025-09-05 12:11:31 +02:00
|
|
|
|
if not model:
|
2025-11-10 15:37:46 +01:00
|
|
|
|
payload = orjson.loads(body_bytes.decode("utf-8"))
|
2025-09-05 12:11:31 +02:00
|
|
|
|
model = payload.get("model")
|
2025-11-18 19:02:36 +01:00
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
if not model:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'model'"
|
|
|
|
|
|
)
|
2025-11-10 15:37:46 +01:00
|
|
|
|
except orjson.JSONDecodeError as e:
|
2025-08-26 18:19:43 +02:00
|
|
|
|
raise HTTPException(status_code=400, detail=f"Invalid JSON: {e}") from e
|
|
|
|
|
|
|
|
|
|
|
|
# 2. Endpoint logic
|
|
|
|
|
|
endpoint = await choose_endpoint(model)
|
2025-09-05 12:11:31 +02:00
|
|
|
|
#await increment_usage(endpoint, model)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
client = ollama.AsyncClient(host=endpoint)
|
|
|
|
|
|
|
|
|
|
|
|
# 3. Proxy a simple show request
|
|
|
|
|
|
show = await client.show(model=model)
|
|
|
|
|
|
|
|
|
|
|
|
# 4. Return ShowResponse
|
|
|
|
|
|
return show
|
|
|
|
|
|
|
2025-11-18 19:02:36 +01:00
|
|
|
|
# -------------------------------------------------------------
|
2025-11-28 14:59:29 +01:00
|
|
|
|
@app.get("/api/token_counts")
|
|
|
|
|
|
async def token_counts_proxy():
|
|
|
|
|
|
breakdown = []
|
|
|
|
|
|
total = 0
|
|
|
|
|
|
async for entry in db.load_token_counts():
|
|
|
|
|
|
total += entry['total_tokens']
|
|
|
|
|
|
breakdown.append({
|
|
|
|
|
|
"endpoint": entry["endpoint"],
|
|
|
|
|
|
"model": entry["model"],
|
|
|
|
|
|
"input_tokens": entry["input_tokens"],
|
|
|
|
|
|
"output_tokens": entry["output_tokens"],
|
|
|
|
|
|
"total_tokens": entry["total_tokens"],
|
|
|
|
|
|
})
|
|
|
|
|
|
return {"total_tokens": total, "breakdown": breakdown}
|
|
|
|
|
|
|
2025-12-02 12:18:23 +01:00
|
|
|
|
@app.post("/api/aggregate_time_series_days")
|
|
|
|
|
|
async def aggregate_time_series_days_proxy(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Aggregate time_series entries older than days into daily aggregates by endpoint/model/date.
|
|
|
|
|
|
"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
body_bytes = await request.body()
|
|
|
|
|
|
if not body_bytes:
|
|
|
|
|
|
days = 30
|
|
|
|
|
|
trim_old = False
|
|
|
|
|
|
else:
|
|
|
|
|
|
payload = orjson.loads(body_bytes.decode("utf-8"))
|
|
|
|
|
|
days = int(payload.get("days", 30))
|
|
|
|
|
|
trim_old = bool(payload.get("trim_old", False))
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
days = 30
|
|
|
|
|
|
trim_old = False
|
|
|
|
|
|
aggregated = await db.aggregate_time_series_older_than(days, trim_old=trim_old)
|
|
|
|
|
|
return {"status": "ok", "days": days, "trim_old": trim_old, "aggregated_groups": aggregated}
|
|
|
|
|
|
|
2025-11-18 19:02:36 +01:00
|
|
|
|
# 12. API route – Stats
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.post("/api/stats")
|
|
|
|
|
|
async def stats_proxy(request: Request, model: Optional[str] = None):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Return token usage statistics for a specific model.
|
|
|
|
|
|
"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
body_bytes = await request.body()
|
|
|
|
|
|
|
|
|
|
|
|
if not model:
|
|
|
|
|
|
payload = orjson.loads(body_bytes.decode("utf-8"))
|
|
|
|
|
|
model = payload.get("model")
|
|
|
|
|
|
|
|
|
|
|
|
if not model:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'model'"
|
|
|
|
|
|
)
|
|
|
|
|
|
except orjson.JSONDecodeError as e:
|
|
|
|
|
|
raise HTTPException(status_code=400, detail=f"Invalid JSON: {e}") from e
|
|
|
|
|
|
|
|
|
|
|
|
# Get token counts from database
|
|
|
|
|
|
token_data = await db.get_token_counts_for_model(model)
|
|
|
|
|
|
|
|
|
|
|
|
if not token_data:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=404, detail="No token data found for this model"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-11-19 17:05:25 +01:00
|
|
|
|
# Get time series data for the last 30 days (43200 minutes = 30 days)
|
|
|
|
|
|
# Assuming entries are grouped by minute, 30 days = 43200 entries max
|
2025-11-18 19:02:36 +01:00
|
|
|
|
time_series = []
|
2025-11-19 17:28:31 +01:00
|
|
|
|
endpoint_totals = defaultdict(int) # Track tokens per endpoint
|
|
|
|
|
|
|
2025-11-19 17:05:25 +01:00
|
|
|
|
async for entry in db.get_latest_time_series(limit=50000):
|
2025-11-18 19:02:36 +01:00
|
|
|
|
if entry['model'] == model:
|
|
|
|
|
|
time_series.append({
|
2025-11-19 17:28:31 +01:00
|
|
|
|
'endpoint': entry['endpoint'],
|
2025-11-18 19:02:36 +01:00
|
|
|
|
'timestamp': entry['timestamp'],
|
|
|
|
|
|
'input_tokens': entry['input_tokens'],
|
|
|
|
|
|
'output_tokens': entry['output_tokens'],
|
|
|
|
|
|
'total_tokens': entry['total_tokens']
|
|
|
|
|
|
})
|
2025-11-19 17:28:31 +01:00
|
|
|
|
# Accumulate total tokens per endpoint
|
|
|
|
|
|
endpoint_totals[entry['endpoint']] += entry['total_tokens']
|
2025-11-18 19:02:36 +01:00
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
'model': model,
|
|
|
|
|
|
'input_tokens': token_data['input_tokens'],
|
|
|
|
|
|
'output_tokens': token_data['output_tokens'],
|
|
|
|
|
|
'total_tokens': token_data['total_tokens'],
|
2025-11-19 17:28:31 +01:00
|
|
|
|
'time_series': time_series,
|
|
|
|
|
|
'endpoint_distribution': dict(endpoint_totals)
|
2025-11-18 19:02:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 00:12:56 +02:00
|
|
|
|
# 12. API route – Copy
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.post("/api/copy")
|
2025-09-05 12:11:31 +02:00
|
|
|
|
async def copy_proxy(request: Request, source: Optional[str] = None, destination: Optional[str] = None):
|
2025-08-26 18:19:43 +02:00
|
|
|
|
"""
|
|
|
|
|
|
Proxy a model copy request to each Ollama endpoint and reply with Status Code.
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 1. Parse and validate request
|
|
|
|
|
|
try:
|
|
|
|
|
|
body_bytes = await request.body()
|
|
|
|
|
|
|
2025-09-05 12:11:31 +02:00
|
|
|
|
if not source and not destination:
|
2025-11-10 15:37:46 +01:00
|
|
|
|
payload = orjson.loads(body_bytes.decode("utf-8"))
|
2025-09-05 12:11:31 +02:00
|
|
|
|
src = payload.get("source")
|
|
|
|
|
|
dst = payload.get("destination")
|
|
|
|
|
|
else:
|
|
|
|
|
|
src = source
|
|
|
|
|
|
dst = destination
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
|
|
|
|
|
if not src:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'source'"
|
|
|
|
|
|
)
|
|
|
|
|
|
if not dst:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'destination'"
|
|
|
|
|
|
)
|
2025-11-10 15:37:46 +01:00
|
|
|
|
except orjson.JSONDecodeError as e:
|
2025-08-26 18:19:43 +02:00
|
|
|
|
raise HTTPException(status_code=400, detail=f"Invalid JSON: {e}") from e
|
|
|
|
|
|
|
|
|
|
|
|
# 3. Iterate over all endpoints to copy the model on each endpoint
|
|
|
|
|
|
status_list = []
|
|
|
|
|
|
for endpoint in config.endpoints:
|
2025-09-05 12:11:31 +02:00
|
|
|
|
if "/v1" not in endpoint:
|
|
|
|
|
|
client = ollama.AsyncClient(host=endpoint)
|
|
|
|
|
|
# 4. Proxy a simple copy request
|
|
|
|
|
|
copy = await client.copy(source=src, destination=dst)
|
|
|
|
|
|
status_list.append(copy.status)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
|
|
|
|
|
# 4. Return with 200 OK if all went well, 404 if a single endpoint failed
|
2025-09-05 12:11:31 +02:00
|
|
|
|
return Response(status_code=404 if 404 in status_list else 200)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 00:12:56 +02:00
|
|
|
|
# 13. API route – Delete
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.delete("/api/delete")
|
2025-09-05 12:11:31 +02:00
|
|
|
|
async def delete_proxy(request: Request, model: Optional[str] = None):
|
2025-08-26 18:19:43 +02:00
|
|
|
|
"""
|
|
|
|
|
|
Proxy a model delete request to each Ollama endpoint and reply with Status Code.
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 1. Parse and validate request
|
|
|
|
|
|
try:
|
|
|
|
|
|
body_bytes = await request.body()
|
|
|
|
|
|
|
2025-09-05 12:11:31 +02:00
|
|
|
|
if not model:
|
2025-11-10 15:37:46 +01:00
|
|
|
|
payload = orjson.loads(body_bytes.decode("utf-8"))
|
2025-09-05 12:11:31 +02:00
|
|
|
|
model = payload.get("model")
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
|
|
|
|
|
if not model:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'model'"
|
|
|
|
|
|
)
|
2025-11-10 15:37:46 +01:00
|
|
|
|
except orjson.JSONDecodeError as e:
|
2025-08-26 18:19:43 +02:00
|
|
|
|
raise HTTPException(status_code=400, detail=f"Invalid JSON: {e}") from e
|
|
|
|
|
|
|
|
|
|
|
|
# 2. Iterate over all endpoints to delete the model on each endpoint
|
|
|
|
|
|
status_list = []
|
|
|
|
|
|
for endpoint in config.endpoints:
|
2025-09-05 12:11:31 +02:00
|
|
|
|
if "/v1" not in endpoint:
|
|
|
|
|
|
client = ollama.AsyncClient(host=endpoint)
|
|
|
|
|
|
# 3. Proxy a simple copy request
|
|
|
|
|
|
copy = await client.delete(model=model)
|
|
|
|
|
|
status_list.append(copy.status)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
2025-10-28 11:08:52 +01:00
|
|
|
|
# 4. Return 200 0K, if a single enpoint fails, respond with 404
|
2025-09-05 12:11:31 +02:00
|
|
|
|
return Response(status_code=404 if 404 in status_list else 200)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 00:12:56 +02:00
|
|
|
|
# 14. API route – Pull
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.post("/api/pull")
|
2025-09-05 12:11:31 +02:00
|
|
|
|
async def pull_proxy(request: Request, model: Optional[str] = None):
|
2025-08-26 18:19:43 +02:00
|
|
|
|
"""
|
|
|
|
|
|
Proxy a pull request to all Ollama endpoint and report status back.
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 1. Parse and validate request
|
|
|
|
|
|
try:
|
|
|
|
|
|
body_bytes = await request.body()
|
|
|
|
|
|
|
2025-09-05 12:11:31 +02:00
|
|
|
|
if not model:
|
2025-11-10 15:37:46 +01:00
|
|
|
|
payload = orjson.loads(body_bytes.decode("utf-8"))
|
2025-09-05 12:11:31 +02:00
|
|
|
|
model = payload.get("model")
|
|
|
|
|
|
insecure = payload.get("insecure")
|
|
|
|
|
|
else:
|
|
|
|
|
|
insecure = None
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
|
|
|
|
|
if not model:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'model'"
|
|
|
|
|
|
)
|
2025-11-10 15:37:46 +01:00
|
|
|
|
except orjson.JSONDecodeError as e:
|
2025-08-26 18:19:43 +02:00
|
|
|
|
raise HTTPException(status_code=400, detail=f"Invalid JSON: {e}") from e
|
|
|
|
|
|
|
|
|
|
|
|
# 2. Iterate over all endpoints to pull the model
|
|
|
|
|
|
status_list = []
|
|
|
|
|
|
for endpoint in config.endpoints:
|
2025-09-05 12:11:31 +02:00
|
|
|
|
if "/v1" not in endpoint:
|
|
|
|
|
|
client = ollama.AsyncClient(host=endpoint)
|
|
|
|
|
|
# 3. Proxy a simple pull request
|
|
|
|
|
|
pull = await client.pull(model=model, insecure=insecure, stream=False)
|
|
|
|
|
|
status_list.append(pull)
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
|
|
|
|
|
combined_status = []
|
|
|
|
|
|
for status in status_list:
|
|
|
|
|
|
combined_status += status
|
|
|
|
|
|
|
|
|
|
|
|
# 4. Report back a deduplicated status message
|
|
|
|
|
|
final_status = list(dict.fromkeys(combined_status))
|
|
|
|
|
|
|
|
|
|
|
|
return dict(final_status)
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 00:12:56 +02:00
|
|
|
|
# 15. API route – Push
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.post("/api/push")
|
|
|
|
|
|
async def push_proxy(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Proxy a push request to Ollama and respond the deduplicated Ollama endpoint replies.
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 1. Parse and validate request
|
|
|
|
|
|
try:
|
|
|
|
|
|
body_bytes = await request.body()
|
2025-11-10 15:37:46 +01:00
|
|
|
|
payload = orjson.loads(body_bytes.decode("utf-8"))
|
2025-08-26 18:19:43 +02:00
|
|
|
|
|
|
|
|
|
|
model = payload.get("model")
|
|
|
|
|
|
insecure = payload.get("insecure")
|
|
|
|
|
|
|
|
|
|
|
|
if not model:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'model'"
|
|
|
|
|
|
)
|
2025-11-10 15:37:46 +01:00
|
|
|
|
except orjson.JSONDecodeError as e:
|
2025-08-26 18:19:43 +02:00
|
|
|
|
raise HTTPException(status_code=400, detail=f"Invalid JSON: {e}") from e
|
|
|
|
|
|
|
|
|
|
|
|
# 2. Iterate over all endpoints
|
|
|
|
|
|
status_list = []
|
|
|
|
|
|
for endpoint in config.endpoints:
|
|
|
|
|
|
client = ollama.AsyncClient(host=endpoint)
|
|
|
|
|
|
# 3. Proxy a simple push request
|
|
|
|
|
|
push = await client.push(model=model, insecure=insecure, stream=False)
|
|
|
|
|
|
status_list.append(push)
|
|
|
|
|
|
|
|
|
|
|
|
combined_status = []
|
|
|
|
|
|
for status in status_list:
|
|
|
|
|
|
combined_status += status
|
|
|
|
|
|
|
|
|
|
|
|
# 4. Report a deduplicated status
|
|
|
|
|
|
final_status = list(dict.fromkeys(combined_status))
|
|
|
|
|
|
|
|
|
|
|
|
return dict(final_status)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 00:12:56 +02:00
|
|
|
|
# 16. API route – Version
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.get("/api/version")
|
|
|
|
|
|
async def version_proxy(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Proxy a version request to Ollama and reply lowest version of all endpoints.
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 1. Query all endpoints for version
|
2025-09-13 16:57:09 +02:00
|
|
|
|
tasks = [fetch.endpoint_details(ep, "/api/version", "version") for ep in config.endpoints if "/v1" not in ep]
|
2025-08-26 18:19:43 +02:00
|
|
|
|
all_versions = await asyncio.gather(*tasks)
|
2025-09-05 12:11:31 +02:00
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
def version_key(v):
|
|
|
|
|
|
return tuple(map(int, v.split('.')))
|
|
|
|
|
|
|
|
|
|
|
|
# 2. Return a JSONResponse with the min Version of all endpoints to maintain compatibility
|
|
|
|
|
|
return JSONResponse(
|
|
|
|
|
|
content={"version": str(min(all_versions, key=version_key))},
|
|
|
|
|
|
status_code=200,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 00:12:56 +02:00
|
|
|
|
# 17. API route – tags
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.get("/api/tags")
|
|
|
|
|
|
async def tags_proxy(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Proxy a tags request to Ollama endpoints and reply with a unique list of all models.
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
2025-09-05 12:11:31 +02:00
|
|
|
|
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# 1. Query all endpoints for models
|
2025-09-13 16:57:09 +02:00
|
|
|
|
tasks = [fetch.endpoint_details(ep, "/api/tags", "models") for ep in config.endpoints if "/v1" not in ep]
|
|
|
|
|
|
tasks += [fetch.endpoint_details(ep, "/models", "data", config.api_keys[ep]) for ep in config.endpoints if "/v1" in ep]
|
2025-08-26 18:19:43 +02:00
|
|
|
|
all_models = await asyncio.gather(*tasks)
|
|
|
|
|
|
|
|
|
|
|
|
models = {'models': []}
|
|
|
|
|
|
for modellist in all_models:
|
2025-09-15 11:57:00 +02:00
|
|
|
|
for model in modellist:
|
|
|
|
|
|
if not "model" in model.keys(): # Relable OpenAI models with Ollama Model.model from Model.id
|
2025-09-15 19:12:00 +02:00
|
|
|
|
model['model'] = model['id'] + ":latest"
|
2025-09-15 11:57:00 +02:00
|
|
|
|
else:
|
|
|
|
|
|
model['id'] = model['model']
|
2025-09-15 17:00:53 +02:00
|
|
|
|
if not "name" in model.keys(): # Relable OpenAI models with Ollama Model.name from Model.model to have model,name keys
|
|
|
|
|
|
model['name'] = model['model']
|
|
|
|
|
|
else:
|
|
|
|
|
|
model['id'] = model['model']
|
2025-08-26 18:19:43 +02:00
|
|
|
|
models['models'] += modellist
|
|
|
|
|
|
|
|
|
|
|
|
# 2. Return a JSONResponse with a deduplicated list of unique models for inference
|
|
|
|
|
|
return JSONResponse(
|
2025-09-05 12:11:31 +02:00
|
|
|
|
content={"models": dedupe_on_keys(models['models'], ['digest','name','id'])},
|
2025-08-26 18:19:43 +02:00
|
|
|
|
status_code=200,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 00:12:56 +02:00
|
|
|
|
# 18. API route – ps
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.get("/api/ps")
|
|
|
|
|
|
async def ps_proxy(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Proxy a ps request to all Ollama endpoints and reply a unique list of all running models.
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 1. Query all endpoints for running models
|
2025-09-13 16:57:09 +02:00
|
|
|
|
tasks = [fetch.endpoint_details(ep, "/api/ps", "models") for ep in config.endpoints if "/v1" not in ep]
|
2025-08-26 18:19:43 +02:00
|
|
|
|
loaded_models = await asyncio.gather(*tasks)
|
|
|
|
|
|
|
|
|
|
|
|
models = {'models': []}
|
|
|
|
|
|
for modellist in loaded_models:
|
|
|
|
|
|
models['models'] += modellist
|
|
|
|
|
|
|
2025-09-01 09:01:41 +02:00
|
|
|
|
# 2. Return a JSONResponse with deduplicated currently deployed models
|
2025-08-26 18:19:43 +02:00
|
|
|
|
return JSONResponse(
|
|
|
|
|
|
content={"models": dedupe_on_keys(models['models'], ['digest'])},
|
|
|
|
|
|
status_code=200,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 00:12:56 +02:00
|
|
|
|
# 19. Proxy usage route – for monitoring
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.get("/api/usage")
|
|
|
|
|
|
async def usage_proxy(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Return a snapshot of the usage counter for each endpoint.
|
|
|
|
|
|
Useful for debugging / monitoring.
|
|
|
|
|
|
"""
|
2025-11-04 17:55:19 +01:00
|
|
|
|
return {"usage_counts": usage_counts,
|
|
|
|
|
|
"token_usage_counts": token_usage_counts}
|
2025-08-30 00:12:56 +02:00
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
# 20. Proxy config route – for monitoring and frontent usage
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.get("/api/config")
|
|
|
|
|
|
async def config_proxy(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Return a simple JSON object that contains the configured
|
|
|
|
|
|
Ollama endpoints. The front‑end uses this to display
|
|
|
|
|
|
which endpoints are being proxied.
|
|
|
|
|
|
"""
|
2025-08-30 12:43:35 +02:00
|
|
|
|
async def check_endpoint(url: str):
|
2025-11-07 13:59:16 +01:00
|
|
|
|
client: aiohttp.ClientSession = app_state["session"]
|
|
|
|
|
|
headers = None
|
|
|
|
|
|
if "/v1" in url:
|
|
|
|
|
|
headers = {"Authorization": "Bearer " + config.api_keys[url]}
|
|
|
|
|
|
target_url = f"{url}/models"
|
|
|
|
|
|
else:
|
|
|
|
|
|
target_url = f"{url}/api/version"
|
|
|
|
|
|
|
2025-08-30 12:43:35 +02:00
|
|
|
|
try:
|
2025-11-07 13:59:16 +01:00
|
|
|
|
async with client.get(target_url, headers=headers) as resp:
|
|
|
|
|
|
await _ensure_success(resp)
|
|
|
|
|
|
data = await resp.json()
|
2025-09-10 10:21:49 +02:00
|
|
|
|
if "/v1" in url:
|
|
|
|
|
|
return {"url": url, "status": "ok", "version": "latest"}
|
|
|
|
|
|
else:
|
|
|
|
|
|
return {"url": url, "status": "ok", "version": data.get("version")}
|
2025-09-11 18:53:23 +02:00
|
|
|
|
except Exception as e:
|
2025-11-07 13:59:16 +01:00
|
|
|
|
detail = _format_connection_issue(target_url, e)
|
|
|
|
|
|
return {"url": url, "status": "error", "detail": detail}
|
2025-08-30 12:43:35 +02:00
|
|
|
|
|
|
|
|
|
|
results = await asyncio.gather(*[check_endpoint(ep) for ep in config.endpoints])
|
2026-01-14 09:28:02 +01:00
|
|
|
|
return {
|
|
|
|
|
|
"endpoints": results,
|
|
|
|
|
|
"require_router_api_key": bool(config.router_api_key),
|
|
|
|
|
|
}
|
2025-08-30 00:12:56 +02:00
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
# 21. API route – OpenAI compatible Embedding
|
2025-08-28 09:40:33 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.post("/v1/embeddings")
|
|
|
|
|
|
async def openai_embedding_proxy(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Proxy an OpenAI API compatible embedding request to Ollama and reply with embeddings.
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 1. Parse and validate request
|
|
|
|
|
|
try:
|
|
|
|
|
|
body_bytes = await request.body()
|
2025-11-10 15:37:46 +01:00
|
|
|
|
payload = orjson.loads(body_bytes.decode("utf-8"))
|
2025-08-28 09:40:33 +02:00
|
|
|
|
|
|
|
|
|
|
model = payload.get("model")
|
2025-09-11 13:56:51 +02:00
|
|
|
|
doc = payload.get("input")
|
2025-09-05 12:11:31 +02:00
|
|
|
|
|
2025-08-28 09:40:33 +02:00
|
|
|
|
if not model:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'model'"
|
|
|
|
|
|
)
|
2025-09-11 13:56:51 +02:00
|
|
|
|
if not doc:
|
2025-08-28 09:40:33 +02:00
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'input'"
|
|
|
|
|
|
)
|
2025-11-10 15:37:46 +01:00
|
|
|
|
except orjson.JSONDecodeError as e:
|
2025-08-28 09:40:33 +02:00
|
|
|
|
raise HTTPException(status_code=400, detail=f"Invalid JSON: {e}") from e
|
|
|
|
|
|
|
|
|
|
|
|
# 2. Endpoint logic
|
|
|
|
|
|
endpoint = await choose_endpoint(model)
|
|
|
|
|
|
await increment_usage(endpoint, model)
|
2025-11-14 15:53:26 +01:00
|
|
|
|
if "/v1" in endpoint: # and is_ext_openai_endpoint(endpoint):
|
2025-09-05 12:11:31 +02:00
|
|
|
|
api_key = config.api_keys[endpoint]
|
|
|
|
|
|
else:
|
|
|
|
|
|
api_key = "ollama"
|
2025-09-15 09:04:38 +02:00
|
|
|
|
base_url = ep2base(endpoint)
|
2025-09-21 16:20:36 +02:00
|
|
|
|
oclient = openai.AsyncOpenAI(base_url=base_url, default_headers=default_headers, api_key=api_key)
|
2025-08-28 09:40:33 +02:00
|
|
|
|
|
|
|
|
|
|
# 3. Async generator that streams embedding data and decrements the counter
|
2025-09-21 16:20:36 +02:00
|
|
|
|
async_gen = await oclient.embeddings.create(input=doc, model=model)
|
2025-08-28 09:40:33 +02:00
|
|
|
|
|
|
|
|
|
|
await decrement_usage(endpoint, model)
|
|
|
|
|
|
|
|
|
|
|
|
# 5. Return a StreamingResponse backed by the generator
|
|
|
|
|
|
return async_gen
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 00:12:56 +02:00
|
|
|
|
# 22. API route – OpenAI compatible Chat Completions
|
2025-08-27 09:23:59 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.post("/v1/chat/completions")
|
2025-08-28 09:40:33 +02:00
|
|
|
|
async def openai_chat_completions_proxy(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Proxy an OpenAI API compatible chat completions request to Ollama and reply with a streaming response.
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 1. Parse and validate request
|
|
|
|
|
|
try:
|
|
|
|
|
|
body_bytes = await request.body()
|
2025-11-10 15:37:46 +01:00
|
|
|
|
payload = orjson.loads(body_bytes.decode("utf-8"))
|
2025-08-28 09:40:33 +02:00
|
|
|
|
|
|
|
|
|
|
model = payload.get("model")
|
|
|
|
|
|
messages = payload.get("messages")
|
|
|
|
|
|
frequency_penalty = payload.get("frequency_penalty")
|
|
|
|
|
|
presence_penalty = payload.get("presence_penalty")
|
|
|
|
|
|
response_format = payload.get("response_format")
|
|
|
|
|
|
seed = payload.get("seed")
|
|
|
|
|
|
stop = payload.get("stop")
|
|
|
|
|
|
stream = payload.get("stream")
|
|
|
|
|
|
stream_options = payload.get("stream_options")
|
|
|
|
|
|
temperature = payload.get("temperature")
|
|
|
|
|
|
top_p = payload.get("top_p")
|
|
|
|
|
|
max_tokens = payload.get("max_tokens")
|
2025-09-05 12:11:31 +02:00
|
|
|
|
max_completion_tokens = payload.get("max_completion_tokens")
|
2025-08-30 00:12:56 +02:00
|
|
|
|
tools = payload.get("tools")
|
|
|
|
|
|
|
2025-09-17 11:40:48 +02:00
|
|
|
|
if ":latest" in model:
|
|
|
|
|
|
model = model.split(":latest")
|
|
|
|
|
|
model = model[0]
|
|
|
|
|
|
|
2025-08-30 00:12:56 +02:00
|
|
|
|
params = {
|
|
|
|
|
|
"messages": messages,
|
|
|
|
|
|
"model": model,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-11 13:56:51 +02:00
|
|
|
|
optional_params = {
|
|
|
|
|
|
"tools": tools,
|
|
|
|
|
|
"response_format": response_format,
|
2025-11-21 09:56:42 +01:00
|
|
|
|
"stream_options": stream_options or {"include_usage": True },
|
2025-09-11 13:56:51 +02:00
|
|
|
|
"max_completion_tokens": max_completion_tokens,
|
|
|
|
|
|
"max_tokens": max_tokens,
|
|
|
|
|
|
"temperature": temperature,
|
|
|
|
|
|
"top_p": top_p,
|
|
|
|
|
|
"seed": seed,
|
|
|
|
|
|
"presence_penalty": presence_penalty,
|
|
|
|
|
|
"frequency_penalty": frequency_penalty,
|
|
|
|
|
|
"stop": stop,
|
|
|
|
|
|
"stream": stream,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
params.update({k: v for k, v in optional_params.items() if v is not None})
|
2025-08-28 09:40:33 +02:00
|
|
|
|
|
|
|
|
|
|
if not model:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'model'"
|
|
|
|
|
|
)
|
|
|
|
|
|
if not isinstance(messages, list):
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'messages' (must be a list)"
|
|
|
|
|
|
)
|
2025-11-10 15:37:46 +01:00
|
|
|
|
except orjson.JSONDecodeError as e:
|
2025-08-28 09:40:33 +02:00
|
|
|
|
raise HTTPException(status_code=400, detail=f"Invalid JSON: {e}") from e
|
|
|
|
|
|
|
|
|
|
|
|
# 2. Endpoint logic
|
|
|
|
|
|
endpoint = await choose_endpoint(model)
|
|
|
|
|
|
await increment_usage(endpoint, model)
|
2025-08-30 00:12:56 +02:00
|
|
|
|
base_url = ep2base(endpoint)
|
2025-09-21 16:20:36 +02:00
|
|
|
|
oclient = openai.AsyncOpenAI(base_url=base_url, default_headers=default_headers, api_key=config.api_keys[endpoint])
|
2025-08-28 09:40:33 +02:00
|
|
|
|
# 3. Async generator that streams completions data and decrements the counter
|
|
|
|
|
|
async def stream_ochat_response():
|
|
|
|
|
|
try:
|
|
|
|
|
|
# The chat method returns a generator of dicts (or GenerateResponse)
|
2025-08-30 00:12:56 +02:00
|
|
|
|
async_gen = await oclient.chat.completions.create(**params)
|
2025-08-28 09:40:33 +02:00
|
|
|
|
if stream == True:
|
|
|
|
|
|
async for chunk in async_gen:
|
|
|
|
|
|
data = (
|
|
|
|
|
|
chunk.model_dump_json()
|
|
|
|
|
|
if hasattr(chunk, "model_dump_json")
|
2025-11-10 15:37:46 +01:00
|
|
|
|
else orjson.dumps(chunk)
|
2025-08-28 09:40:33 +02:00
|
|
|
|
)
|
2025-11-21 09:56:42 +01:00
|
|
|
|
if chunk.choices:
|
|
|
|
|
|
if chunk.choices[0].delta.content is not None:
|
|
|
|
|
|
yield f"data: {data}\n\n".encode("utf-8")
|
|
|
|
|
|
if chunk.usage is not None:
|
|
|
|
|
|
prompt_tok = chunk.usage.prompt_tokens or 0
|
|
|
|
|
|
comp_tok = chunk.usage.completion_tokens or 0
|
|
|
|
|
|
if prompt_tok != 0 or comp_tok != 0:
|
2025-12-13 12:34:24 +01:00
|
|
|
|
local_model = model
|
2025-11-21 09:56:42 +01:00
|
|
|
|
if not is_ext_openai_endpoint(endpoint):
|
2025-12-13 12:34:24 +01:00
|
|
|
|
if not ":" in model:
|
|
|
|
|
|
local_model = model if ":" in model else model + ":latest"
|
2025-11-24 09:33:54 +01:00
|
|
|
|
await token_queue.put((endpoint, local_model, prompt_tok, comp_tok))
|
2025-09-23 17:33:15 +02:00
|
|
|
|
yield b"data: [DONE]\n\n"
|
2025-08-28 09:40:33 +02:00
|
|
|
|
else:
|
2025-11-06 14:27:34 +01:00
|
|
|
|
prompt_tok = async_gen.usage.prompt_tokens or 0
|
|
|
|
|
|
comp_tok = async_gen.usage.completion_tokens or 0
|
2025-11-18 19:02:36 +01:00
|
|
|
|
if prompt_tok != 0 or comp_tok != 0:
|
|
|
|
|
|
await token_queue.put((endpoint, model, prompt_tok, comp_tok))
|
2025-08-28 09:40:33 +02:00
|
|
|
|
json_line = (
|
|
|
|
|
|
async_gen.model_dump_json()
|
|
|
|
|
|
if hasattr(async_gen, "model_dump_json")
|
2025-11-10 15:37:46 +01:00
|
|
|
|
else orjson.dumps(async_gen)
|
2025-08-28 09:40:33 +02:00
|
|
|
|
)
|
|
|
|
|
|
yield json_line.encode("utf-8") + b"\n"
|
|
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
# Ensure counter is decremented even if an exception occurs
|
|
|
|
|
|
await decrement_usage(endpoint, model)
|
|
|
|
|
|
|
|
|
|
|
|
# 4. Return a StreamingResponse backed by the generator
|
|
|
|
|
|
return StreamingResponse(
|
|
|
|
|
|
stream_ochat_response(),
|
|
|
|
|
|
media_type="application/json",
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 00:12:56 +02:00
|
|
|
|
# 23. API route – OpenAI compatible Completions
|
2025-08-28 09:40:33 +02:00
|
|
|
|
# -------------------------------------------------------------
|
2025-08-27 09:23:59 +02:00
|
|
|
|
@app.post("/v1/completions")
|
2025-08-28 09:40:33 +02:00
|
|
|
|
async def openai_completions_proxy(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Proxy an OpenAI API compatible chat completions request to Ollama and reply with a streaming response.
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 1. Parse and validate request
|
|
|
|
|
|
try:
|
|
|
|
|
|
body_bytes = await request.body()
|
2025-11-10 15:37:46 +01:00
|
|
|
|
payload = orjson.loads(body_bytes.decode("utf-8"))
|
2025-08-28 09:40:33 +02:00
|
|
|
|
|
|
|
|
|
|
model = payload.get("model")
|
|
|
|
|
|
prompt = payload.get("prompt")
|
|
|
|
|
|
frequency_penalty = payload.get("frequency_penalty")
|
|
|
|
|
|
presence_penalty = payload.get("presence_penalty")
|
|
|
|
|
|
seed = payload.get("seed")
|
|
|
|
|
|
stop = payload.get("stop")
|
|
|
|
|
|
stream = payload.get("stream")
|
|
|
|
|
|
stream_options = payload.get("stream_options")
|
|
|
|
|
|
temperature = payload.get("temperature")
|
|
|
|
|
|
top_p = payload.get("top_p")
|
|
|
|
|
|
max_tokens = payload.get("max_tokens")
|
2025-09-05 12:11:31 +02:00
|
|
|
|
max_completion_tokens = payload.get("max_completion_tokens")
|
2025-08-30 00:12:56 +02:00
|
|
|
|
suffix = payload.get("suffix")
|
|
|
|
|
|
|
2025-09-17 11:40:48 +02:00
|
|
|
|
if ":latest" in model:
|
|
|
|
|
|
model = model.split(":latest")
|
|
|
|
|
|
model = model[0]
|
|
|
|
|
|
|
2025-08-30 00:12:56 +02:00
|
|
|
|
params = {
|
|
|
|
|
|
"prompt": prompt,
|
|
|
|
|
|
"model": model,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-11 13:56:51 +02:00
|
|
|
|
optional_params = {
|
|
|
|
|
|
"frequency_penalty": frequency_penalty,
|
|
|
|
|
|
"presence_penalty": presence_penalty,
|
|
|
|
|
|
"seed": seed,
|
|
|
|
|
|
"stop": stop,
|
|
|
|
|
|
"stream": stream,
|
2025-11-21 09:56:42 +01:00
|
|
|
|
"stream_options": stream_options or {"include_usage": True },
|
2025-09-11 13:56:51 +02:00
|
|
|
|
"temperature": temperature,
|
|
|
|
|
|
"top_p": top_p,
|
|
|
|
|
|
"max_tokens": max_tokens,
|
|
|
|
|
|
"max_completion_tokens": max_completion_tokens,
|
|
|
|
|
|
"suffix": suffix
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
params.update({k: v for k, v in optional_params.items() if v is not None})
|
2025-09-10 15:25:25 +02:00
|
|
|
|
|
2025-08-28 09:40:33 +02:00
|
|
|
|
if not model:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'model'"
|
|
|
|
|
|
)
|
|
|
|
|
|
if not prompt:
|
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
|
status_code=400, detail="Missing required field 'prompt'"
|
|
|
|
|
|
)
|
2025-11-10 15:37:46 +01:00
|
|
|
|
except orjson.JSONDecodeError as e:
|
2025-08-28 09:40:33 +02:00
|
|
|
|
raise HTTPException(status_code=400, detail=f"Invalid JSON: {e}") from e
|
|
|
|
|
|
|
|
|
|
|
|
# 2. Endpoint logic
|
|
|
|
|
|
endpoint = await choose_endpoint(model)
|
|
|
|
|
|
await increment_usage(endpoint, model)
|
2025-08-30 00:12:56 +02:00
|
|
|
|
base_url = ep2base(endpoint)
|
2025-09-21 16:20:36 +02:00
|
|
|
|
oclient = openai.AsyncOpenAI(base_url=base_url, default_headers=default_headers, api_key=config.api_keys[endpoint])
|
2025-08-28 09:40:33 +02:00
|
|
|
|
|
|
|
|
|
|
# 3. Async generator that streams completions data and decrements the counter
|
2025-11-21 09:56:42 +01:00
|
|
|
|
async def stream_ocompletions_response(model=model):
|
2025-08-28 09:40:33 +02:00
|
|
|
|
try:
|
|
|
|
|
|
# The chat method returns a generator of dicts (or GenerateResponse)
|
2025-08-30 00:12:56 +02:00
|
|
|
|
async_gen = await oclient.completions.create(**params)
|
2025-08-28 09:40:33 +02:00
|
|
|
|
if stream == True:
|
|
|
|
|
|
async for chunk in async_gen:
|
|
|
|
|
|
data = (
|
|
|
|
|
|
chunk.model_dump_json()
|
|
|
|
|
|
if hasattr(chunk, "model_dump_json")
|
2025-11-10 15:37:46 +01:00
|
|
|
|
else orjson.dumps(chunk)
|
2025-08-28 09:40:33 +02:00
|
|
|
|
)
|
2025-11-21 09:56:42 +01:00
|
|
|
|
if chunk.choices:
|
|
|
|
|
|
if chunk.choices[0].finish_reason == None:
|
|
|
|
|
|
yield f"data: {data}\n\n".encode("utf-8")
|
|
|
|
|
|
if chunk.usage is not None:
|
|
|
|
|
|
prompt_tok = chunk.usage.prompt_tokens or 0
|
|
|
|
|
|
comp_tok = chunk.usage.completion_tokens or 0
|
|
|
|
|
|
if prompt_tok != 0 or comp_tok != 0:
|
2025-12-13 12:34:24 +01:00
|
|
|
|
local_model = model
|
2025-11-21 09:56:42 +01:00
|
|
|
|
if not is_ext_openai_endpoint(endpoint):
|
|
|
|
|
|
if not ":" in model:
|
2025-12-13 11:58:49 +01:00
|
|
|
|
local_model = model if ":" in model else model + ":latest"
|
2025-11-24 09:33:54 +01:00
|
|
|
|
await token_queue.put((endpoint, local_model, prompt_tok, comp_tok))
|
2025-08-28 09:40:33 +02:00
|
|
|
|
# Final DONE event
|
|
|
|
|
|
yield b"data: [DONE]\n\n"
|
|
|
|
|
|
else:
|
2025-11-06 14:27:34 +01:00
|
|
|
|
prompt_tok = async_gen.usage.prompt_tokens or 0
|
|
|
|
|
|
comp_tok = async_gen.usage.completion_tokens or 0
|
2025-11-18 19:02:36 +01:00
|
|
|
|
if prompt_tok != 0 or comp_tok != 0:
|
|
|
|
|
|
await token_queue.put((endpoint, model, prompt_tok, comp_tok))
|
2025-08-28 09:40:33 +02:00
|
|
|
|
json_line = (
|
|
|
|
|
|
async_gen.model_dump_json()
|
|
|
|
|
|
if hasattr(async_gen, "model_dump_json")
|
2025-11-10 15:37:46 +01:00
|
|
|
|
else orjson.dumps(async_gen)
|
2025-08-28 09:40:33 +02:00
|
|
|
|
)
|
|
|
|
|
|
yield json_line.encode("utf-8") + b"\n"
|
|
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
# Ensure counter is decremented even if an exception occurs
|
|
|
|
|
|
await decrement_usage(endpoint, model)
|
|
|
|
|
|
|
|
|
|
|
|
# 4. Return a StreamingResponse backed by the generator
|
|
|
|
|
|
return StreamingResponse(
|
|
|
|
|
|
stream_ocompletions_response(),
|
|
|
|
|
|
media_type="application/json",
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 00:12:56 +02:00
|
|
|
|
# 24. OpenAI API compatible models endpoint
|
2025-08-28 09:40:33 +02:00
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 00:12:56 +02:00
|
|
|
|
@app.get("/v1/models")
|
|
|
|
|
|
async def openai_models_proxy(request: Request):
|
|
|
|
|
|
"""
|
2025-09-05 12:11:31 +02:00
|
|
|
|
Proxy an OpenAI API models request to Ollama endpoints and reply with a unique list of all models.
|
2025-08-27 09:23:59 +02:00
|
|
|
|
|
2025-08-30 00:12:56 +02:00
|
|
|
|
"""
|
|
|
|
|
|
# 1. Query all endpoints for models
|
2025-09-13 16:57:09 +02:00
|
|
|
|
tasks = [fetch.endpoint_details(ep, "/api/tags", "models") for ep in config.endpoints if "/v1" not in ep]
|
|
|
|
|
|
tasks += [fetch.endpoint_details(ep, "/models", "data", config.api_keys[ep]) for ep in config.endpoints if "/v1" in ep]
|
2025-08-30 00:12:56 +02:00
|
|
|
|
all_models = await asyncio.gather(*tasks)
|
|
|
|
|
|
|
|
|
|
|
|
models = {'data': []}
|
|
|
|
|
|
for modellist in all_models:
|
2025-09-05 12:11:31 +02:00
|
|
|
|
for model in modellist:
|
|
|
|
|
|
if not "id" in model.keys(): # Relable Ollama models with OpenAI Model.id from Model.name
|
|
|
|
|
|
model['id'] = model['name']
|
|
|
|
|
|
else:
|
|
|
|
|
|
model['name'] = model['id']
|
2025-08-30 00:12:56 +02:00
|
|
|
|
models['data'] += modellist
|
|
|
|
|
|
|
|
|
|
|
|
# 2. Return a JSONResponse with a deduplicated list of unique models for inference
|
|
|
|
|
|
return JSONResponse(
|
|
|
|
|
|
content={"data": dedupe_on_keys(models['data'], ['name'])},
|
|
|
|
|
|
status_code=200,
|
2025-08-27 09:23:59 +02:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 00:12:56 +02:00
|
|
|
|
# 25. Serve the static front‑end
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
|
|
|
2025-08-30 12:43:35 +02:00
|
|
|
|
@app.get("/favicon.ico")
|
|
|
|
|
|
async def redirect_favicon():
|
|
|
|
|
|
return RedirectResponse(url="/static/favicon.ico")
|
|
|
|
|
|
|
2025-08-30 00:12:56 +02:00
|
|
|
|
@app.get("/", response_class=HTMLResponse)
|
|
|
|
|
|
async def index(request: Request):
|
|
|
|
|
|
"""
|
2025-09-05 12:11:31 +02:00
|
|
|
|
Render the dynamic NOMYO Router dashboard listing the configured endpoints
|
|
|
|
|
|
and the models details, availability & task status.
|
2025-08-30 00:12:56 +02:00
|
|
|
|
"""
|
2026-01-05 17:16:31 +01:00
|
|
|
|
index_path = STATIC_DIR / "index.html"
|
|
|
|
|
|
try:
|
|
|
|
|
|
return HTMLResponse(content=index_path.read_text(encoding="utf-8"), status_code=200)
|
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
|
raise HTTPException(status_code=404, detail="Page not found")
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
raise HTTPException(status_code=500, detail="Internal server error")
|
2025-08-30 00:12:56 +02:00
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-08-30 12:43:35 +02:00
|
|
|
|
# 26. Healthendpoint
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.get("/health")
|
|
|
|
|
|
async def health_proxy(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Health‑check endpoint for monitoring the proxy.
|
|
|
|
|
|
|
|
|
|
|
|
* Queries each configured endpoint for its `/api/version` response.
|
|
|
|
|
|
* Returns a JSON object containing:
|
|
|
|
|
|
- `status`: "ok" if every endpoint replied, otherwise "error".
|
|
|
|
|
|
- `endpoints`: a mapping of endpoint URL → `{status, version|detail}`.
|
|
|
|
|
|
* The HTTP status code is 200 when everything is healthy, 503 otherwise.
|
|
|
|
|
|
"""
|
|
|
|
|
|
# Run all health checks in parallel
|
2025-11-14 15:53:26 +01:00
|
|
|
|
tasks = [fetch.endpoint_details(ep, "/api/version", "version") for ep in config.endpoints] # if not is_ext_openai_endpoint(ep)]
|
2025-08-30 12:43:35 +02:00
|
|
|
|
|
|
|
|
|
|
results = await asyncio.gather(*tasks, return_exceptions=True)
|
|
|
|
|
|
|
|
|
|
|
|
health_summary = {}
|
|
|
|
|
|
overall_ok = True
|
|
|
|
|
|
|
|
|
|
|
|
for ep, result in zip(config.endpoints, results):
|
|
|
|
|
|
if isinstance(result, Exception):
|
|
|
|
|
|
# Endpoint did not respond / returned an error
|
|
|
|
|
|
health_summary[ep] = {"status": "error", "detail": str(result)}
|
|
|
|
|
|
overall_ok = False
|
|
|
|
|
|
else:
|
|
|
|
|
|
# Successful response – report the reported version
|
|
|
|
|
|
health_summary[ep] = {"status": "ok", "version": result}
|
|
|
|
|
|
|
|
|
|
|
|
response_payload = {
|
|
|
|
|
|
"status": "ok" if overall_ok else "error",
|
|
|
|
|
|
"endpoints": health_summary,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
http_status = 200 if overall_ok else 503
|
|
|
|
|
|
return JSONResponse(content=response_payload, status_code=http_status)
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-09-05 12:11:31 +02:00
|
|
|
|
# 27. SSE route for usage broadcasts
|
|
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.get("/api/usage-stream")
|
|
|
|
|
|
async def usage_stream(request: Request):
|
|
|
|
|
|
"""
|
|
|
|
|
|
Server‑Sent‑Events that emits a JSON payload every time the
|
|
|
|
|
|
global `usage_counts` dictionary changes.
|
|
|
|
|
|
"""
|
|
|
|
|
|
async def event_generator():
|
|
|
|
|
|
# The queue that receives *every* new snapshot
|
|
|
|
|
|
queue = await subscribe()
|
|
|
|
|
|
try:
|
|
|
|
|
|
while True:
|
|
|
|
|
|
# If the client disconnects, cancel the loop
|
|
|
|
|
|
if await request.is_disconnected():
|
|
|
|
|
|
break
|
|
|
|
|
|
data = await queue.get()
|
2025-09-12 09:44:56 +02:00
|
|
|
|
if data is None:
|
|
|
|
|
|
break
|
2025-09-05 12:11:31 +02:00
|
|
|
|
# Send the data as a single SSE message
|
|
|
|
|
|
yield f"data: {data}\n\n"
|
|
|
|
|
|
finally:
|
|
|
|
|
|
# Clean‑up: unsubscribe from the broadcast channel
|
|
|
|
|
|
await unsubscribe(queue)
|
|
|
|
|
|
|
|
|
|
|
|
return StreamingResponse(event_generator(), media_type="text/event-stream")
|
|
|
|
|
|
|
|
|
|
|
|
# -------------------------------------------------------------
|
2025-09-10 10:21:49 +02:00
|
|
|
|
# 28. FastAPI startup/shutdown events
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# -------------------------------------------------------------
|
|
|
|
|
|
@app.on_event("startup")
|
|
|
|
|
|
async def startup_event() -> None:
|
2025-11-18 11:16:21 +01:00
|
|
|
|
global config, db
|
2025-08-26 18:19:43 +02:00
|
|
|
|
# Load YAML config (or use defaults if not present)
|
2025-11-07 13:59:16 +01:00
|
|
|
|
config_path = _config_path_from_env()
|
|
|
|
|
|
config = Config.from_yaml(config_path)
|
|
|
|
|
|
if config_path.exists():
|
|
|
|
|
|
print(
|
|
|
|
|
|
f"Loaded configuration from {config_path}:\n"
|
|
|
|
|
|
f" endpoints={config.endpoints},\n"
|
|
|
|
|
|
f" max_concurrent_connections={config.max_concurrent_connections}"
|
|
|
|
|
|
)
|
|
|
|
|
|
else:
|
|
|
|
|
|
print(
|
|
|
|
|
|
f"No configuration file found at {config_path}. "
|
|
|
|
|
|
"Falling back to default settings."
|
|
|
|
|
|
)
|
2025-11-18 11:16:21 +01:00
|
|
|
|
|
|
|
|
|
|
# Initialize database
|
|
|
|
|
|
db = TokenDatabase(config.db_path)
|
|
|
|
|
|
await db.init_db()
|
|
|
|
|
|
|
|
|
|
|
|
# Load existing token counts from database
|
|
|
|
|
|
async for count_entry in db.load_token_counts():
|
|
|
|
|
|
endpoint = count_entry['endpoint']
|
|
|
|
|
|
model = count_entry['model']
|
|
|
|
|
|
input_tokens = count_entry['input_tokens']
|
|
|
|
|
|
output_tokens = count_entry['output_tokens']
|
|
|
|
|
|
total_tokens = count_entry['total_tokens']
|
|
|
|
|
|
|
|
|
|
|
|
token_usage_counts[endpoint][model] = total_tokens
|
|
|
|
|
|
|
2025-09-10 10:21:49 +02:00
|
|
|
|
ssl_context = ssl.create_default_context()
|
|
|
|
|
|
connector = aiohttp.TCPConnector(limit=0, limit_per_host=512, ssl=ssl_context)
|
2025-10-03 10:04:50 +02:00
|
|
|
|
timeout = aiohttp.ClientTimeout(total=60, connect=15, sock_read=120, sock_connect=15)
|
2025-09-10 10:21:49 +02:00
|
|
|
|
session = aiohttp.ClientSession(connector=connector, timeout=timeout)
|
|
|
|
|
|
|
|
|
|
|
|
app_state["connector"] = connector
|
|
|
|
|
|
app_state["session"] = session
|
2025-11-13 10:13:10 +01:00
|
|
|
|
token_worker_task = asyncio.create_task(token_worker())
|
2025-11-18 11:16:21 +01:00
|
|
|
|
flush_task = asyncio.create_task(flush_buffer())
|
2025-09-10 10:21:49 +02:00
|
|
|
|
|
|
|
|
|
|
@app.on_event("shutdown")
|
|
|
|
|
|
async def shutdown_event() -> None:
|
2025-09-12 09:44:56 +02:00
|
|
|
|
await close_all_sse_queues()
|
2025-12-02 12:18:23 +01:00
|
|
|
|
await flush_remaining_buffers()
|
2025-10-03 10:04:50 +02:00
|
|
|
|
await app_state["session"].close()
|
2025-11-14 15:53:26 +01:00
|
|
|
|
if token_worker_task is not None:
|
2025-11-18 11:16:21 +01:00
|
|
|
|
token_worker_task.cancel()
|
|
|
|
|
|
if flush_task is not None:
|
|
|
|
|
|
flush_task.cancel()
|