# Eval Compare

> Compare the same prompt's outputs from several models or prompt versions side by side, with a word diff, a scorecard, and a preference vote.

Source: https://www.honestui.com/docs/ai/eval-compare

```tsx
"use client"

import * as React from "react"

import {
  EvalCompare,
  type EvalCandidate,
  type EvalMetric,
  type EvalVote,
} from "@/components/ai/eval-compare"

const metrics: EvalMetric[] = [
  { key: "correctness", label: "Correctness", better: "higher", format: (v) => `${Math.round(v * 100)}%` },
  { key: "faithfulness", label: "Faithfulness", better: "higher", format: (v) => `${Math.round(v * 100)}%` },
  { key: "latency", label: "Latency", better: "lower", format: (v) => `${(v / 1000).toFixed(1)} s` },
  { key: "tokens", label: "Output tokens", better: "lower" },
  { key: "cost", label: "Cost", better: "lower", format: (v) => `$${v.toFixed(4)}` },
]

type EvalCase = { prompt: string; expected: string; candidates: EvalCandidate[] }

// Three support questions answered by two models and a revised prompt.
const cases: EvalCase[] = [
  {
    prompt: "A customer on the Team plan asks whether they can get a refund for the unused months after downgrading.",
    expected: "Prorated credit to the account, not a refund to the card; refunds only within 14 days of renewal.",
    candidates: [
      {
        id: "sonnet",
        label: "Large · v3",
        meta: "Large model, prompt v3",
        output:
          "Downgrading keeps your workspace on Team until the end of the billing period. The unused months are added to your account as a prorated credit, which pays for future invoices. Refunds to your card are only available within 14 days of a renewal.",
        metrics: { correctness: 0.96, faithfulness: 0.98, latency: 2140, tokens: 58, cost: 0.0021 },
        assertions: [
          { label: "Mentions prorated credit", passed: true },
          { label: "States the 14-day window", passed: true },
          { label: "Under 80 words", passed: true },
        ],
      },
      {
        id: "haiku",
        label: "Small · v3",
        meta: "Small model, prompt v3",
        output:
          "Downgrading keeps your workspace on Team until the end of the billing period. The unused months are refunded to your card within 5 business days.",
        metrics: { correctness: 0.41, faithfulness: 0.52, latency: 860, tokens: 31, cost: 0.0004 },
        assertions: [
          { label: "Mentions prorated credit", passed: false, detail: "Promises a card refund instead." },
          { label: "States the 14-day window", passed: false },
          { label: "Under 80 words", passed: true },
        ],
      },
      {
        id: "sonnet-v4",
        label: "Large · v4",
        meta: "Prompt v4 cites the policy",
        output:
          "Downgrading keeps your workspace on Team until the end of the billing period. The unused months become a prorated credit on your account, which pays for future invoices. Card refunds are only available within 14 days of a renewal (Billing policy, section 3).",
        metrics: { correctness: 0.97, faithfulness: 0.99, latency: 2410, tokens: 64, cost: 0.0023 },
        assertions: [
          { label: "Mentions prorated credit", passed: true },
          { label: "States the 14-day window", passed: true },
          { label: "Under 80 words", passed: true },
        ],
      },
    ],
  },
  {
    prompt: "How do I move a project to another workspace?",
    expected: "Project settings → Transfer; needs admin in both workspaces; integrations must be reconnected.",
    candidates: [
      {
        id: "sonnet",
        label: "Large · v3",
        meta: "Large model, prompt v3",
        output:
          "Open the project, go to Settings, and choose Transfer. You need to be an admin in both workspaces. Integrations such as Slack and GitHub disconnect during the move, so reconnect them afterwards.",
        metrics: { correctness: 0.94, faithfulness: 0.97, latency: 1980, tokens: 44, cost: 0.0017 },
        assertions: [
          { label: "Names the Transfer setting", passed: true },
          { label: "Mentions admin in both", passed: true },
          { label: "Under 80 words", passed: true },
        ],
      },
      {
        id: "haiku",
        label: "Small · v3",
        meta: "Small model, prompt v3",
        output:
          "Open the project, go to Settings, and choose Transfer. You need to be an admin in both workspaces.",
        metrics: { correctness: 0.78, faithfulness: 0.96, latency: 790, tokens: 22, cost: 0.0003 },
        assertions: [
          { label: "Names the Transfer setting", passed: true },
          { label: "Mentions admin in both", passed: true },
          { label: "Under 80 words", passed: true },
        ],
      },
      {
        id: "sonnet-v4",
        label: "Large · v4",
        meta: "Prompt v4 cites the policy",
        output:
          "Open the project, go to Settings, and choose Transfer. You need to be an admin in both workspaces. Integrations such as Slack and GitHub disconnect during the move, so reconnect them afterwards (Help center: Moving projects).",
        metrics: { correctness: 0.95, faithfulness: 0.97, latency: 2260, tokens: 51, cost: 0.0019 },
        assertions: [
          { label: "Names the Transfer setting", passed: true },
          { label: "Mentions admin in both", passed: true },
          { label: "Under 80 words", passed: true },
        ],
      },
    ],
  },
]

/** Results are fixed sample data; no model runs here. */
export function EvalCompareDemo() {
  const [caseIndex, setCaseIndex] = React.useState(0)
  const [votes, setVotes] = React.useState<EvalVote[]>(() => cases.map(() => null))
  const current = cases[caseIndex]

  return (
    <EvalCompare
      className="max-w-4xl"
      prompt={current.prompt}
      expected={current.expected}
      candidates={current.candidates}
      metrics={metrics}
      caseIndex={caseIndex}
      caseCount={cases.length}
      onCaseChange={setCaseIndex}
      vote={votes[caseIndex]}
      onVote={(vote) => setVotes((all) => all.map((entry, index) => (index === caseIndex ? vote : entry)))}
    />
  )
}

```

## Overview [#overview]

Eval Compare puts two to four answers to the same prompt next to each other: different models, prompt versions, or settings. It can show what changed word by word against a baseline, score each answer on the metrics you measured, list the checks each one passed, and collect a person's preference.

It shows the results you pass. Running the models and scoring them is your application's work.

## Anatomy [#anatomy]

<Anatomy title="Eval Compare">
  <AnatomyItem name="Header" description="The prompt, the expected answer, the case switcher, and the Differences toggle" />

  <AnatomyItem name="Column" description="One per candidate: its label, note, output, checks, and Prefer" />

  <AnatomyItem name="Scorecard" description="A table with a row per metric and a column per candidate" />

  <AnatomyItem name="Footer" description="Tie, and the announcement of the vote, when you collect votes" />
</Anatomy>

Installing `eval-compare` brings [Button](/docs/components/button) and [Toggle](/docs/components/toggle) along as registry dependencies.

## Behavior [#behavior]

### Differences [#differences]

Differences compares every column with the baseline, word by word. Added words are tinted and removed words are struck through, and each column counts both. The first candidate is the baseline until a person chooses another with its "Compare against" link. `diffWords` is exported if you need the same comparison elsewhere.

Very long outputs fall back to marking the whole text as changed instead of blocking the page.

### Scorecard [#scorecard]

Each metric names the direction that wins with `better`. The best value in a row gets a dot and medium weight. A row where every candidate ties, or with fewer than two values, has no best. A missing value shows as a dash. Pass `format` to show units such as seconds or dollars.

### Checks and votes [#checks-and-votes]

Passed checks keep a neutral mark and failed ones are red. Passing `onVote` shows Prefer under each column and Tie in the footer. Pressing the current choice again clears it, and `onVote` receives `null`.

### Cases [#cases]

With `caseIndex`, `caseCount`, and `onCaseChange`, the header shows a switcher such as "Case 2 of 12". Pass the new case's prompt and candidates when it changes.

### Layout [#layout]

Columns sit side by side when the component is wide enough and stack when it is narrow. Rows line up across columns, so outputs of different lengths still start their checks at the same height.

## Accessibility [#accessibility]

The comparison is a group named Comparison, not a landmark, so several on one page stay distinct. Each candidate is an article named by its label. Added and removed words use `ins` and `del`. The scorecard is a table with row and column headers, and the best value is announced as best. Prefer and Differences are toggle buttons that report whether they are pressed, and a vote is announced.

| Key                                  | Action                                                                   |
| ------------------------------------ | ------------------------------------------------------------------------ |
| <kbd>Tab</kbd>                       | Move across the switcher, Differences, the baseline links, and the votes |
| <kbd>Enter</kbd> or <kbd>Space</kbd> | Press the focused control                                                |

## Installation [#installation]


  

  
    <CliBlock commands="[&#x22;@honestai/eval-compare&#x22;]" />
  

  
    
      
        Add the components it is built from.
      

      <CliBlock commands="[&#x22;button&#x22;, &#x22;toggle&#x22;]" />

      
        Copy and paste this file into your project.
      

      ### components/ai/eval-compare.tsx

```tsx
"use client"

import * as React from "react"
import {
  Check as CheckIcon,
  ChevronLeft as ChevronLeftIcon,
  ChevronRight as ChevronRightIcon,
  CircleX as CircleXIcon,
  Diff as DiffIcon,
  Equal as EqualIcon,
  ThumbsUp as ThumbsUpIcon,
} from "honestui/icons"

import { cn } from "@/lib/utils"
import { Button } from "@/components/honest-ui/ui/button"
import { Toggle } from "@/components/honest-ui/ui/toggle"

/* -------------------------------------------------------------------------------------------------
 * Word diff
 * -----------------------------------------------------------------------------------------------*/

export type WordDiffPart = {
  type: "equal" | "added" | "removed"
  /** The words, with the spaces that followed them. */
  text: string
}

// Above this many word pairs the table gets large, so the diff falls back to
// "everything changed" instead of blocking the main thread.
const MAX_DIFF_CELLS = 250_000

function tokenize(text: string) {
  // Each token is a word plus the whitespace after it, so joining tokens
  // gives back the original text.
  return text.match(/\S+\s*|\s+/g) ?? []
}

/**
 * Compares two texts word by word with a longest-common-subsequence table.
 * Words match when they are equal apart from the whitespace after them.
 * Consecutive parts of the same type are merged.
 */
export function diffWords(before: string, after: string): WordDiffPart[] {
  const a = tokenize(before)
  const b = tokenize(after)
  const parts: WordDiffPart[] = []

  const push = (type: WordDiffPart["type"], text: string) => {
    const last = parts[parts.length - 1]
    if (last && last.type === type) last.text += text
    else parts.push({ type, text })
  }

  if (a.length * b.length > MAX_DIFF_CELLS) {
    if (before) push("removed", before)
    if (after) push("added", after)
    return parts
  }

  const key = (token: string) => token.trimEnd()
  // lengths[i][j] is the LCS length of a[i..] and b[j..].
  const lengths: Uint32Array[] = Array.from({ length: a.length + 1 }, () => new Uint32Array(b.length + 1))
  for (let i = a.length - 1; i >= 0; i -= 1) {
    for (let j = b.length - 1; j >= 0; j -= 1) {
      lengths[i][j] =
        key(a[i]) === key(b[j])
          ? lengths[i + 1][j + 1] + 1
          : Math.max(lengths[i + 1][j], lengths[i][j + 1])
    }
  }

  let i = 0
  let j = 0
  while (i < a.length && j < b.length) {
    if (key(a[i]) === key(b[j])) {
      // Keep the new text's spacing.
      push("equal", b[j])
      i += 1
      j += 1
    } else if (lengths[i + 1][j] >= lengths[i][j + 1]) {
      push("removed", a[i])
      i += 1
    } else {
      push("added", b[j])
      j += 1
    }
  }
  while (i < a.length) push("removed", a[i++])
  while (j < b.length) push("added", b[j++])

  return parts
}

function countWords(parts: WordDiffPart[], type: WordDiffPart["type"]) {
  return parts.reduce((total, part) => (part.type === type ? total + tokenize(part.text.trim()).length : total), 0)
}

/* -------------------------------------------------------------------------------------------------
 * Types
 * -----------------------------------------------------------------------------------------------*/

export type EvalMetric = {
  key: string
  label: string
  /** Which direction wins. Without it the row has no best value. */
  better?: "higher" | "lower"
  /** Formats a value for display, such as `(v) => \`${v} ms\``. */
  format?: (value: number) => string
}

export type EvalAssertion = {
  label: string
  passed: boolean
  /** Why it failed, or what was checked. */
  detail?: string
}

export type EvalCandidate = {
  id: string
  /** The model or prompt version, such as "gpt-5-mini". */
  label: string
  /** A short note under the label, such as "Prompt v3 · temperature 0.2". */
  meta?: string
  /** The output as plain text. Line breaks are kept. */
  output: string
  /** A value for each metric `key`. `null` or a missing key shows as no value. */
  metrics?: Record<string, number | null | undefined>
  assertions?: EvalAssertion[]
}

export type EvalVote = string | "tie" | null

export type EvalCompareLabels = {
  /** Names the comparison region. */
  region: string
  prompt: string
  expected: string
  /** The Toggle that turns the word diff on. */
  showDiff: string
  baseline: string
  /** The button that makes a column the baseline. */
  useAsBaseline: (label: string) => string
  /** The count under each compared column, such as "12 words added, 4 removed". */
  diffSummary: (added: number, removed: number) => string
  added: string
  removed: string
  scorecard: string
  metric: string
  best: string
  noValue: string
  checks: (passed: number, total: number) => string
  passed: string
  failed: string
  /** The vote button's visible text; the candidate's label follows it for screen readers. */
  preferButton: string
  tie: string
  /** Announced when a vote is cast. */
  voted: (label: string | null) => string
  previousCase: string
  nextCase: string
  caseCount: (index: number, count: number) => string
}

export const defaultEvalCompareLabels: EvalCompareLabels = {
  region: "Comparison",
  prompt: "Prompt",
  expected: "Expected",
  showDiff: "Differences",
  baseline: "Baseline",
  useAsBaseline: (label) => `Compare against ${label}`,
  diffSummary: (added, removed) =>
    added === 0 && removed === 0 ? "Same words as the baseline" : `${added} words added, ${removed} removed`,
  added: "Added",
  removed: "Removed",
  scorecard: "Scorecard",
  metric: "Metric",
  best: "Best",
  noValue: "No value",
  checks: (passed, total) => `${passed} of ${total} checks passed`,
  passed: "Passed",
  failed: "Failed",
  preferButton: "Prefer",
  tie: "Tie",
  voted: (label) => (label ? `Preferred ${label}.` : "Marked as a tie."),
  previousCase: "Previous case",
  nextCase: "Next case",
  caseCount: (index, count) => `Case ${index + 1} of ${count}`,
}

export type EvalCompareProps = Omit<React.ComponentProps<"div">, "children"> & {
  /** The input every candidate answered. */
  prompt?: React.ReactNode
  /** A reference answer, shown under the prompt. */
  expected?: React.ReactNode
  /** Two to four outputs, in column order. */
  candidates: EvalCandidate[]
  /** The rows of the scorecard. Without it, no scorecard is shown. */
  metrics?: EvalMetric[]
  /** Whether the word diff is shown. */
  diff?: boolean
  defaultDiff?: boolean
  onDiffChange?: (diff: boolean) => void
  /** The candidate the others are compared against. Defaults to the first. */
  baselineId?: string
  defaultBaselineId?: string
  onBaselineChange?: (id: string) => void
  /** The current preference: a candidate `id`, `"tie"`, or `null`. Passing `onVote` shows the vote. */
  vote?: EvalVote
  onVote?: (vote: EvalVote) => void
  /** With `caseCount` and `onCaseChange`, shows the case switcher. Zero-based. */
  caseIndex?: number
  caseCount?: number
  onCaseChange?: (index: number) => void
  /** Draws an output, for example as Markdown, while the diff is off. */
  renderOutput?: (candidate: EvalCandidate) => React.ReactNode
  labels?: Partial<EvalCompareLabels>
}

const regularText =
  "[font-size:var(--hui-font-size-regular)] [letter-spacing:var(--hui-letter-spacing-regular)] [line-height:var(--hui-line-height-regular)]"
const metaText =
  "[font-size:var(--hui-font-size-small)] [letter-spacing:var(--hui-letter-spacing-small)] [line-height:var(--hui-line-height-small)]"
const secondary = "text-[var(--hui-color-foreground-base-secondary)]"
const hairline = "border-[var(--hui-color-border-base-primary)]"

function formatMetric(metric: EvalMetric, value: number) {
  if (metric.format) return metric.format(value)
  return new Intl.NumberFormat(undefined, { maximumFractionDigits: 2 }).format(value)
}

/** The ids holding the best value in a metric row, or none when the row has no winner. */
function bestIds(metric: EvalMetric, candidates: EvalCandidate[]) {
  if (!metric.better) return new Set<string>()
  const values = candidates
    .map((candidate) => ({ id: candidate.id, value: candidate.metrics?.[metric.key] }))
    .filter((entry): entry is { id: string; value: number } => typeof entry.value === "number" && Number.isFinite(entry.value))
  if (values.length < 2) return new Set<string>()
  const target =
    metric.better === "higher"
      ? Math.max(...values.map((entry) => entry.value))
      : Math.min(...values.map((entry) => entry.value))
  const winners = values.filter((entry) => entry.value === target)
  // Everyone tied is no winner.
  if (winners.length === values.length) return new Set<string>()
  return new Set(winners.map((entry) => entry.id))
}

/**
 * The same prompt's outputs from several models or prompt versions, side by
 * side, with an optional word diff against a baseline, a scorecard, and a
 * preference vote. It shows the results you pass; running the eval is your
 * application's work.
 */
function EvalCompare({
  prompt,
  expected,
  candidates,
  metrics,
  diff: diffProp,
  defaultDiff = false,
  onDiffChange,
  baselineId: baselineProp,
  defaultBaselineId,
  onBaselineChange,
  vote,
  onVote,
  caseIndex,
  caseCount,
  onCaseChange,
  renderOutput,
  labels: labelsProp,
  className,
  ...props
}: EvalCompareProps) {
  const labels = { ...defaultEvalCompareLabels, ...labelsProp }
  const regionId = React.useId()

  const [diffState, setDiffState] = React.useState(defaultDiff)
  const diff = diffProp ?? diffState
  const setDiff = (next: boolean) => {
    if (diffProp === undefined) setDiffState(next)
    onDiffChange?.(next)
  }

  const [baselineState, setBaselineState] = React.useState(defaultBaselineId)
  const requestedBaseline = baselineProp ?? baselineState
  const baseline = candidates.find((candidate) => candidate.id === requestedBaseline) ?? candidates[0]
  const setBaseline = (id: string) => {
    if (baselineProp === undefined) setBaselineState(id)
    onBaselineChange?.(id)
  }

  const [announcement, setAnnouncement] = React.useState("")
  const castVote = (next: EvalVote) => {
    if (!onVote) return
    onVote(next)
    const candidate = candidates.find((entry) => entry.id === next)
    setAnnouncement(next === null ? "" : labels.voted(next === "tie" ? null : candidate?.label ?? next))
  }

  const diffs = React.useMemo(() => {
    const map = new Map<string, WordDiffPart[]>()
    if (!diff || !baseline) return map
    for (const candidate of candidates) {
      if (candidate.id !== baseline.id) map.set(candidate.id, diffWords(baseline.output, candidate.output))
    }
    return map
  }, [diff, baseline, candidates])

  const showCases = caseCount !== undefined && caseCount > 1 && onCaseChange && caseIndex !== undefined
  const hasAssertions = candidates.some((candidate) => candidate.assertions?.length)
  const columns = Math.min(Math.max(candidates.length, 1), 4)

  return (
    <div
      // A named group, not a landmark, so several comparisons on a page stay distinct.
      role="group"
      aria-label={labels.region}
      data-slot="eval-compare"
      className={cn(
        "@container w-full min-w-0 overflow-hidden rounded-[var(--hui-radius-4)] border-[0.5px] bg-[var(--hui-color-background-base-primary)] text-[var(--hui-color-foreground-base-primary)] shadow-[var(--hui-shadow-feather)]",
        hairline,
        regularText,
        className
      )}
      {...props}
    >
      {/* Prompt, case switcher, and diff toggle. */}
      <div
        data-slot="eval-compare-header"
        className={cn("flex flex-col gap-[var(--hui-space-3)] border-b-[0.5px] px-[var(--hui-space-5)] py-[var(--hui-space-4)]", hairline)}
      >
        <div className="flex min-w-0 flex-wrap items-center gap-x-[var(--hui-space-4)] gap-y-[var(--hui-space-2)]">
          <span
            id={regionId}
            className={cn("me-auto [font-weight:var(--hui-font-weight-medium)]", secondary, metaText)}
          >
            {labels.prompt}
          </span>
          {showCases && (
            <div data-slot="eval-compare-cases" className="flex items-center gap-[var(--hui-space-1)]">
              <Button
                variant="ghost"
                size="icon-sm"
                aria-label={labels.previousCase}
                disabled={caseIndex <= 0}
                onClick={() => onCaseChange(caseIndex - 1)}
              >
                <ChevronLeftIcon aria-hidden="true" />
              </Button>
              <span aria-live="polite" className={cn("min-w-[9ch] text-center tabular-nums", secondary, metaText)}>
                {labels.caseCount(caseIndex, caseCount)}
              </span>
              <Button
                variant="ghost"
                size="icon-sm"
                aria-label={labels.nextCase}
                disabled={caseIndex >= caseCount - 1}
                onClick={() => onCaseChange(caseIndex + 1)}
              >
                <ChevronRightIcon aria-hidden="true" />
              </Button>
            </div>
          )}
          {candidates.length > 1 && (
            <Toggle
              size="sm"
              data-slot="eval-compare-diff-toggle"
              pressed={diff}
              onPressedChange={setDiff}
              className={metaText}
            >
              <DiffIcon aria-hidden="true" className="size-3.5" />
              {labels.showDiff}
            </Toggle>
          )}
        </div>
        {prompt !== undefined && (
          <div aria-labelledby={regionId} role="group" className="m-0 min-w-0 whitespace-pre-wrap break-words">
            {prompt}
          </div>
        )}
        {expected !== undefined && (
          <div
            data-slot="eval-compare-expected"
            className={cn(
              "flex min-w-0 gap-[var(--hui-space-3)] rounded-[var(--hui-radius-2)] bg-[var(--hui-color-background-base-secondary)] px-[var(--hui-space-4)] py-[var(--hui-space-3)]",
              metaText
            )}
          >
            <span className={cn("shrink-0 [font-weight:var(--hui-font-weight-medium)]", secondary)}>{labels.expected}</span>
            <span className="min-w-0 whitespace-pre-wrap break-words">{expected}</span>
          </div>
        )}
      </div>

      {/* One column per candidate. Rows line up across columns through subgrid. */}
      <div
        data-slot="eval-compare-columns"
        style={{ "--eval-columns": columns } as React.CSSProperties}
        className="grid grid-cols-1 @lg:grid-cols-[repeat(var(--eval-columns),minmax(11rem,1fr))] @lg:overflow-x-auto"
      >
        {candidates.map((candidate, index) => {
          const isBaseline = diff && candidate.id === baseline?.id
          const parts = diffs.get(candidate.id)
          const passedCount = candidate.assertions?.filter((assertion) => assertion.passed).length ?? 0
          const isPreferred = vote === candidate.id
          return (
            <article
              key={candidate.id}
              aria-labelledby={`${regionId}-${index}`}
              data-slot="eval-compare-candidate"
              data-baseline={isBaseline || undefined}
              data-preferred={isPreferred || undefined}
              className={cn(
                "row-span-4 grid min-w-0 grid-rows-subgrid gap-0 border-b-[0.5px] last:border-b-0 @lg:border-b-0 @lg:border-e-[0.5px] @lg:last:border-e-0",
                hairline,
                isPreferred && "bg-[var(--hui-color-background-base-secondary)]"
              )}
            >
              <header className="flex min-w-0 items-start gap-[var(--hui-space-3)] px-[var(--hui-space-5)] pt-[var(--hui-space-4)]">
                <div className="min-w-0 flex-1">
                  <p
                    id={`${regionId}-${index}`}
                    className="m-0 truncate [font-weight:var(--hui-font-weight-medium)]"
                  >
                    {candidate.label}
                  </p>
                  {candidate.meta && <p className={cn("m-0 truncate", secondary, metaText)}>{candidate.meta}</p>}
                </div>
                {isBaseline ? (
                  <span
                    data-slot="eval-compare-baseline"
                    className={cn(
                      "shrink-0 rounded-[var(--hui-radius-1)] border-[0.5px] px-[var(--hui-space-2)] [font-weight:var(--hui-font-weight-medium)]",
                      hairline,
                      secondary,
                      metaText
                    )}
                  >
                    {labels.baseline}
                  </span>
                ) : diff ? (
                  <Button
                    variant="link"
                    size="xs"
                    className={cn("h-auto shrink-0 px-0", secondary, metaText)}
                    onClick={() => setBaseline(candidate.id)}
                  >
                    {labels.useAsBaseline(candidate.label)}
                  </Button>
                ) : null}
              </header>

              <div data-slot="eval-compare-output" className="min-w-0 px-[var(--hui-space-5)] py-[var(--hui-space-4)]">
                {parts ? (
                  <>
                    <p className="m-0 whitespace-pre-wrap break-words">
                      {parts.map((part, partIndex) =>
                        part.type === "equal" ? (
                          <React.Fragment key={partIndex}>{part.text}</React.Fragment>
                        ) : part.type === "added" ? (
                          <ins
                            key={partIndex}
                            title={labels.added}
                            className="rounded-[3px] bg-[var(--hui-color-background-success-primary)] text-[var(--hui-color-foreground-success-primary-hover)] no-underline [box-decoration-break:clone]"
                          >
                            {part.text}
                          </ins>
                        ) : (
                          <del
                            key={partIndex}
                            title={labels.removed}
                            className="text-[var(--hui-color-foreground-danger-primary)] decoration-[var(--hui-color-foreground-danger-primary)]"
                          >
                            {part.text}
                          </del>
                        )
                      )}
                    </p>
                    <p className={cn("m-0 mt-[var(--hui-space-3)] tabular-nums", secondary, metaText)}>
                      {labels.diffSummary(countWords(parts, "added"), countWords(parts, "removed"))}
                    </p>
                  </>
                ) : renderOutput && !diff ? (
                  renderOutput(candidate)
                ) : (
                  <p className="m-0 whitespace-pre-wrap break-words">{candidate.output}</p>
                )}
              </div>

              <div className="min-w-0 px-[var(--hui-space-5)]">
                {hasAssertions && candidate.assertions?.length ? (
                  <div data-slot="eval-compare-checks" className={cn("border-t-[0.5px] py-[var(--hui-space-3)]", hairline)}>
                    <p className={cn("m-0 mb-[var(--hui-space-2)] tabular-nums", secondary, metaText)}>
                      {labels.checks(passedCount, candidate.assertions.length)}
                    </p>
                    <ul className="m-0 flex list-none flex-col gap-[var(--hui-space-2)] p-0">
                      {candidate.assertions.map((assertion, assertionIndex) => (
                        <li key={assertionIndex} className={cn("flex min-w-0 items-start gap-[var(--hui-space-2)]", metaText)}>
                          {assertion.passed ? (
                            <CheckIcon aria-hidden="true" className="mt-[2px] size-3.5 shrink-0 text-[var(--hui-color-foreground-base-tertiary)]" />
                          ) : (
                            <CircleXIcon aria-hidden="true" className="mt-[2px] size-3.5 shrink-0 text-[var(--hui-color-foreground-danger-primary)]" />
                          )}
                          <span className="min-w-0">
                            <span className="sr-only">{assertion.passed ? labels.passed : labels.failed}: </span>
                            <span className={assertion.passed ? undefined : "text-[var(--hui-color-foreground-danger-primary)]"}>
                              {assertion.label}
                            </span>
                            {assertion.detail && <span className={cn("block", secondary)}>{assertion.detail}</span>}
                          </span>
                        </li>
                      ))}
                    </ul>
                  </div>
                ) : null}
              </div>

              <div className="flex min-w-0 items-end px-[var(--hui-space-5)] pb-[var(--hui-space-4)]">
                {onVote && (
                  <Button
                    variant={isPreferred ? "secondary" : "outline"}
                    size="xs"
                    data-slot="eval-compare-prefer"
                    aria-pressed={isPreferred}
                    onClick={() => castVote(isPreferred ? null : candidate.id)}
                  >
                    {isPreferred ? <CheckIcon aria-hidden="true" /> : <ThumbsUpIcon aria-hidden="true" />}
                    {labels.preferButton}
                    <span className="sr-only"> {candidate.label}</span>
                  </Button>
                )}
              </div>
            </article>
          )
        })}
      </div>

      {metrics && metrics.length > 0 && (
        <EvalScorecard candidates={candidates} metrics={metrics} labels={labels} />
      )}

      {onVote && (
        <div
          data-slot="eval-compare-footer"
          className={cn("flex items-center justify-end gap-[var(--hui-space-3)] border-t-[0.5px] px-[var(--hui-space-5)] py-[var(--hui-space-3)]", hairline)}
        >
          <span aria-live="polite" className={cn("me-auto", secondary, metaText)}>
            {announcement}
          </span>
          <Button
            variant={vote === "tie" ? "secondary" : "ghost"}
            size="xs"
            data-slot="eval-compare-tie"
            aria-pressed={vote === "tie"}
            onClick={() => castVote(vote === "tie" ? null : "tie")}
          >
            <EqualIcon aria-hidden="true" />
            {labels.tie}
          </Button>
        </div>
      )}
    </div>
  )
}

function EvalScorecard({
  candidates,
  metrics,
  labels,
}: {
  candidates: EvalCandidate[]
  metrics: EvalMetric[]
  labels: EvalCompareLabels
}) {
  return (
    <div data-slot="eval-compare-scorecard" className={cn("overflow-x-auto border-t-[0.5px]", hairline)}>
      <table className={cn("w-full border-collapse tabular-nums", metaText)}>
        <caption className="sr-only">{labels.scorecard}</caption>
        <thead>
          <tr className={cn("border-b-[0.5px]", hairline)}>
            <th
              scope="col"
              className={cn("py-[var(--hui-space-3)] ps-[var(--hui-space-5)] pe-[var(--hui-space-4)] text-start [font-weight:var(--hui-font-weight-medium)]", secondary)}
            >
              {labels.scorecard}
            </th>
            {candidates.map((candidate) => (
              <th
                key={candidate.id}
                scope="col"
                className={cn("px-[var(--hui-space-4)] py-[var(--hui-space-3)] text-end [font-weight:var(--hui-font-weight-medium)] last:pe-[var(--hui-space-5)]", secondary)}
              >
                {candidate.label}
              </th>
            ))}
          </tr>
        </thead>
        <tbody>
          {metrics.map((metric) => {
            const winners = bestIds(metric, candidates)
            return (
              <tr key={metric.key} data-slot="eval-compare-metric" className={cn("border-b-[0.5px] last:border-b-0", hairline)}>
                <th
                  scope="row"
                  className={cn("py-[var(--hui-space-3)] ps-[var(--hui-space-5)] pe-[var(--hui-space-4)] text-start [font-weight:var(--hui-font-weight-regular)]", secondary)}
                >
                  {metric.label}
                </th>
                {candidates.map((candidate) => {
                  const value = candidate.metrics?.[metric.key]
                  const isBest = winners.has(candidate.id)
                  return (
                    <td
                      key={candidate.id}
                      data-best={isBest || undefined}
                      className={cn(
                        "px-[var(--hui-space-4)] py-[var(--hui-space-3)] text-end whitespace-nowrap last:pe-[var(--hui-space-5)]",
                        isBest && "[font-weight:var(--hui-font-weight-medium)]"
                      )}
                    >
                      {typeof value === "number" && Number.isFinite(value) ? (
                        <span className="inline-flex items-center justify-end gap-[var(--hui-space-2)]">
                          {isBest && (
                            <span
                              aria-hidden="true"
                              className="size-1.5 rounded-full bg-[var(--hui-color-foreground-base-primary)]"
                            />
                          )}
                          {formatMetric(metric, value)}
                          {isBest && <span className="sr-only"> ({labels.best})</span>}
                        </span>
                      ) : (
                        <span className={secondary}>
                          <span aria-hidden="true">–</span>
                          <span className="sr-only">{labels.noValue}</span>
                        </span>
                      )}
                    </td>
                  )
                })}
              </tr>
            )
          })}
        </tbody>
      </table>
    </div>
  )
}

export { EvalCompare }

```

      
        Update the import paths to match your project setup.
      
    
  


## Usage [#usage]

```tsx
import { EvalCompare, type EvalMetric } from "@/components/ai/eval-compare"

const metrics: EvalMetric[] = [
  { key: "correctness", label: "Correctness", better: "higher" },
  { key: "latency", label: "Latency", better: "lower", format: (ms) => `${ms} ms` },
]

<EvalCompare
  prompt={run.prompt}
  candidates={run.outputs.map((output) => ({
    id: output.model,
    label: output.model,
    output: output.text,
    metrics: output.scores,
  }))}
  metrics={metrics}
  vote={vote}
  onVote={setVote}
/>
```

## Examples [#examples]

### Two prompt versions [#two-prompt-versions]

Differences starts on, so the change between versions is the first thing a reviewer sees.

```tsx
"use client"

import { EvalCompare, type EvalCandidate } from "@/components/ai/eval-compare"

// Two versions of a release-notes prompt, compared word by word.
const candidates: EvalCandidate[] = [
  {
    id: "v1",
    label: "Prompt v1",
    meta: "Current",
    output:
      "We improved search so it is faster and more accurate. Exports now support CSV and Excel. Fixed a bug where some users could not log in.",
  },
  {
    id: "v2",
    label: "Prompt v2",
    meta: "Plain language, no filler",
    output:
      "Search results load twice as fast. Exports now support CSV, Excel, and Google Sheets. Fixed a bug that signed some people out after a password change.",
  },
]

export function EvalCompareDiff() {
  return (
    <EvalCompare
      className="max-w-3xl"
      prompt="Write three lines of release notes from these merged pull requests."
      candidates={candidates}
      defaultDiff
    />
  )
}

```

## API reference [#api-reference]

### EvalCompare [#evalcompare]

Accepts native div props beyond those listed.

| Prop                | Type                                      | Default         | Description                                                  |
| ------------------- | ----------------------------------------- | --------------- | ------------------------------------------------------------ |
| `candidates`        | `EvalCandidate[]`                         | Required        | Two to four outputs, in column order.                        |
| `prompt`            | `ReactNode`                               | None            | The input every candidate answered.                          |
| `expected`          | `ReactNode`                               | None            | A reference answer, shown under the prompt.                  |
| `metrics`           | `EvalMetric[]`                            | None            | The scorecard rows. Without it, no scorecard is shown.       |
| `diff`              | `boolean`                                 | None            | Controls whether Differences is on.                          |
| `defaultDiff`       | `boolean`                                 | `false`         | Whether Differences starts on.                               |
| `onDiffChange`      | `(diff: boolean) => void`                 | None            | Called when Differences is toggled.                          |
| `baselineId`        | `string`                                  | First candidate | Controls the column the others are compared against.         |
| `defaultBaselineId` | `string`                                  | None            | The starting baseline.                                       |
| `onBaselineChange`  | `(id: string) => void`                    | None            | Called when a person picks another baseline.                 |
| `vote`              | `string \| "tie" \| null`                 | None            | The current preference.                                      |
| `onVote`            | `(vote: EvalVote) => void`                | None            | Passing it shows Prefer and Tie.                             |
| `caseIndex`         | `number`                                  | None            | The current case, from zero.                                 |
| `caseCount`         | `number`                                  | None            | How many cases there are.                                    |
| `onCaseChange`      | `(index: number) => void`                 | None            | Passing it with the two above shows the case switcher.       |
| `renderOutput`      | `(candidate: EvalCandidate) => ReactNode` | None            | Draws an output, such as Markdown, while Differences is off. |
| `labels`            | `Partial<EvalCompareLabels>`              | English         | Every visible and announced string.                          |

### Types [#types]

| Export          | Shape                                                                        |
| --------------- | ---------------------------------------------------------------------------- |
| `EvalCandidate` | `id`, `label`, `output`, and optional `meta`, `metrics`, and `assertions`    |
| `EvalMetric`    | `key`, `label`, and optional `better` (`"higher"` or `"lower"`) and `format` |
| `EvalAssertion` | `label`, `passed`, and an optional `detail`                                  |
| `WordDiffPart`  | `type` (`"equal"`, `"added"`, or `"removed"`) and `text`                     |
| `diffWords`     | `(before: string, after: string) => WordDiffPart[]`                          |
