checking system…
Docs / back / src/maf/dashboard/routers/llm.py · line 71
Python · 114 lines
  1"""LLM picker + OpenRouter × Ollama rankings endpoints."""
  2
  3from __future__ import annotations
  4
  5import os
  6from pathlib import Path
  7from typing import Any
  8
  9from fastapi import APIRouter, Request
 10from fastapi.responses import HTMLResponse
 11from fastapi.templating import Jinja2Templates
 12
 13router = APIRouter()
 14_TEMPLATES_DIR = Path(__file__).resolve().parents[1] / "templates"
 15templates = Jinja2Templates(directory=str(_TEMPLATES_DIR))
 16
 17
 18@router.get("/api/llm/rankings")
 19async def api_llm_rankings(category: str = "programming", force: bool = False) -> dict[str, Any]:
 20    """Return the OpenRouter leaderboard for ``category``, joined with Ollama Cloud.
 21
 22    Each entry has ``rank``, ``permaslug``, ``total_tokens`` and either a
 23    populated ``ollama_id``/``ollama_match_quality`` (model is available on
 24    your Ollama Cloud account) or empty strings (not available — UI greys it out).
 25    """
 26    from maf.llm.openrouter_rankings import (
 27        KNOWN_CATEGORIES, PROFILE_TO_CATEGORY, get_cache,
 28    )
 29
 30    if category not in KNOWN_CATEGORIES:
 31        return {"category": category, "error": "unknown category",
 32                "known": sorted(KNOWN_CATEGORIES)}
 33
 34    cache = get_cache()
 35    ranking = await cache.get(
 36        category,
 37        ollama_api_key=os.environ.get("OLLAMA_API_KEY", ""),
 38        force=bool(force),
 39    )
 40
 41    return {
 42        "category": category,
 43        "known_categories": sorted(KNOWN_CATEGORIES),
 44        "profile_to_category": PROFILE_TO_CATEGORY,
 45        "ranking": [
 46            {
 47                "rank": r.rank,
 48                "permaslug": r.permaslug,
 49                "author": r.author,
 50                "short_id": r.short_id,
 51                "total_tokens": r.total_tokens,
 52                "ollama_id": r.ollama_id,
 53                "ollama_match_quality": r.ollama_match_quality,
 54                "has_ollama_match": bool(r.ollama_id),
 55                "score_intelligence": r.score_intelligence,
 56                "score_coding": r.score_coding,
 57                "score_agentic": r.score_agentic,
 58            }
 59            for r in ranking[:50]
 60        ],
 61        "total_models": len(ranking),
 62        "available_on_ollama": sum(1 for r in ranking if r.ollama_id),
 63        "with_benchmark_scores": sum(
 64            1 for r in ranking
 65            if r.score_intelligence or r.score_coding or r.score_agentic
 66        ),
 67    }
 68
 69
 70@router.get("/api/llm/picker")
 71async def api_llm_picker(profile: str = "synthesis") -> dict[str, Any]:
 72    """What model would the picker choose for ``profile`` right now?
 73
 74    Includes the rankings-based pick alongside the static PROFILE_MAP entry
 75    so the user can compare them.
 76    """
 77    from maf.llm.model_picker import PROFILE_MAP, PickContext, pick_model
 78    from maf.llm.openrouter_rankings import (
 79        DEFAULT_CATEGORY, PROFILE_TO_BENCHMARK, PROFILE_TO_CATEGORY,
 80        best_ollama_for_profile, get_cache,
 81    )
 82
 83    category = PROFILE_TO_CATEGORY.get(profile, DEFAULT_CATEGORY)
 84    benchmark = PROFILE_TO_BENCHMARK.get(profile, "")
 85    cache = get_cache()
 86    cached = cache.peek(category)
 87    rankings_pick, dimension = (
 88        best_ollama_for_profile(cached, profile) if cached else (None, "")
 89    )
 90    static_pick = PROFILE_MAP.get(profile, "")
 91    effective = pick_model(PickContext(profile=profile))
 92    return {
 93        "profile": profile,
 94        "category": category,
 95        "benchmark_dimension": benchmark,
 96        "static_map_pick": static_pick,
 97        "rankings_pick": rankings_pick,
 98        "rankings_picked_via": dimension,
 99        "effective_pick": effective,
100        "rankings_enabled": (
101            (rankings_pick is not None) and
102            (effective == rankings_pick)
103        ),
104    }
105
106
107@router.get("/llm", response_class=HTMLResponse)
108async def page_llm(request: Request) -> HTMLResponse:
109    """OpenRouter × Ollama rankings dashboard page."""
110    return templates.TemplateResponse("llm.html", {
111        "request": request,
112        "active_nav": "llm",
113    })