SPB Git forge

spb/ai-atlas

Public
41commits 1branches 0releases
4.6 MBsize
maindefault branch
12 days agolast push
HTML 77.2% TypeScript 10.5% Python 9.6% JavaScript 2.5%
43.0 KB · 596 lines python
Raw Blame History
1"""Intelligence surfaces (API 1.1) — deterministic, no LLM, no composite scores:2/frontier · /pareto · /pulse · /open · /find-a-model · /run-locally · /time-machine."""3from __future__ import annotations45import statistics6from collections import defaultdict7from datetime import UTC, datetime, timedelta8from datetime import time as dtime9from typing import Any1011from fastapi import APIRouter, Query, Request1213from aiatlas.api.common import (14    CLAIM_COLS,15    CLAIM_FROM,16    DOWNLOADABLE_CATEGORIES,17    ENTITY_COLS,18    ENTITY_FROM,19    EVENT_COLS,20    EVENT_FROM,21    OPEN_CATEGORIES,22    PRICE_COLS,23    PRICE_FROM,24    ApiError,25    cached,26    change_event,27    csv,28    deployment_row,29    entity_summary,30    num_expr,31    openness_values,32    parse_date,33    resolve_entity,34    resolve_id,35)36from aiatlas.db import connection, fetch_all, fetch_one, fetch_val37from aiatlas.ontology.licenses import LICENSES, normalize_license38from aiatlas.services import hardware_fit as hf39from aiatlas.services.finder import USE_CASES, find_models40from aiatlas.services.frontier import (41    FRONTIER_METHODOLOGY,42    all_primary_groups,43    benchmark_meta,44    frontier_model_ids,45    group_rows,46    group_summary,47    leader_at,48    leaderboard_rows,49    load_results,50    primary_group,51    rank_rows,52)53from aiatlas.services.pareto import pareto_frontier5455router = APIRouter(prefix="/api/v1", tags=["intelligence"])56PARAMS = num_expr("e.attributes->>'parameter_count'")57CONTEXT = num_expr("e.attributes->>'context_length'")58QUALITY_BENCHMARKS = ("artificial-analysis-intelligence-index", "gpqa")59PARETO_X = {"output_price": "cheapest current output price (USD / 1M tokens)", "input_price": "cheapest current input price (USD / 1M tokens)",60            "parameter_count": "total parameters", "context_length": "context window (tokens)", "memory_estimate": "ESTIMATED memory at 4-bit, 8K context (GB)"}616263def _f(v: Any) -> float | None:64    if v is None or isinstance(v, bool):65        return None66    try:67        return float(v)68    except (TypeError, ValueError):69        return None707172def price_delta(old: Any, new: Any) -> dict[str, Any] | None:73    """PRICE_CHANGED events carry `{input_per_mtok, output_per_mtok}` dicts (or a bare number). Returns % changes (output first) or None."""74    def pick(v: Any, k: str) -> float | None:75        return _f(v.get(k)) if isinstance(v, dict) else (_f(v) if k == "output_per_mtok" else None)7677    out: dict[str, Any] = {}78    for k, label in (("output_per_mtok", "output"), ("input_per_mtok", "input")):79        o, n = pick(old, k), pick(new, k)80        if o and n is not None:81            out[f"{label}_percent"] = round((n - o) / o * 100, 2)82            out[f"{label}_from"], out[f"{label}_to"] = o, n83    if not out:84        return None85    out["percent"] = out.get("output_percent", out.get("input_percent"))86    return out878889def _mods(attrs: dict[str, Any]) -> set[str]:90    out: set[str] = set()91    for k in ("modalities", "modalities_input", "modalities_output"):92        v = attrs.get(k)93        if isinstance(v, list):94            out |= {str(x).lower() for x in v}95    return {("image" if m == "vision" else "document" if m == "pdf" else m) for m in out}969798async def _cheapest_prices(conn: Any, model_ids: list[str] | None = None) -> dict[str, dict[str, Any]]:99    where = "p.valid_to is null" + (" and p.model_id = any(cast(:ids as text[]))" if model_ids is not None else "")100    rows = await fetch_all(conn, f"""select p.model_id, min(p.output_per_mtok) filter (where p.output_per_mtok > 0) as min_output, min(p.input_per_mtok) filter (where p.input_per_mtok > 0) as min_input,101                                            count(distinct p.provider_id) as providers, max(p.context_length) as max_ctx102                                     from prices p where {where} group by 1""", ids=model_ids)103    return {r["model_id"]: r for r in rows}104105106async def _cheapest_offer(conn: Any, model_id: str, field: str = "output") -> dict[str, Any] | None:107    col = "p.output_per_mtok" if field == "output" else "p.input_per_mtok"108    row = await fetch_one(conn, f"select {PRICE_COLS} from {PRICE_FROM} where p.model_id = :id and p.valid_to is null and {col} > 0 order by {col} asc limit 1", id=model_id)109    return deployment_row(row) if row else None110111112# ------------------------------------------------------------------------------------------------------------------ /frontier113114115@router.get("/frontier")116@cached(300)117async def frontier(request: Request, limit: int = Query(12, ge=1, le=50)) -> dict[str, Any]:118    now = datetime.now(UTC)119    async with connection() as conn:120        groups = await all_primary_groups(conn)121        fids, composition = await frontier_model_ids(conn)122        major = await fetch_all(conn, f"""123            select distinct on (e.id) {EVENT_COLS}, ev.occurred_at, ev.is_backfill from {EVENT_FROM} left join sources s on s.id = ev.source_id124            where ev.event_type in ('NEW_MODEL','RELEASE') and ev.importance >= 3 and e.entity_type = 'model' and e.merged_into is null and coalesce(s.tier, 2) <= 2125              and (ev.is_backfill = false or e.attributes->>'release_date' >= :since)126            order by e.id, ev.occurred_at desc""", since=(now - timedelta(days=60)).date().isoformat())127        major.sort(key=lambda r: (str((r.get("e_attributes") or {}).get("release_date") or ""), r["occurred_at"]), reverse=True)128        cheapest = await fetch_one(conn, f"select {PRICE_COLS} from {PRICE_FROM} where p.valid_to is null and p.output_per_mtok > 0 and p.model_id = any(cast(:ids as text[])) order by p.output_per_mtok asc limit 1", ids=sorted(fids))129        cheapest_1m = await fetch_one(conn, f"""select {PRICE_COLS} from {PRICE_FROM} where p.valid_to is null and p.output_per_mtok > 0 and p.model_id = any(cast(:ids as text[]))130                                                and coalesce(p.context_length, case when m.attributes->>'context_length' ~ '^[0-9]+$' then (m.attributes->>'context_length')::bigint end) >= 1000000131                                                order by p.output_per_mtok asc limit 1""", ids=sorted(fids))132        ctx_rows = await fetch_all(conn, f"select {ENTITY_COLS}, {CONTEXT} as ctx from {ENTITY_FROM} where e.entity_type = 'model' and e.merged_into is null and {CONTEXT} is not null order by {CONTEXT} desc, e.canonical_name limit :lim", lim=limit)133        open_rows = await fetch_all(conn, f"select {ENTITY_COLS} from {ENTITY_FROM} where e.entity_type = 'model' and e.merged_into is null and e.attributes->>'openness' = any(cast(:o as text[]))", o=list(OPEN_CATEGORIES))134        movements = await fetch_all(conn, f"""select {EVENT_COLS}, ev.occurred_at from {EVENT_FROM} where ev.is_backfill = false and ev.occurred_at > now() - interval '30 days'135                                              and (ev.event_type in ('BENCHMARK_UPDATED','BENCHMARK_LEADER_CHANGED','NEW_BENCHMARK_LEADER') or ev.event_type = 'PRICE_CHANGED')136                                              order by ev.occurred_at desc limit 500""")137        all_prices = await _cheapest_prices(conn)138    # ranks per model across primary groups139    ranks: dict[str, dict[str, int]] = defaultdict(dict)140    for bid, g in groups.items():141        for r in rank_rows(g["rows"], g["higher_is_better"]):142            ranks[r["model_id"]][g["benchmark"]["slug"]] = r["rank"]143    bench_frontier = []144    agentic = []145    for bid, g in sorted(groups.items(), key=lambda kv: -kv[1]["n"]):146        lb = leaderboard_rows(g)147        if g["n"] >= 20 and lb:148            leader, second = lb[0], (lb[1] if len(lb) > 1 else None)149            bench_frontier.append({"benchmark": {k: g["benchmark"].get(k) for k in ("id", "slug", "name", "category")}, "group": group_summary(g), "leader": leader, "second": second,150                                   "gap": round(leader["score"] - second["score"], 3) if second else None})151        if (g["benchmark"].get("category") or "").lower() == "agentic" and lb:152            agentic.append({"benchmark": {k: g["benchmark"].get(k) for k in ("id", "slug", "name")}, "group": group_summary(g), "leaders": lb[:5]})153    open_frontier = []154    for r in open_rows:155        attrs = r["attributes"] or {}156        rk = ranks.get(r["id"], {})157        best = min(rk.values()) if rk else None158        open_frontier.append({"model": entity_summary(r), "best_rank": best, "best_rank_on": min(rk, key=rk.get) if rk else None, "parameter_count": _f(attrs.get("parameter_count")),159                              "context_length": _f(attrs.get("context_length")), "ranks": dict(sorted(rk.items())), "_k": (best if best is not None else 10_000, -(_f(attrs.get("parameter_count")) or 0))})160    open_frontier.sort(key=lambda x: x["_k"])161    for x in open_frontier:162        x.pop("_k")163    # efficiency frontier: quality (index or GPQA primary group) vs cheapest output price164    quality_group = None165    for slug in QUALITY_BENCHMARKS:166        quality_group = next((g for g in groups.values() if g["benchmark"]["slug"] == slug), None)167        if quality_group:168            break169    eff_points = []170    if quality_group:171        for r in rank_rows(quality_group["rows"], quality_group["higher_is_better"]):172            pr = all_prices.get(r["model_id"])173            if pr and pr["min_output"] is not None:174                eff_points.append({"id": r["model_id"], "model": {"id": r["model_id"], "slug": r["model_slug"], "name": r["model_name"]}, "x": float(pr["min_output"]), "y": float(r["score"]),175                                   "rank": r["rank"], "trust_level": r["trust_level"]})176    eff_front = set(pareto_frontier(eff_points, maximize_y=quality_group["higher_is_better"] if quality_group else True))177    multimodal = []178    for mid, rk in ranks.items():179        if min(rk.values()) > 10:180            continue181        row = next((r for g in groups.values() for r in g["rows"] if r["model_id"] == mid), None)182        if not row:183            continue184        mods = _mods(row.get("model_attrs") or {})185        if len(mods) >= 3:186            multimodal.append({"model": {"id": mid, "slug": row["model_slug"], "name": row["model_name"]}, "modalities": sorted(mods), "top10_on": sorted(b for b, k in rk.items() if k <= 10)})187    moves = []188    for r in movements:189        ev = change_event(r)190        if ev["event_type"] == "PRICE_CHANGED":191            d = price_delta(ev.get("old_value"), ev.get("new_value"))192            if d and d["percent"] is not None and abs(d["percent"]) >= 20:193                ev["percent_change"] = d["percent"]194                ev["price_delta"] = d195                ev["provider"] = (ev.get("meta") or {}).get("provider")196                moves.append(ev)197        else:198            moves.append(ev)199    return {200        "latest_major_models": [change_event(r) | {"occurred_at": r["occurred_at"], "is_backfill": r["is_backfill"]} for r in major[:limit]],201        "benchmark_frontier": bench_frontier,202        "price_frontier": {"cheapest_output": _deploy_or_none(cheapest), "cheapest_output_1m_context": _deploy_or_none(cheapest_1m), "frontier_models": len(fids), "composition": composition},203        "context_frontier": [{"model": entity_summary(r), "context_length": r["ctx"]} for r in ctx_rows],204        "open_weight_frontier": {"items": open_frontier[:limit], "dimensions": ["best_rank", "parameter_count", "context_length"], "note": "sorted by best benchmark rank then parameters; no composite"},205        "efficiency_frontier": {"quality": {"benchmark": quality_group["benchmark"]["slug"], "group": group_summary(quality_group)} if quality_group else None, "x": "cheapest current output price (USD / 1M tokens)",206                                "points": [{**p, "pareto": p["id"] in eff_front} for p in sorted(eff_points, key=lambda p: p["x"])], "frontier": sorted(eff_front)},207        "agentic_frontier": agentic,208        "multimodal_frontier": sorted(multimodal, key=lambda x: (-len(x["top10_on"]), x["model"]["name"] or ""))[:limit],209        "recent_frontier_movements": moves[:50],210        "generated_at": now,211        "methodology": FRONTIER_METHODOLOGY + " benchmark_frontier lists the primary comparability group of every benchmark with ≥ 20 current results; efficiency_frontier is the "212                       "Pareto set (maximise quality score, minimise cheapest current output price); recent movements are non-backfill benchmark events and price moves ≥ 20% in 30 days. "213                       "Nothing here is a composite ranking.",214    }215216217def _deploy_or_none(row: dict[str, Any] | None) -> dict[str, Any] | None:218    return deployment_row(row) if row else None219220221# ------------------------------------------------------------------------------------------------------------------ /pareto222223224@router.get("/pareto")225@cached(300)226async def pareto(request: Request, benchmark: str = Query(...), x: str = Query("output_price"), y: str = Query("score"), metric: str | None = None, config_key: str | None = None,227                 org: str | None = None, family: str | None = None, openness: str | None = None) -> dict[str, Any]:228    if x == "latency":229        raise ApiError(400, "x=latency is not available: AI Atlas does not store latency measurements (nothing is estimated for it)")230    if x not in PARETO_X:231        raise ApiError(400, f"x must be one of {', '.join(PARETO_X)}")232    if y != "score":233        raise ApiError(400, "y must be 'score'")234    async with connection() as conn:235        bench = await resolve_entity(conn, benchmark, ("benchmark",), aliases=True)236        rows = await load_results(conn, benchmark_ids=[bench["id"]])237        groups = list(group_rows(rows).values())238        g = None239        if metric or config_key:240            cands = [gg for gg in groups if (not metric or gg["metric"] == metric.lower()) and (not config_key or gg["config_key"] == config_key)]241            g = max(cands, key=lambda gg: gg["model_count"]) if cands else None242        else:243            g = primary_group(bench.get("attributes"), groups)244        if not g:245            return {"benchmark": entity_summary(bench), "points": [], "frontier": [], "groups": [group_summary(x) for x in groups], "note": "no current results in the requested group"}246        ranked = rank_rows(g["rows"], g["higher_is_better"])247        ids = [r["model_id"] for r in ranked]248        prices = await _cheapest_prices(conn, ids) if x in ("output_price", "input_price") else {}249        offers: dict[str, dict[str, Any]] = {}250        if x in ("output_price", "input_price"):251            col = "p.output_per_mtok" if x == "output_price" else "p.input_per_mtok"252            for pr in await fetch_all(conn, f"select distinct on (p.model_id) {PRICE_COLS} from {PRICE_FROM} where p.valid_to is null and {col} > 0 and p.model_id = any(cast(:ids as text[])) order by p.model_id, {col} asc", ids=ids):253                offers[pr["m_id"]] = deployment_row(pr)254        org_id = await resolve_id(conn, org) if org else None255        fam_ids: set[str] | None = None256        if family:257            fr = await fetch_all(conn, "select e.id from entities e left join entities f on f.id = e.family_id where e.entity_type = 'model' and (f.slug = :f or f.id = :f or e.attributes->>'family' ilike :f)", f=family)258            fam_ids = {r["id"] for r in fr}259    open_vals = set(openness_values(openness)) if openness else None260    points = []261    for r in ranked:262        attrs = r.get("model_attrs") or {}263        if org_id and r.get("organization_id") != org_id:264            continue265        if fam_ids is not None and r["model_id"] not in fam_ids:266            continue267        if open_vals and str(attrs.get("openness") or "") not in open_vals:268            continue269        xv: float | None270        provider = None271        if x == "output_price":272            pr = prices.get(r["model_id"])273            xv = _f(pr["min_output"]) if pr else None274            provider = (offers.get(r["model_id"]) or {}).get("provider")275        elif x == "input_price":276            pr = prices.get(r["model_id"])277            xv = _f(pr["min_input"]) if pr else None278            provider = (offers.get(r["model_id"]) or {}).get("provider")279        elif x == "parameter_count":280            xv = _f(attrs.get("parameter_count"))281        elif x == "context_length":282            xv = _f(attrs.get("context_length"))283        else:284            pc = hf.parameter_count(attrs)285            xv = hf.estimate_memory_gb(pc, "4bit", 8192) if pc else None286        if xv is None:287            continue288        points.append({"id": r["model_id"], "model": {"id": r["model_id"], "slug": r["model_slug"], "name": r["model_name"], "organization": r.get("org_name"), "openness": attrs.get("openness")},289                       "x": xv, "y": float(r["score"]), "rank": r["rank"], "trust_level": r["trust_level"], "config": {k: v for k, v in (r.get("config") or {}).items() if k in ("reasoning_effort", "reasoning", "variant", "evaluator")},290                       "context_length": _f(attrs.get("context_length")), "parameter_count": _f(attrs.get("parameter_count")), "release_date": attrs.get("release_date"),291                       **({"provider": provider} if provider else {}), **({"estimated": True} if x == "memory_estimate" else {})})292    front = pareto_frontier(points, maximize_y=g["higher_is_better"])293    fset = set(front)294    for p in points:295        p["pareto"] = p["id"] in fset296    return {"benchmark": entity_summary(bench), "group": group_summary(g), "groups": [group_summary(gg) for gg in groups], "x": {"key": x, "label": PARETO_X[x]}, "y": {"key": "score", "label": f"{g['metric']} on {bench['canonical_name']}"},297            "points": sorted(points, key=lambda p: p["x"]), "frontier": front,298            "methodology": f"Points are the best current row per canonical model in comparability group '{g['label']}'. Price = cheapest current offer across providers "299                           f"(the provider shown). Pareto frontier maximises the score and minimises x; exact ties are all kept. memory_estimate is an estimate (see /methodology)."}300301302# ------------------------------------------------------------------------------------------------------------------ /pulse303304305@router.get("/pulse")306@cached(120)307async def pulse(request: Request, days: int = Query(7, ge=1, le=90)) -> dict[str, Any]:308    now = datetime.now(UTC)309    since = now - timedelta(days=days)310    async with connection() as conn:311        c = await fetch_one(conn, f"""312            with ev as (select ev.*, e.entity_type as et, e.attributes as attrs from change_events ev left join entities e on e.id = ev.entity_id313                        where ev.is_backfill = false and ev.occurred_at > :since)314            select (select count(distinct entity_id) from ev where event_type = 'NEW_MODEL' and et = 'model') as new_models,315                   (select count(distinct entity_id) from ev where event_type = 'NEW_MODEL' and et = 'model' and attrs->>'openness' in ('open-weights','open-source')) as new_open_models,316                   (select count(*) from entities where entity_type = 'artifact' and merged_into is null and first_seen_at > :since) as new_artifacts,317                   (select count(distinct entity_id) from ev where event_type = 'NEW_PAPER') as new_papers,318                   (select count(*) from ev where event_type = 'PROVIDER_LISTED') as provider_listings,319                   (select count(*) from ev where event_type = 'PROVIDER_DELISTED') as provider_delistings,320                   (select count(*) from ev where event_type = 'PRICE_CHANGED') as price_changes,321                   (select count(distinct entity_id) from ev where event_type = 'NEW_MODEL' and et = 'model' and {num_expr("attrs->>'context_length'")} >= 1000000) as new_models_1m_context,322                   (select count(*) from ev where event_type = 'DOCUMENT_CHANGED') as documents_changed,323                   (select count(distinct d.source_id) from snapshots s join documents d on d.id = s.document_id where s.observed_at > :since) as sources_observed,324                   (select count(*) from ev where event_type <> 'DOCUMENT_CHANGED') as events_total""", since=since)325        pct = await fetch_all(conn, f"select {EVENT_COLS}, ev.occurred_at from {EVENT_FROM} where ev.is_backfill = false and ev.occurred_at > :since and ev.event_type = 'PRICE_CHANGED' "326                                    f"order by ev.occurred_at desc limit 500", since=since)327        ctx_models = await fetch_all(conn, f"""select distinct on (e.id) {ENTITY_COLS} from {EVENT_FROM} where ev.is_backfill = false and ev.occurred_at > :since and ev.event_type = 'NEW_MODEL'328                                               and e.entity_type = 'model' and e.merged_into is null and {CONTEXT} >= 1000000 order by e.id limit 100""", since=since)329        rows = await load_results(conn, current_only=False)330        meta = await benchmark_meta(conn)331    moves: list[float] = []332    price_items: list[dict[str, Any]] = []333    for r in pct:334        ev = change_event(r)335        d = price_delta(ev.get("old_value"), ev.get("new_value"))336        if d and d["percent"] is not None:337            moves.append(float(d["percent"]))338        price_items.append({"id": ev["id"], "summary": ev["summary"], "model": ev["entity"], "provider": (ev.get("meta") or {}).get("provider"), "provider_id": (ev.get("meta") or {}).get("provider_id"),339                            "occurred_at": r.get("occurred_at"), "percent_change": d["percent"] if d else None, "delta": d, "source_url": ev.get("source_url")})340    by_bench: dict[str, list[dict[str, Any]]] = defaultdict(list)341    for r in rows:342        by_bench[r["benchmark_id"]].append(r)343    new_leaders = []344    for bid, brows in by_bench.items():345        m = meta.get(bid, {})346        la, lb = leader_at(brows, since, m.get("attributes")), leader_at(brows, now, m.get("attributes"))347        if lb and (la is None or la["model"]["id"] != lb["model"]["id"]):348            new_leaders.append({"benchmark": {"id": bid, "slug": m.get("slug"), "name": m.get("name")}, "previous": la, "current": lb})349    c = c or {}350    counters = {351        "new_models": {"value": int(c.get("new_models") or 0), "definition": "canonical models with a NEW_MODEL event that occurred in the window (not back-filled)"},352        "new_open_weight_models": {"value": int(c.get("new_open_models") or 0), "definition": "subset of new_models with openness open-weights / open-source"},353        "new_artifacts": {"value": int(c.get("new_artifacts") or 0), "definition": "artifact entities (checkpoints, quantisations, conversions) first seen in the window"},354        "new_papers": {"value": int(c.get("new_papers") or 0), "definition": "papers with a NEW_PAPER event that occurred in the window"},355        "provider_listings": {"value": int(c.get("provider_listings") or 0), "definition": "PROVIDER_LISTED events in the window"},356        "provider_delistings": {"value": int(c.get("provider_delistings") or 0), "definition": "PROVIDER_DELISTED events in the window"},357        "price_changes": {"value": int(c.get("price_changes") or 0), "median_percent": round(statistics.median(moves), 2) if moves else None, "items": price_items[:50],358                          "definition": "PRICE_CHANGED events in the window; % change = output price (else input) new vs old from the event's old/new values; median over events with both values"},359        "new_models_1m_context": {"value": int(c.get("new_models_1m_context") or 0), "items": [entity_summary(m) for m in ctx_models],360                                  "definition": "new_models whose context_length is at least 1 000 000 tokens"},361        "new_benchmark_leaders": {"value": len(new_leaders), "items": new_leaders, "definition": "benchmarks whose primary-group leader (computed from results observed by each date) changed over the window"},362        "documents_changed": {"value": int(c.get("documents_changed") or 0), "definition": "DOCUMENT_CHANGED events in the window"},363        "sources_observed": {"value": int(c.get("sources_observed") or 0), "definition": "distinct sources with at least one snapshot taken in the window"},364        "events_total": {"value": int(c.get("events_total") or 0), "definition": "all non-backfill events (excluding source-document changes) that occurred in the window"},365    }366    return {"days": days, "since": since, "until": now, "counters": counters,367            "note": "Deterministic counters over events that OCCURRED in the window and are not back-filled history; each counter carries its own definition."}368369370# ------------------------------------------------------------------------------------------------------------------ /open371372373@router.get("/open")374@cached(300)375async def open_models(request: Request, sort: str = Query("release", pattern="^(release|params|context|rank|name|downloads)$"), license: str | None = None,376                      min_params: float | None = Query(None, ge=0), max_params: float | None = Query(None, ge=0), min_context: int | None = Query(None, ge=0),377                      modality: str | None = None, days: int | None = Query(None, ge=1, le=3650), limit: int = Query(50, ge=1, le=200), offset: int = Query(0, ge=0),378                      openness: str | None = None) -> dict[str, Any]:379    """Open-weight / open-source / restricted-weights canonical models with licence permissions, observed dimensions, best results, estimated hardware fit."""380    where = ["e.entity_type = 'model'", "e.merged_into is null", "e.attributes->>'openness' = any(cast(:o as text[]))"]381    params: dict[str, Any] = {"o": openness_values(openness) if openness else list(DOWNLOADABLE_CATEGORIES)}382    if license:383        from aiatlas.api.routers.models import license_match_sql, license_params384385        where.append(license_match_sql())386        params.update(license_params(license))387    if min_params is not None:388        where.append(f"{PARAMS} >= :minp")389        params["minp"] = float(min_params)390    if max_params is not None:391        where.append(f"{PARAMS} <= :maxp")392        params["maxp"] = float(max_params)393    if min_context is not None:394        where.append(f"{CONTEXT} >= :minc")395        params["minc"] = float(min_context)396    if modality:397        where.append("(e.attributes->'modalities' ? :mod or e.attributes->'modalities_input' ? :mod or e.attributes->'modalities_output' ? :mod)")398        params["mod"] = modality399    if days:400        where.append("(e.attributes->>'release_date' >= :since or e.first_seen_at > now() - make_interval(days => :days))")401        params["since"] = (datetime.now(UTC) - timedelta(days=days)).date().isoformat()402        params["days"] = days403    order = {"release": "e.attributes->>'release_date' desc nulls last", "params": f"{PARAMS} desc nulls last", "context": f"{CONTEXT} desc nulls last", "name": "e.canonical_name asc",404             "downloads": num_expr("e.attributes->>'metric.downloads'") + " desc nulls last", "rank": "e.canonical_name asc"}[sort]405    where_sql = " and ".join(where)406    async with connection() as conn:407        rows = await fetch_all(conn, f"select {ENTITY_COLS}, e.family_id from {ENTITY_FROM} where {where_sql} order by {order}, e.id limit :lim offset :off", lim=limit if sort != "rank" else 2000, off=0 if sort == "rank" else offset, **params)408        total = await fetch_val(conn, f"select count(*) from {ENTITY_FROM} where {where_sql}", **params)409        groups = await all_primary_groups(conn)410        prices = await _cheapest_prices(conn, [r["id"] for r in rows])411        summary = await fetch_one(conn, """select jsonb_object_agg(k, n) as by_cat from (select attributes->>'openness' as k, count(*) as n from entities412                                            where entity_type = 'model' and merged_into is null and attributes->>'openness' in ('open-weights','open-source','restricted-weights','restricted') group by 1) x""")413        lic_rows = await fetch_all(conn, """select coalesce(attributes->>'license_key', attributes->>'license') as raw, count(*) as n from entities where entity_type = 'model' and merged_into is null414                                            and attributes->>'openness' in ('open-weights','open-source','restricted-weights','restricted') and (attributes ? 'license' or attributes ? 'license_key') group by 1""")415        # released in the last 30 days (release_date when known; first_seen_at only for models without any release date)416        new_30d = await fetch_val(conn, """select count(*) from entities where entity_type = 'model' and merged_into is null and attributes->>'openness' in ('open-weights','open-source','restricted-weights','restricted')417                                           and case when attributes->>'release_date' ~ '^\\d{4}' then left(attributes->>'release_date', 10) >= to_char((now() at time zone 'UTC') - interval '30 days', 'YYYY-MM-DD')418                                                    else first_seen_at > now() - interval '30 days' end""")419    ranks: dict[str, dict[str, int]] = defaultdict(dict)420    for g in groups.values():421        for r in rank_rows(g["rows"], g["higher_is_better"]):422            ranks[r["model_id"]][g["benchmark"]["slug"]] = r["rank"]423    items = []424    for r in rows:425        attrs = r["attributes"] or {}426        key = attrs.get("license_key") or normalize_license(attrs.get("license"))427        info = LICENSES.get(key) if key else None428        rk = ranks.get(r["id"], {})429        best = sorted(rk.items(), key=lambda kv: kv[1])[:3]430        pr = prices.get(r["id"])431        items.append({"model": entity_summary(r), "licence": {**info.as_dict(), "raw": attrs.get("license")} if info else {"key": None, "raw": attrs.get("license"), "note": "not classified"},432                      "dimensions": {"parameter_count": _f(attrs.get("parameter_count")), "active_parameter_count": _f(attrs.get("active_parameter_count")), "context_length": _f(attrs.get("context_length")),433                                     "modalities": sorted(_mods(attrs)), "release_date": attrs.get("release_date"), "openness": attrs.get("openness"), "downloads": _f(attrs.get("metric.downloads"))},434                      "best_results": [{"benchmark": b, "rank": k} for b, k in best], "best_rank": best[0][1] if best else None,435                      "hardware_fit": {"4bit_64gb": hf.fit_detailed(attrs, 64, quant="4bit", context=8192), "8bit_128gb": hf.fit_detailed(attrs, 128, quant="8bit", context=8192), "estimated": True},436                      "providers": int(pr["providers"]) if pr else 0, "cheapest_output_per_mtok": _f(pr["min_output"]) if pr else None})437    if sort == "rank":438        items.sort(key=lambda x: (x["best_rank"] if x["best_rank"] is not None else 10_000, x["model"]["name"] or ""))439        items = items[offset:offset + limit]440    lic_counts: dict[str, int] = defaultdict(int)441    for lr in lic_rows:442        k = lr["raw"] if lr["raw"] in LICENSES else normalize_license(lr["raw"])443        lic_counts[k or f"raw:{lr['raw']}"] += int(lr["n"])444    return {"items": items, "total": int(total or 0), "limit": limit, "offset": offset,445            "summary": {"by_category": (summary or {}).get("by_cat") or {}, "by_license_top": [{"key": k, "label": LICENSES[k].label if k in LICENSES else k, "models": n} for k, n in sorted(lic_counts.items(), key=lambda kv: -kv[1])[:12]],446                        "new_30d": int(new_30d or 0), "new_30d_definition": "release_date within the last 30 days (first_seen_at only when no release date is known)"},447            "note": "Universe = canonical models whose weights can be downloaded (open-weights, open-source, restricted-weights). hardware_fit values are ESTIMATES (see /methodology); "448                    "best_results are ranks inside each benchmark's primary comparability group — no composite score."}449450451# ------------------------------------------------------------------------------------------------------------------ /find-a-model452453454@router.get("/find-a-model")455@cached(300)456async def find_a_model(request: Request, use_case: str | None = Query(None), deployment: str = Query("any", pattern="^(local|api|any)$"), memory_gb: float | None = Query(None, gt=0, le=100000),457                       quant: str = Query("4bit"), context_min: int | None = Query(None, ge=0), license: str = Query("any", pattern="^(commercial|any)$"), openness: str | None = None,458                       max_input_price: float | None = Query(None, ge=0), max_output_price: float | None = Query(None, ge=0), modalities: str | None = None,459                       limit: int = Query(30, ge=1, le=100)) -> dict[str, Any]:460    if use_case and use_case not in USE_CASES:461        raise ApiError(400, f"use_case must be one of {', '.join(USE_CASES)}")462    async with connection() as conn:463        out = await find_models(conn, use_case=use_case, deployment=deployment, memory_gb=memory_gb, quant=quant, context_min=context_min, license=license,464                                openness=openness_values(openness) if openness else None, max_input_price=max_input_price, max_output_price=max_output_price,465                                modalities=csv(modalities), limit=limit)466        if deployment != "local":467            ids = [m["model"]["id"] for m in out["matches"]]468            rows = await fetch_all(conn, f"""select * from (select {PRICE_COLS}, row_number() over (partition by p.model_id order by p.output_per_mtok asc nulls last) as rn469                                                          from {PRICE_FROM} where p.valid_to is null and p.model_id = any(cast(:ids as text[]))) x where rn <= 5""", ids=ids)470            by: dict[str, list[dict[str, Any]]] = defaultdict(list)471            for r in rows:472                by[r["m_id"]].append(deployment_row(r))473            for m in out["matches"]:474                m["deployments"] = by.get(m["model"]["id"], [])475    return out476477478# ------------------------------------------------------------------------------------------------------------------ /run-locally479480481@router.get("/run-locally")482@cached(300)483async def run_locally(request: Request, memory_gb: float = Query(..., gt=0, le=100000), gpu_count: int = Query(1, ge=1, le=8), quant: str = Query("4bit"), context: int = Query(8192, ge=0, le=10_000_000),484                      batch: int = Query(1, ge=1, le=256), platform: str = Query("any", pattern="^(apple|nvidia|amd|any)$"), use_case: str | None = None,485                      limit: int = Query(60, ge=1, le=300), openness: str | None = None) -> dict[str, Any]:486    """Canonical models (and their compatible artifacts) whose ESTIMATED footprint fits `memory_gb × gpu_count`."""487    if gpu_count not in (1, 2, 4, 8):488        raise ApiError(400, "gpu_count must be 1, 2, 4 or 8")489    q = hf.normalize_quant(quant)490    fmt_pref = {"apple": ("mlx", "gguf"), "nvidia": ("gguf", "awq", "gptq", "fp8", "nvfp4", "int4", "int8"), "amd": ("gguf", "mxfp4", "int8"), "any": ()}[platform]491    where = ["e.entity_type = 'model'", "e.merged_into is null", "e.attributes ? 'parameter_count'", "e.attributes->>'openness' = any(cast(:o as text[]))"]492    params: dict[str, Any] = {"o": openness_values(openness) if openness else list(DOWNLOADABLE_CATEGORIES)}493    async with connection() as conn:494        rows = await fetch_all(conn, f"select {ENTITY_COLS} from {ENTITY_FROM} where {' and '.join(where)} order by {PARAMS} desc nulls last limit 3000", **params)495        arts = await fetch_all(conn, f"""496            select a.canonical_id as model_id, {ENTITY_COLS} from entities e join lateral (select e.canonical_id) a on true left join entities eo on eo.id = e.organization_id497            where e.entity_type = 'artifact' and e.merged_into is null and e.canonical_id is not null498            union all499            select r.object_id as model_id, {ENTITY_COLS} from relations r join entities e on e.id = r.subject_id left join entities eo on eo.id = e.organization_id500            where r.predicate = 'quantized_from' and r.valid_to is null and e.merged_into is null and (e.attributes->>'is_quantized' = 'true' or e.attributes ? 'quant_format')""")501        groups = await all_primary_groups(conn) if use_case else {}502    cat_models: set[str] | None = None503    if use_case:504        cat = {"coding": "coding", "reasoning": "reasoning", "agentic": "agentic", "vision": "multimodal", "math": "math"}.get(use_case, use_case)505        cat_models = {r["model_id"] for g in groups.values() if (g["benchmark"].get("category") or "").lower() == cat for r in g["rows"]}506    art_by: dict[str, list[dict[str, Any]]] = defaultdict(list)507    for a in arts:508        art_by[a["model_id"]].append(a)509    items = []510    for r in rows:511        if cat_models is not None and r["id"] not in cat_models:512            continue513        attrs = r["attributes"] or {}514        f = hf.fit_detailed(attrs, memory_gb, quant=q, context=context, batch=batch, gpu_count=gpu_count)515        if not f:516            continue517        compatible = []518        for a in art_by.get(r["id"], []):519            aa = a["attributes"] or {}520            fmt = str(aa.get("quant_format") or "").lower()521            if fmt_pref and fmt and fmt not in fmt_pref:522                continue523            size = hf.file_size_gb(aa)524            af = hf.fit_detailed({**attrs, **{k: v for k, v in aa.items() if k in ("num_hidden_layers", "num_key_value_heads", "num_attention_heads", "head_dim", "hidden_size")}}, memory_gb,525                                 quant=q, context=context, batch=batch, gpu_count=gpu_count, observed_size_gb=size)526            compatible.append({"artifact": entity_summary(a), "quant_format": fmt or None, "file_size_gb": size, "weights_source": "observed" if size else "estimated", "fit": af})527        compatible.sort(key=lambda x: (not (x["fit"] or {}).get("fits", False), x["file_size_gb"] or 1e9))528        items.append({"model": entity_summary(r), "fit": f, "artifacts": compatible[:8], "artifact_count": len(art_by.get(r["id"], []))})529    fits = [i for i in items if i["fit"]["fits"]]530    fits.sort(key=lambda i: -(i["fit"].get("parameter_count") or 0))531    return {"inputs": {"memory_gb": memory_gb, "gpu_count": gpu_count, "total_memory_gb": memory_gb * gpu_count, "quant": q, "context": context, "batch": batch, "platform": platform, "use_case": use_case},532            "estimated": True, "assumptions": hf.ASSUMPTIONS, "counts": {"fits": len(fits), "evaluated": len(items)}, "items": fits[:limit],533            "note": "Every figure is an ESTIMATE: weights = params × bytes/param (× 1.15 overhead) unless an artifact's observed file size is available; KV cache uses architecture metadata when known, "534                    + ("else 0.5 GB per 8K tokens × batch. Multi-GPU sums device memory and ignores interconnect." if gpu_count > 1 else "else 0.5 GB per 8K tokens × batch.")}535536537# ------------------------------------------------------------------------------------------------------------------ /time-machine538539540@router.get("/time-machine")541@cached(300)542async def time_machine(request: Request, date: str = Query(..., description="YYYY-MM-DD"), scope: str = Query("models", pattern="^(models|prices|benchmarks|hardware|all)$"),543                       limit: int = Query(50, ge=1, le=300)) -> dict[str, Any]:544    d = parse_date(date, "date")545    assert d is not None546    at = datetime.combine(d, dtime.max, UTC)547    out: dict[str, Any] = {"date": d.isoformat(), "scope": scope}548    async with connection() as conn:549        first = await fetch_val(conn, "select min(first_seen_at) from entities")550        reconstructed = first is not None and at < first551        out["first_entity_at"] = first552        out["reconstructed"] = bool(reconstructed)553        out["note"] = (f"AI Atlas observation history starts at {first.isoformat() if first else 'n/a'}. " +554                       ("This date is earlier: the state is RECONSTRUCTED from claims with effective dates and from release dates — not from direct observation." if reconstructed555                        else "The state is taken from claims, prices and results as they were known at the end of that UTC day."))556        if scope in ("models", "all"):557            rows = await fetch_all(conn, f"""select {ENTITY_COLS}, (e.first_seen_at <= :at) as observed_then from {ENTITY_FROM} where e.entity_type = 'model' and e.merged_into is null558                                             and (e.first_seen_at <= :at or (e.attributes->>'release_date' ~ '^\\d{{4}}' and left(e.attributes->>'release_date', 10) <= :d))559                                             order by e.attributes->>'release_date' desc nulls last, e.canonical_name limit :lim""", at=at, d=d.isoformat(), lim=limit)560            ids = [r["id"] for r in rows]561            claims = await fetch_all(conn, f"""select distinct on (c.entity_id, c.property) c.entity_id, {CLAIM_COLS} from {CLAIM_FROM}562                                               where c.entity_id = any(cast(:ids as text[])) and c.status <> 'retracted' and coalesce(c.effective_at, c.valid_from) <= :at and (c.valid_to is null or c.valid_to > :at)563                                               order by c.entity_id, c.property, c.tier, c.valid_from desc""", ids=ids, at=at) if ids else []564            by: dict[str, dict[str, Any]] = defaultdict(dict)565            for c in claims:566                by[c["entity_id"]][c["property"]] = c["value"]567            total = await fetch_val(conn, """select count(*) from entities e where e.entity_type = 'model' and e.merged_into is null568                                              and (e.first_seen_at <= :at or (e.attributes->>'release_date' ~ '^\\d{4}' and left(e.attributes->>'release_date', 10) <= :d))""", at=at, d=d.isoformat())569            out["models"] = {"items": [{"model": entity_summary(r), "attributes_as_of": by.get(r["id"], {}), "observed_then": bool(r["observed_then"]),570                                        "reconstructed": not r["observed_then"]} for r in rows], "total": int(total or 0), "limit": limit}571        if scope in ("prices", "all"):572            prs = await fetch_all(conn, f"select {PRICE_COLS} from {PRICE_FROM} where p.valid_from <= :at and (p.valid_to is null or p.valid_to > :at) order by p.output_per_mtok nulls last limit :lim", at=at, lim=limit)573            n = await fetch_val(conn, "select count(*) from prices p where p.valid_from <= :at and (p.valid_to is null or p.valid_to > :at)", at=at)574            out["prices"] = {"items": [deployment_row(p) for p in prs], "total": int(n or 0), "note": "offers whose validity interval covers the date (price rows are append-only)"}575        if scope in ("benchmarks", "all"):576            rows_r = await load_results(conn, current_only=False)577            meta = await benchmark_meta(conn)578            by_b: dict[str, list[dict[str, Any]]] = defaultdict(list)579            for r in rows_r:580                by_b[r["benchmark_id"]].append(r)581            leaders = []582            for bid, brows in by_b.items():583                la = leader_at(brows, at, meta.get(bid, {}).get("attributes"))584                if la:585                    leaders.append({"benchmark": {"id": bid, "slug": meta.get(bid, {}).get("slug"), "name": meta.get(bid, {}).get("name")}, "leader": la})586            out["benchmarks"] = {"leaders": sorted(leaders, key=lambda x: x["benchmark"]["name"] or ""), "note": "leaders from results observed by the date (evaluation dates are not used: a result is known only once observed)"}587        if scope in ("hardware", "all"):588            hw = await fetch_all(conn, f"""select {ENTITY_COLS}, (e.first_seen_at <= :at) as observed_then from {ENTITY_FROM} where e.entity_type = 'hardware' and e.merged_into is null589                                           and (e.first_seen_at <= :at or (e.attributes->>'release_date' ~ '^\\d{{4}}' and left(e.attributes->>'release_date', 7) <= :d))590                                           order by e.attributes->>'release_date' desc nulls last limit :lim""", at=at, d=d.isoformat()[:7], lim=limit)591            out["hardware"] = {"items": [{"hardware": entity_summary(r), "reconstructed": not r["observed_then"]} for r in hw]}592    return out593594595__all__ = ["PARETO_X", "router"]596