HTML 77.2%
TypeScript 10.5%
Python 9.6%
JavaScript 2.5%
1"""`EntityDetail` builder (docs/API.md): shared by `/entities/{slug}` and the type-scoped aliases.23API 1.1: blocks run in at most FOUR concurrent groups (one pooled connection each, sequential inside a group) instead of one connection per4block; model details gain `family`, `artifacts`, `deployments`, `identity`, `licence`, `openness`, `version_history` and grouped `benchmarks`;5artifact details carry `canonical` + `artifact_kind`; a resolved `merged_into` hop is reported as `redirected_from`."""6from __future__ import annotations78import asyncio9from collections import defaultdict10from typing import Any1112from sqlalchemy.ext.asyncio import AsyncConnection1314from aiatlas.api.common import (15 ARTIFACT_KINDS,16 CLAIM_COLS,17 CLAIM_FROM,18 COMPANY_TYPES,19 ENTITY_COLS,20 ENTITY_FROM,21 EVENT_COLS,22 EVENT_FROM,23 PRICE_COLS,24 PRICE_FROM,25 RESULT_COLS,26 RESULT_FROM,27 RESULT_ORDER,28 change_event,29 deployment_row,30 enrich_provenance,31 entity_summary,32 price_row,33 result_row,34)35from aiatlas.db import connection, fetch_all, fetch_one36from aiatlas.ontology.benchmarks import TRUST_LABELS37from aiatlas.ontology.licenses import LICENSES, normalize_license38from aiatlas.ontology.openness import OPENNESS_DEFINITIONS, OPENNESS_LABELS, normalize_openness, openness_dimensions39from aiatlas.services import hardware_fit as hf40from aiatlas.services.frontier import config_summary, enrich, group_label4142LINEAGE_PREDICATES = ("derived_from", "fine_tuned_from", "distilled_from", "merged_from", "quantized_from")43RELATION_GROUP_LIMIT = 2444VERSIONED_PROPERTIES = ("context_length", "max_output_tokens", "status", "knowledge_cutoff", "license", "openness", "parameter_count")45MAX_CONCURRENT_GROUPS = 4464748async def relations_grouped(conn: AsyncConnection, entity_id: str) -> list[dict[str, Any]]:49 rows = await fetch_all(conn, f"""50 with rel as (51 select r.predicate, 'out' as direction, r.object_id as other_id, r.observed_at from relations r where r.subject_id = :id and r.valid_to is null52 union all53 select r.predicate, 'in' as direction, r.subject_id as other_id, r.observed_at from relations r where r.object_id = :id and r.valid_to is null),54 ranked as (select rel.*, row_number() over (partition by predicate, direction order by observed_at desc) as rn,55 count(*) over (partition by predicate, direction) as total from rel)56 select k.predicate, k.direction, k.total, {ENTITY_COLS} from ranked k join entities e on e.id = k.other_id left join entities eo on eo.id = e.organization_id57 where k.rn <= :lim and e.merged_into is null order by k.predicate, k.direction, k.rn""", id=entity_id, lim=RELATION_GROUP_LIMIT)58 groups: dict[tuple[str, str], dict[str, Any]] = {}59 for r in rows:60 g = groups.setdefault((r["predicate"], r["direction"]), {"predicate": r["predicate"], "direction": r["direction"], "items": [], "total": int(r["total"])})61 g["items"].append(entity_summary(r))62 return list(groups.values())636465async def sources_of(conn: AsyncConnection, entity_id: str, limit: int = 60) -> list[dict[str, Any]]:66 """Documents attached to the entity + documents whose snapshots back its claims (deduped by URL — `documents.url` is unique)."""67 rows = await fetch_all(conn, """68 with docs as (select d.id from documents d where d.entity_id = :id69 union select s.document_id from claims c join snapshots s on s.id = c.snapshot_id where c.entity_id = :id70 union select s.document_id from relations r join snapshots s on s.id = r.snapshot_id where r.subject_id = :id or r.object_id = :id)71 select d.url, d.doc_type, d.title, greatest(d.last_fetched_at, d.last_changed_at) as last_observed_at, s.id as source_id, s.name as source_name,72 s.domain, s.tier, (select count(*) from snapshots x where x.document_id = d.id) as snapshots73 from docs join documents d on d.id = docs.id left join sources s on s.id = d.source_id74 order by s.tier nulls last, last_observed_at desc nulls last limit :lim""", id=entity_id, lim=limit)75 return [{"source_id": r["source_id"], "source_name": r["source_name"], "domain": r["domain"], "tier": r["tier"], "url": r["url"], "doc_type": r["doc_type"],76 "title": r.get("title"), "last_observed_at": r["last_observed_at"], "snapshots": int(r["snapshots"] or 0)} for r in rows]777879def _scope_sql(is_org: bool) -> str:80 if is_org:81 return ("(ev.entity_id = :id or ev.entity_id in (select id from entities where organization_id = :id union "82 "select object_id from relations where subject_id = :id and predicate in ('develops','owns','operates','published') and valid_to is null))")83 return "ev.entity_id = :id"848586def event_date_col(date_field: str) -> str:87 return "ev.observed_at" if date_field == "observed" else "ev.occurred_at"888990async def timeline_of(conn: AsyncConnection, entity_id: str, entity_type: str, *, limit: int = 30, before: Any = None,91 include_documents: bool = False, include_backfill: bool = False, date_field: str = "occurred") -> list[dict[str, Any]]:92 col = event_date_col(date_field)93 where = [_scope_sql(entity_type in COMPANY_TYPES)]94 params: dict[str, Any] = {"id": entity_id, "lim": limit}95 if before is not None:96 where.append(f"{col} < :before")97 params["before"] = before98 if not include_documents:99 where.append("ev.event_type <> 'DOCUMENT_CHANGED'")100 if not include_backfill:101 where.append("ev.is_backfill = false")102 rows = await fetch_all(conn, f"select {EVENT_COLS}, ev.occurred_at, ev.is_backfill, ev.group_key from {EVENT_FROM} where {' and '.join(where)} "103 f"order by {col} desc, ev.id desc limit :lim", **params)104 out = []105 for r in rows:106 ev = change_event(r)107 ev["occurred_at"], ev["is_backfill"], ev["group_key"] = r.get("occurred_at"), r.get("is_backfill"), r.get("group_key")108 out.append(ev)109 return out110111112async def prices_of_model(conn: AsyncConnection, model_id: str, *, current_only: bool) -> list[dict[str, Any]]:113 cond = "and p.valid_to is null" if current_only else ""114 order = "p.input_per_mtok nulls last, pv.canonical_name" if current_only else "p.valid_from, p.id"115 rows = await fetch_all(conn, f"select {PRICE_COLS} from {PRICE_FROM} where p.model_id = :id {cond} order by {order} limit 500", id=model_id)116 return [price_row(r) for r in rows]117118119async def deployments_of_model(conn: AsyncConnection, model_id: str) -> list[dict[str, Any]]:120 rows = await fetch_all(conn, f"select {PRICE_COLS} from {PRICE_FROM} where p.model_id = :id and p.valid_to is null order by p.output_per_mtok nulls last, pv.canonical_name limit 200", id=model_id)121 return [deployment_row(r) for r in rows]122123124async def prices_of_provider(conn: AsyncConnection, provider_id: str) -> list[dict[str, Any]]:125 rows = await fetch_all(conn, f"select {PRICE_COLS} from {PRICE_FROM} where p.provider_id = :id and p.valid_to is null order by m.canonical_name limit 500", id=provider_id)126 return [price_row(r) for r in rows]127128129async def deployments_of_provider(conn: AsyncConnection, provider_id: str) -> list[dict[str, Any]]:130 """Current offers of this provider, cheapest output first."""131 rows = await fetch_all(conn, f"select {PRICE_COLS} from {PRICE_FROM} where p.provider_id = :id and p.valid_to is null order by p.output_per_mtok asc nulls last, m.canonical_name limit 500", id=provider_id)132 return [deployment_row(r) for r in rows]133134135async def removed_deployments_of_provider(conn: AsyncConnection, provider_id: str, *, days: int = 90) -> list[dict[str, Any]]:136 """Offers this provider closed in the last `days` days (status delisted)."""137 rows = await fetch_all(conn, f"select {PRICE_COLS} from {PRICE_FROM} where p.provider_id = :id and p.valid_to > now() - make_interval(days => :d) order by p.valid_to desc limit 200", id=provider_id, d=days)138 return [deployment_row(r) for r in rows]139140141async def results_of_model(conn: AsyncConnection, model_id: str) -> list[dict[str, Any]]:142 rows = await fetch_all(conn, f"select {RESULT_COLS} from {RESULT_FROM} where r.model_id = :id and r.valid_to is null order by b.canonical_name, r.observed_at desc limit 300", id=model_id)143 return [result_row(r) for r in rows]144145146async def benchmarks_of_model(conn: AsyncConnection, model_id: str) -> dict[str, Any]:147 """Current results grouped by benchmark → metric → (config group): best row, n_rows, trust level, comparability group label."""148 rows = await fetch_all(conn, """149 select r.id, r.model_id, r.benchmark_id, r.score, r.metric, r.unit, r.higher_is_better, r.config, r.evaluated_at, r.observed_at, r.source_url, r.tier, r.confidence,150 r.config_key, r.trust_level, r.extractor, s.key as source_key, b.slug as benchmark_slug, b.canonical_name as benchmark_name, b.attributes->>'category' as category151 from benchmark_results r join entities b on b.id = r.benchmark_id left join sources s on s.id = r.source_id152 where r.model_id = :id and r.valid_to is null and r.is_current order by b.canonical_name, r.observed_at desc limit 500""", id=model_id)153 for r in rows:154 r["model_name"] = None155 enrich(r)156 by_bench: dict[str, dict[str, Any]] = {}157 for r in rows:158 b = by_bench.setdefault(r["benchmark_id"], {"benchmark": {"id": r["benchmark_id"], "slug": r["benchmark_slug"], "name": r["benchmark_name"], "category": r["category"]}, "metrics": {}})159 m = b["metrics"].setdefault(r["metric_canonical"], {"metric": r["metric_canonical"], "groups": {}})160 g = m["groups"].setdefault(r["config_key"], {"config_key": r["config_key"], "label": group_label(r["metric_canonical"], r.get("config")), "rows": []})161 g["rows"].append(r)162 items = []163 for b in by_bench.values():164 metrics = []165 for m in b["metrics"].values():166 groups = []167 for g in m["groups"].values():168 hib = all(x.get("higher_is_better", True) for x in g["rows"])169 best = max(g["rows"], key=lambda x: x["score"]) if hib else min(g["rows"], key=lambda x: x["score"])170 groups.append({"config_key": g["config_key"], "comparability_group": g["label"], "n_rows": len(g["rows"]), "higher_is_better": hib,171 "best": {"score": best["score"], "unit": best.get("unit"), "trust_level": best["trust_level"], "trust_label": TRUST_LABELS.get(best["trust_level"], best["trust_level"]),172 "config": config_summary(best.get("config")), "evaluated_at": best.get("evaluated_at"), "observed_at": best["observed_at"],173 "source_url": best.get("source_url"), "tier": best.get("tier"), "result_id": best["id"]},174 "trust_levels": sorted({x["trust_level"] for x in g["rows"]})})175 metrics.append({"metric": m["metric"], "groups": groups})176 items.append({**b["benchmark"], "metrics": metrics})177 return {"items": items, "total_rows": len(rows), "note": "Current rows only, grouped by benchmark → canonical metric → comparability group (task configuration). "178 "Effort variants folded into this model appear as rows of the same group."}179180181async def leaderboard(conn: AsyncConnection, benchmark_id: str, *, limit: int = 100, offset: int = 0, config: str | None = None, history: bool = False,182 metric: str | None = None, config_key: str | None = None) -> list[dict[str, Any]]:183 where = ["r.benchmark_id = :id"]184 params: dict[str, Any] = {"id": benchmark_id, "lim": limit, "off": offset}185 if not history:186 where.append("r.valid_to is null")187 if config:188 where.append("r.config::text ilike :cfg")189 params["cfg"] = f"%{config}%"190 if metric:191 where.append("lower(r.metric) = lower(:metric)")192 params["metric"] = metric193 if config_key:194 where.append("r.config_key = :ck")195 params["ck"] = config_key196 rows = await fetch_all(conn, f"select {RESULT_COLS}, r.config_key, r.trust_level from {RESULT_FROM} where {' and '.join(where)} order by {RESULT_ORDER} limit :lim offset :off", **params)197 out = []198 for r in rows:199 item = result_row(r)200 item["config_key"] = r.get("config_key")201 item["trust_level"] = r.get("trust_level")202 out.append(item)203 return out204205206async def lineage_of(conn: AsyncConnection, model_id: str) -> dict[str, list[dict[str, Any]]]:207 preds = list(LINEAGE_PREDICATES)208 desc_preds = [p for p in preds if p != "quantized_from"]209 ancestors = await fetch_all(conn, f"""210 with recursive up as (211 select r.object_id as id, 1 as depth from relations r where r.subject_id = :id and r.valid_to is null and r.predicate = any(cast(:preds as text[]))212 union213 select r.object_id, up.depth + 1 from up join relations r on r.subject_id = up.id and r.valid_to is null and r.predicate = any(cast(:preds as text[])) where up.depth < 3)214 select distinct on (e.id) up.depth, {ENTITY_COLS} from up join entities e on e.id = up.id left join entities eo on eo.id = e.organization_id215 where e.id <> :id and e.merged_into is null order by e.id, up.depth""", id=model_id, preds=preds)216 descendants = await fetch_all(conn, f"""217 with recursive down as (218 select r.subject_id as id, 1 as depth from relations r where r.object_id = :id and r.valid_to is null and r.predicate = any(cast(:preds as text[]))219 union220 select r.subject_id, down.depth + 1 from down join relations r on r.object_id = down.id and r.valid_to is null and r.predicate = any(cast(:preds as text[])) where down.depth < 3)221 select distinct on (e.id) down.depth, {ENTITY_COLS} from down join entities e on e.id = down.id left join entities eo on eo.id = e.organization_id222 where e.id <> :id and e.merged_into is null and e.entity_type <> 'artifact' order by e.id, down.depth""", id=model_id, preds=desc_preds)223 quants = await fetch_all(conn, f"""select {ENTITY_COLS} from relations r join entities e on e.id = r.subject_id left join entities eo on eo.id = e.organization_id224 where r.object_id = :id and r.predicate = 'quantized_from' and r.valid_to is null and e.merged_into is null225 order by e.canonical_name limit 100""", id=model_id)226 key = lambda r: (r.get("depth", 0), r["canonical_name"] or "")227 return {"ancestors": [entity_summary(r) for r in sorted(ancestors, key=key)], "descendants": [entity_summary(r) for r in sorted(descendants, key=key)],228 "quantizations": [entity_summary(r) for r in quants]}229230231async def artifacts_of_model(conn: AsyncConnection, model_id: str) -> dict[str, Any]:232 """Artifacts (entity_type 'artifact' with canonical_id = model, or `artifact_of` relation) grouped by kind."""233 rows = await fetch_all(conn, f"""234 with ids as (select e.id, e.artifact_kind from entities e where e.canonical_id = :id and e.entity_type = 'artifact' and e.merged_into is null235 union select r.subject_id, null from relations r join entities x on x.id = r.subject_id where r.object_id = :id and r.predicate in ('artifact_of','quantized_from')236 and r.valid_to is null and x.entity_type = 'artifact' and x.merged_into is null)237 select distinct on (e.id) coalesce(e.artifact_kind, ids.artifact_kind) as kind, {ENTITY_COLS} from ids join entities e on e.id = ids.id left join entities eo on eo.id = e.organization_id238 order by e.id limit 300""", id=model_id)239 groups: dict[str, list[dict[str, Any]]] = defaultdict(list)240 for r in rows:241 kind = r["kind"] if r["kind"] in ARTIFACT_KINDS else "other"242 s = entity_summary(r) or {}243 s["artifact_kind"] = r["kind"]244 groups[kind].append(s)245 ordered = [k for k in (*ARTIFACT_KINDS, "other") if k in groups]246 return {"items": [{"kind": k, "items": sorted(groups[k], key=lambda x: x["name"] or ""), "count": len(groups[k])} for k in ordered], "total": len(rows)}247248249async def family_of_model(conn: AsyncConnection, row: dict[str, Any]) -> dict[str, Any] | None:250 fid = row.get("family_id")251 if fid:252 fam = await fetch_one(conn, f"select {ENTITY_COLS} from {ENTITY_FROM} where e.id = :id", id=fid)253 if fam:254 return entity_summary(fam)255 label = (row.get("attributes") or {}).get("family")256 if label:257 fam = await fetch_one(conn, f"select {ENTITY_COLS} from {ENTITY_FROM} where e.entity_type = 'model_family' and e.merged_into is null and e.canonical_name ilike :n limit 1", n=label)258 return entity_summary(fam) if fam else {"id": None, "entity_type": "model_family", "slug": None, "name": label, "canonical": False,259 "note": "family label from attributes; no model_family entity yet"}260 return None261262263async def canonical_of_artifact(conn: AsyncConnection, row: dict[str, Any]) -> dict[str, Any] | None:264 cid = row.get("canonical_id")265 if not cid:266 rel = await fetch_one(conn, "select object_id from relations where subject_id = :id and predicate in ('artifact_of','quantized_from') and valid_to is null order by predicate limit 1", id=row["id"])267 cid = rel["object_id"] if rel else None268 if not cid:269 return None270 can = await fetch_one(conn, f"select {ENTITY_COLS} from {ENTITY_FROM} where e.id = :id", id=cid)271 return entity_summary(can) if can else None272273274async def identity_of_model(conn: AsyncConnection, model_id: str, identifiers: list[dict[str, str]]) -> dict[str, Any]:275 counts = await fetch_one(conn, """276 select (select count(*) from entities a where a.canonical_id = :id and a.entity_type = 'artifact' and a.merged_into is null and coalesce(a.artifact_kind, '') <> 'checkpoint') as third_party,277 (select count(*) from entities a where a.canonical_id = :id and a.entity_type = 'artifact' and a.merged_into is null and a.artifact_kind = 'checkpoint') as official_artifacts,278 (select count(distinct p.provider_id) from prices p where p.model_id = :id and p.valid_to is null) as providers,279 (select count(*) from entities v where v.merged_into = :id) as folded_variants""", id=model_id)280 hf_repos = [i["value"] for i in identifiers if i["scheme"] == "hf_repo"]281 api_ids = sorted({i["value"] for i in identifiers if i["scheme"].endswith("_model_id") or i["scheme"] in ("openrouter", "api_model_id", "artificial_analysis")})282 c = counts or {}283 return {"canonical_model": True, "official_checkpoints": hf_repos, "official_artifacts": int(c.get("official_artifacts") or 0), "third_party_artifacts": int(c.get("third_party") or 0),284 "provider_deployments": int(c.get("providers") or 0), "folded_variants": int(c.get("folded_variants") or 0), "api_aliases": api_ids,285 "note": "official_checkpoints = hf_repo identifiers carried by the model itself; artifacts are separate entities pointing here through canonical_id."}286287288def licence_block(attrs: dict[str, Any]) -> dict[str, Any] | None:289 key = attrs.get("license_key") or normalize_license(attrs.get("license"))290 info = LICENSES.get(key) if key else None291 if not info:292 return {"key": None, "raw": attrs.get("license") or attrs.get("license_raw"), "note": "licence label not classified in the ontology"} if attrs.get("license") else None293 return {**info.as_dict(), "raw": attrs.get("license_raw") or attrs.get("license"), "url_observed": attrs.get("license_url")}294295296def openness_block(attrs: dict[str, Any]) -> dict[str, Any] | None:297 raw = attrs.get("openness")298 cat = normalize_openness(raw) if raw else None299 dims = attrs.get("openness_dimensions")300 if not isinstance(dims, dict):301 key = attrs.get("license_key") or normalize_license(attrs.get("license"))302 weights = True if cat in ("open-weights", "open-source", "restricted-weights") else False if cat == "proprietary" else None303 dims = openness_dimensions(weights_available=weights, license_key=key, license_raw=attrs.get("license"))304 if not cat and not raw:305 return None306 return {"category": cat or "unknown", "raw": raw, "label": OPENNESS_LABELS.get(cat or "unknown"), "definition": OPENNESS_DEFINITIONS.get(cat or "unknown"), "dimensions": dims,307 "note": "dimensions marked null are unknown, not false"}308309310async def version_history_of(conn: AsyncConnection, entity_id: str) -> list[dict[str, Any]]:311 rows = await fetch_all(conn, f"select {CLAIM_COLS} from {CLAIM_FROM} where c.entity_id = :id and c.property = any(cast(:props as text[])) and c.status <> 'retracted' "312 f"order by c.property, c.valid_from asc, c.observed_at asc limit 2000", id=entity_id, props=list(VERSIONED_PROPERTIES))313 by_prop: dict[str, list[dict[str, Any]]] = defaultdict(list)314 for r in rows:315 by_prop[r["property"]].append(r)316 out = []317 for prop in VERSIONED_PROPERTIES:318 claims = by_prop.get(prop)319 if not claims:320 continue321 transitions = []322 prev: Any = None323 for c in claims:324 if c["status"] == "conflicting":325 continue326 if c["value"] == prev and transitions:327 transitions[-1]["valid_to"] = c["valid_to"] or transitions[-1]["valid_to"]328 continue329 transitions.append({"from": prev, "to": c["value"], "valid_from": c["valid_from"], "valid_to": c["valid_to"], "effective_at": c["effective_at"],330 "source_url": c["source_url"], "tier": c["tier"], "claim_id": c["id"], "status": c["status"]})331 prev = c["value"]332 out.append({"property": prop, "transitions": transitions, "current": transitions[-1]["to"] if transitions else None})333 return out334335336async def providers_of_model(conn: AsyncConnection, model_id: str) -> list[dict[str, Any]]:337 rows = await fetch_all(conn, f"""338 with ids as (select r.object_id as id from relations r where r.subject_id = :id and r.predicate = 'available_through' and r.valid_to is null339 union select p.provider_id from prices p where p.model_id = :id and p.valid_to is null)340 select {ENTITY_COLS} from ids join entities e on e.id = ids.id left join entities eo on eo.id = e.organization_id341 where e.merged_into is null order by e.canonical_name limit 100""", id=model_id)342 return [entity_summary(r) for r in rows]343344345async def hardware_fit_of_model(conn: AsyncConnection, attrs: dict[str, Any]) -> list[dict[str, Any]] | None:346 params = hf.parameter_count(attrs)347 if params is None:348 return None349 context = 8192350 rows = await fetch_all(conn, f"select {ENTITY_COLS} from {ENTITY_FROM} where e.entity_type = 'hardware' and e.merged_into is null and e.attributes ? 'memory_gb' order by e.canonical_name limit 200")351 out: list[dict[str, Any]] = []352 for r in rows:353 mem = hf.hardware_memory_gb(r["attributes"])354 if mem is None:355 continue356 f = hf.fit(params, mem, "4bit", context)357 out.append({"hardware": entity_summary(r), "memory_gb": mem, **f})358 out.sort(key=lambda x: (not x["fits"], -x["headroom_gb"]))359 return out360361362async def related_by_type(conn: AsyncConnection, entity_id: str, etype: str, *, limit: int = 50, offset: int = 0, include_org_children: bool = False) -> tuple[list[dict[str, Any]], int]:363 """Entities of `etype` linked to `entity_id` by any live relation (either direction) — optionally also those whose organization is `entity_id`."""364 org_sql = "union select id from entities where organization_id = :id" if include_org_children else ""365 rows = await fetch_all(conn, f"""366 with ids as (select r.object_id as id from relations r where r.subject_id = :id and r.valid_to is null367 union select r.subject_id from relations r where r.object_id = :id and r.valid_to is null {org_sql})368 select count(*) over () as total, {ENTITY_COLS} from ids join entities e on e.id = ids.id left join entities eo on eo.id = e.organization_id369 where e.entity_type = :t and e.merged_into is null order by coalesce(e.attributes->>'release_date', e.attributes->>'published_at', '') desc, e.updated_at desc370 limit :lim offset :off""", id=entity_id, t=etype, lim=limit, off=offset)371 return [entity_summary(r) for r in rows], int(rows[0]["total"]) if rows else 0372373374async def _run_group(tasks: list[tuple[str, Any, tuple[Any, ...], dict[str, Any]]]) -> dict[str, Any]:375 """Run the blocks of one group sequentially on a single pooled connection."""376 out: dict[str, Any] = {}377 async with connection() as conn:378 for name, fn, args, kw in tasks:379 out[name] = await fn(conn, *args, **kw)380 return out381382383async def entity_detail(row: dict[str, Any]) -> dict[str, Any]:384 """Full detail. Blocks are spread over ≤ 4 concurrent groups (one connection each)."""385 eid, etype = row["id"], row["entity_type"]386 provenance = dict(row.get("provenance") or {})387 attrs = row.get("attributes") or {}388389 async def base(conn: AsyncConnection) -> dict[str, Any]:390 aliases = await fetch_all(conn, "select alias from entity_aliases where entity_id = :id order by kind, alias limit 200", id=eid)391 idents = await fetch_all(conn, "select scheme, value from entity_identifiers where entity_id = :id order by scheme, value limit 200", id=eid)392 await enrich_provenance(conn, provenance)393 return {"aliases": [a["alias"] for a in aliases], "identifiers": [{"scheme": i["scheme"], "value": i["value"]} for i in idents]}394395 T = lambda name, fn, *args, **kw: (name, fn, args, kw)396 groups: list[list[tuple[str, Any, tuple[Any, ...], dict[str, Any]]]] = [397 [T("base", base), T("relations", relations_grouped, eid)],398 [T("sources", sources_of, eid), T("timeline", timeline_of, eid, etype)],399 ]400 if etype == "model":401 groups[0] += [T("prices", prices_of_model, eid, current_only=True), T("deployments", deployments_of_model, eid), T("providers", providers_of_model, eid)]402 groups[1] += [T("price_history", prices_of_model, eid, current_only=False), T("family", family_of_model, row), T("artifacts", artifacts_of_model, eid)]403 groups.append([T("results", results_of_model, eid), T("benchmarks", benchmarks_of_model, eid), T("version_history", version_history_of, eid)])404 groups.append([T("lineage", lineage_of, eid), T("hardware_fit", hardware_fit_of_model, attrs), T("papers", related_by_type, eid, "paper", limit=24),405 T("repositories", related_by_type, eid, "repository", limit=24)])406 elif etype == "artifact":407 groups[0] += [T("canonical", canonical_of_artifact, row), T("prices", prices_of_model, eid, current_only=True)]408 groups[1] += [T("lineage", lineage_of, eid), T("hardware_fit", hardware_fit_of_model, attrs)]409 elif etype in COMPANY_TYPES:410 groups[0] += [T("models", related_by_type, eid, "model", limit=50, include_org_children=True)]411 groups[1] += [T("papers", related_by_type, eid, "paper", limit=24, include_org_children=True), T("repositories", related_by_type, eid, "repository", limit=24, include_org_children=True)]412 elif etype == "provider":413 groups[0] += [T("prices", prices_of_provider, eid), T("deployments", deployments_of_provider, eid)]414 groups[1] += [T("models", _provider_models, eid), T("removed", removed_deployments_of_provider, eid)]415 elif etype == "benchmark":416 groups[0] += [T("results", leaderboard, eid, limit=100)]417 elif etype == "hardware":418 groups[0] += [T("models", related_by_type, eid, "model", limit=50)]419 elif etype in ("framework", "library", "runtime"):420 groups[0] += [T("repositories", related_by_type, eid, "repository", limit=24)]421 elif etype == "model_family":422 groups[0] += [T("models", _family_models, eid)]423424 results = await asyncio.gather(*(_run_group(g) for g in groups[:MAX_CONCURRENT_GROUPS]))425 blocks: dict[str, Any] = {}426 for r in results:427 blocks.update(r)428429 detail = entity_summary(row) or {}430 detail["attributes"] = attrs431 detail["provenance"] = provenance432 detail.update(blocks.pop("base"))433 detail["relations"] = blocks.pop("relations")434 detail["sources"] = blocks.pop("sources")435 detail["timeline"] = blocks.pop("timeline")436 for k, v in blocks.items():437 if k in ("models",) and isinstance(v, tuple):438 items, total = v439 detail[k] = {"items": items, "total": total, "limit": 50, "offset": 0}440 elif k in ("papers", "repositories") and isinstance(v, tuple):441 detail[k] = v[0]442 elif v is not None:443 detail[k] = v444 if etype == "model":445 if "hardware_fit" in detail:446 detail["hardware_fit_assumptions"] = hf.ASSUMPTIONS447 detail["identity"] = await _with_conn(identity_of_model, eid, detail.get("identifiers") or [])448 lic = licence_block(attrs)449 if lic is not None:450 detail["licence"] = lic451 opn = openness_block(attrs)452 if opn is not None:453 detail["openness"] = opn454 detail["family_id"] = row.get("family_id")455 detail["identity_confidence"] = row.get("identity_confidence")456 if etype == "artifact":457 detail["artifact_kind"] = row.get("artifact_kind")458 detail.setdefault("canonical", None)459 if "hardware_fit" in detail:460 detail["hardware_fit_assumptions"] = hf.ASSUMPTIONS461 if row.get("redirected_from"):462 detail["redirected_from"] = row["redirected_from"]463 return detail464465466async def _with_conn(fn: Any, *args: Any) -> Any:467 async with connection() as conn:468 return await fn(conn, *args)469470471async def _provider_models(conn: AsyncConnection, provider_id: str) -> tuple[list[dict[str, Any]], int]:472 rows = await fetch_all(conn, f"""473 with ids as (select p.model_id as id from prices p where p.provider_id = :id and p.valid_to is null474 union select r.subject_id from relations r where r.object_id = :id and r.predicate = 'available_through' and r.valid_to is null)475 select count(*) over () as total, {ENTITY_COLS} from ids join entities e on e.id = ids.id left join entities eo on eo.id = e.organization_id476 where e.merged_into is null order by e.canonical_name limit 50""", id=provider_id)477 return [entity_summary(r) for r in rows], int(rows[0]["total"]) if rows else 0478479480async def _family_models(conn: AsyncConnection, family_id: str) -> tuple[list[dict[str, Any]], int]:481 rows = await fetch_all(conn, f"""select count(*) over () as total, {ENTITY_COLS} from {ENTITY_FROM} where e.family_id = :id and e.entity_type = 'model' and e.merged_into is null482 order by e.attributes->>'release_date' desc nulls last, e.canonical_name limit 50""", id=family_id)483 return [entity_summary(r) for r in rows], int(rows[0]["total"]) if rows else 0484485486__all__ = [487 "VERSIONED_PROPERTIES",488 "artifacts_of_model",489 "benchmarks_of_model",490 "deployments_of_model",491 "entity_detail",492 "event_date_col",493 "family_of_model",494 "hardware_fit_of_model",495 "leaderboard",496 "licence_block",497 "lineage_of",498 "openness_block",499 "prices_of_model",500 "prices_of_provider",501 "providers_of_model",502 "related_by_type",503 "relations_grouped",504 "results_of_model",505 "sources_of",506 "timeline_of",507 "version_history_of",508]509