Skip to content

The platform for continuously improving AI agents.

2.6 KB
Raw Blame
import type { ExperimentBlock, ExperimentDocument } from "./document";
import { evaluationContext, modelCandidates } from "./node-overview-data";

export interface BenchmarkTest {
  node: string;
  evalSet?: { title: string; id?: string };
  dataset: { title: string; node?: string; partition?: string };
}

interface BenchmarkRow {
  key: string;
  candidate?: ReturnType<typeof modelCandidates>[number];
  tests: BenchmarkTest[];
}

export function benchmarkRows(document: ExperimentDocument, nodes: ExperimentBlock[]) {
  const rows = new Map<string, BenchmarkRow>();
  for (const node of nodes) {
    const settings = { ...node.operation?.arguments, ...node.config };
    const context = evaluationContext(document, node.id);
    const suiteEdge = document.edges.find(
      (edge) => edge.target === node.id && edge.input === "suite"
    );
    const suite = document.nodes.find((item) => item.id === suiteEdge?.source);
    const suiteId = settings.eval_set ?? settings.evaluation_set_id ?? settings.eval_set_id;
    const title =
      suite?.title ?? (typeof suiteId === "string" ? "Evaluation set" : context.evaluation);
    const evalSet = title
      ? { id: typeof suiteId === "string" ? suiteId : undefined, title }
      : undefined;
    const datasets = context.datasets.length
      ? context.datasets
      : document.edges
          .filter((edge) => edge.target === node.id && edge.input === "scores")
          .flatMap((edge) => evaluationContext(document, edge.source).datasets);
    const tests = (datasets.length ? datasets : [undefined]).map((data) => {
      const input = document.nodes.find((item) => item.id === data?.id);
      const splitSource =
        input?.kind === "split"
          ? document.edges.find((edge) => edge.target === input.id && edge.input === "data")
          : undefined;
      const source = document.nodes.find((item) => item.id === splitSource?.source);
      return {
        dataset: {
          node: data?.id,
          partition: data?.partition,
          title: source?.title ?? data?.title ?? "No dataset",
        },
        evalSet,
        node: node.id,
      };
    });
    const candidates = modelCandidates(document, node.id);
    for (const candidate of candidates.length ? candidates : [undefined]) {
      const key = JSON.stringify([
        candidate?.kind,
        candidate?.kind === "trained"
          ? (candidate.job ?? candidate.nodeId ?? candidate.id)
          : (candidate?.model ?? candidate?.id),
      ]);
      const existing = rows.get(key);
      if (existing) existing.tests.push(...tests);
      else rows.set(key, { candidate, key, tests: [...tests] });
    }
  }
  return [...rows.values()];
}