// BetterBench (https://github.com/GGZ14/BetterBench, Apache-2.0): the benchmark Kaname runs // against deployments. Flags verified against v0.6.1 (`betterbench run --help`, betterbench/cli.py). import { dirname, join } from "node:path"; import type { BenchmarkSettings } from "@/lib/db/schema"; import { coreEnv } from "@/lib/env"; export function betterbenchBin(): string { return coreEnv().BETTERBENCH_BIN; } /** The Python of BetterBench's own venv (it imports `betterbench`). */ export function betterbenchPython(): string { const bin = betterbenchBin(); return bin.includes("/") ? join(dirname(bin), "python") : "python3"; } export function runArgs(o: { endpoint: string; model: string; out: string; settings: BenchmarkSettings; maxModelLen: number | null; notes: Record; }): string[] { const s = o.settings; return [ "run", "--endpoint", o.endpoint, "--model", o.model, "--out", o.out, // Kaname knows the context window; llama-server's /v1/models doesn't report it. ...(o.maxModelLen ? ["--max-model-len", String(o.maxModelLen)] : []), ...(s.quick ? ["--quick"] : []), ...(s.passes != null ? ["--passes", String(s.passes)] : []), ...(s.warmup != null ? ["--warmup", String(s.warmup)] : []), ...s.phases.map((p) => `--${p}`), ...Object.entries(o.notes).flatMap(([k, v]) => ["--note", `${k}=${v}`]), "--no-update-check", ]; } /** * Prints Kaname's BenchmarkSummary for a results.json, using BetterBench's own report functions * so the numbers match its HTML report. */ export const SUMMARY_SCRIPT = ` import json, math, sys from betterbench import report as r res = json.load(open(sys.argv[1])) def clean(v): if isinstance(v, dict): return {str(k): clean(x) for k, x in v.items()} if isinstance(v, (list, tuple)): return [clean(x) for x in v] if isinstance(v, (bool, str)) or v is None: return v try: f = float(v) except (TypeError, ValueError): return None if math.isnan(f) or math.isinf(f): return None return int(f) if float(f).is_integer() and not isinstance(v, float) else f rows = r.single_rows(res) cfg = res.get("config", {}) env = res.get("env", {}) reasoning = r.reasoning_rows(res) gate = res.get("sample_gate") or r.sample_gate(res) trunc, ok_runs = r.truncation_summary(res) failed = sum(1 for recs in res.get("single_stream", {}).values() for x in recs if not x.get("ok")) keep_cfg = ("warmup", "runs_per_category", "temperature", "top_p", "top_k", "greedy", "unique_nonce", "weights", "concurrency_levels", "concurrency_requests", "prefill_runs", "prefill_warmup", "prefill_max_tokens") out = { "v": 2, "combined": r.combined_score(res, rows), "single": rows, "prefill": r.prefill_rows(res), "concurrency": r.concurrency_rows(res), "reasoning": reasoning if r.has_reasoning_evidence(reasoning) else [], "gate": gate.get("under_sampled", []), "truncated": [trunc, ok_runs], "failedSingle": failed, "phases": r.phases_present(res), "cacheHits": r.cache_hits(res), "betterbenchVersion": res.get("betterbench_version", ""), "corpusVersion": res.get("corpus_version", ""), "corpusHash": env.get("corpus_hash"), "timestamp": env.get("timestamp"), "maxModelLen": env.get("max_model_len"), "config": {k: cfg.get(k) for k in keep_cfg}, "passes": cfg.get("runs_per_category"), } print(json.dumps(clean(out))) `; /** Version of the summary shape above; older stored summaries are recomputed from results.json. */ export const SUMMARY_VERSION = 2;