All files / src/apps/user/pages/evaluation BestConfigurations.tsx

100% Statements 13/13
95.83% Branches 23/24
100% Functions 6/6
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176                                                                        3x 3x       3x 2x                                   3x                                                                                                                   3x         101x 101x     24x           101x 101x   101x                                                           2x                  
/**
 * What works best on a dataset — every experiment's newest run on one
 * dataset version, variant by variant.
 *
 * Rows are grouped by how they were graded (a scorer, and for the LLM judge
 * the grading model), because two scorers' 0.8s are not one measurement.
 * Within a group, **best value** marks the variants nothing else beats on
 * both score and cost. svc-core does the grouping and marking; this renders
 * it.
 */
 
import {
  BestConfigurations as Best,
  BestConfigurationGroup,
} from "@/objects/Evaluation";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery } from "@tanstack/react-query";
import { Trophy } from "lucide-react";
import React from "react";
import { Link } from "react-router-dom";
import {
  EmptyBlock,
  ErrorNote,
  ScoreBadge,
  SectionCard,
  Skeleton,
  experimentHref,
  formatCost,
  formatDuration,
  formatScore,
  formatSpread,
  targetLabel,
} from "./components";
 
/** A group's heading: the scorer, and whose judgement it was. */
export const groupHeading = (group: BestConfigurationGroup): string =>
  group.scorer === "llm"
    ? `${group.scorer_label} · ${group.judge?.model_id ? targetLabel(group.judge) : "default judge"}`
    : group.scorer_label;
 
const GroupTable: React.FC<{ group: BestConfigurationGroup }> = ({ group }) => (
  <div>
    <div className="text-[10px] uppercase tracking-wide font-medium text-gray-400 dark:text-gray-500 mb-1">
      {groupHeading(group)}
    </div>
    <div className="overflow-x-auto">
      <table className="w-full text-xs">
        <thead>
          <tr className="text-left text-[10px] uppercase tracking-wide text-gray-400 dark:text-gray-500">
            <th className="py-1.5 pr-2 font-medium w-6">#</th>
            <th className="py-1.5 pr-2 font-medium">Variant</th>
            <th className="py-1.5 pr-2 font-medium">Score</th>
            <th className="py-1.5 pr-2 font-medium">Pass rate</th>
            <th className="py-1.5 pr-2 font-medium">Cost / case</th>
            <th className="py-1.5 pr-2 font-medium">Latency</th>
          </tr>
        </thead>
        <tbody className="divide-y divide-gray-100 dark:divide-gray-700">
          {group.rows.map((row, i) => (
            <tr key={`${row.batch_id}:${row.variant_id}`}>
              <td className="py-2 pr-2 text-gray-400 dark:text-gray-500 tabular-nums">
                {i + 1}
              </td>
              <td className="py-2 pr-2 min-w-[12rem]">
                <div className="flex items-center gap-1.5 flex-wrap">
                  <span className="text-gray-800 dark:text-gray-200">
                    {row.variant_name}
                  </span>
                  {row.best_value && (
                    <span
                      className="text-[10px] px-1.5 py-0.5 rounded-full bg-violet-50 dark:bg-violet-950/40 text-violet-700 dark:text-violet-300 border border-violet-200 dark:border-violet-800"
                      title="Nothing else in this group scores as well for less"
                    >
                      Best value
                    </span>
                  )}
                  {row.preset && (
                    <span className="text-[10px] px-1.5 py-0.5 rounded-full bg-sky-50 dark:bg-sky-950/40 text-sky-700 dark:text-sky-300 border border-sky-200 dark:border-sky-800">
                      {row.preset.dimension}: {row.preset.label}
                    </span>
                  )}
                </div>
                <Link
                  to={experimentHref(row.experiment_id)}
                  className="text-[11px] text-gray-400 dark:text-gray-500 hover:underline"
                >
                  {row.experiment_name}
                </Link>
              </td>
              <td className="py-2 pr-2 whitespace-nowrap">
                <ScoreBadge value={row.mean_score} />
                {row.score_sd != null && (
                  <span
                    className="block text-[10px] text-gray-400 dark:text-gray-500 tabular-nums"
                    title={`How far the mean moved across ${row.repeats} repeats (standard deviation)`}
                  >
                    {formatSpread(row.score_sd)}
                  </span>
                )}
              </td>
              <td className="py-2 pr-2 tabular-nums text-gray-600 dark:text-gray-300">
                {formatScore(row.pass_rate)}
              </td>
              <td className="py-2 pr-2 tabular-nums text-gray-600 dark:text-gray-300">
                {formatCost(row.cost_per_case_usd)}
              </td>
              <td className="py-2 pr-2 tabular-nums text-gray-600 dark:text-gray-300">
                {formatDuration(row.mean_latency_ms)}
              </td>
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  </div>
);
 
const BestConfigurations: React.FC<{
  suiteId: string;
  /** The dataset version to compare on; null when the dataset has none. */
  version: number | null;
}> = ({ suiteId, version }) => {
  const { getApiHandler } = useApiHandler();
  const query = useQuery<Best>({
    queryKey: [QUERY_KEYS.GET.BEST_CONFIGURATIONS, suiteId, version],
    queryFn: () =>
      getApiHandler().evaluation_router.getBestConfigurations(
        suiteId,
        version ?? undefined,
      ),
    enabled: !!suiteId,
  });
  const data: Best | undefined = query.data;
  const groups = data?.groups ?? [];
 
  return (
    <SectionCard
      icon={Trophy}
      title="Best configurations"
      subtitle={
        version !== null
          ? `every experiment's newest run on version ${version}, ranked within how it was graded`
          : "every experiment's newest run, ranked within how it was graded"
      }
    >
      {query.isLoading ? (
        <Skeleton className="h-16 w-full" />
      ) : query.isError ? (
        <ErrorNote message="Could not load the comparison." />
      ) : groups.length === 0 ? (
        <EmptyBlock
          icon={Trophy}
          message="Nothing to compare yet."
          hint="Finish a run of an experiment on this version and its variants are ranked here, beside every other experiment's."
        />
      ) : (
        <div className="space-y-5">
          {groups.length > 1 && (
            <p className="text-xs text-gray-400 dark:text-gray-500">
              These experiments grade differently, so each grading is ranked on
              its own — a score is only comparable with scores measured the same
              way.
            </p>
          )}
          {groups.map((group) => (
            <GroupTable key={group.key} group={group} />
          ))}
        </div>
      )}
    </SectionCard>
  );
};
 
export default BestConfigurations;