CoverageCheck / web /src /components /evals /ExampleCompare.tsx
TBM99's picture
Deploy coverage-check 3559250
23d418f verified
Raw History Blame Contribute Delete
4.33 kB
import { useEffect, useMemo, useState } from 'react'
import { AnswerBody } from '@/components/chat/AnswerBody'
import { SERIES, ORDER } from '@/lib/chart'
import { isGood, outcomeLabel } from '@/lib/format'
import { idle, reduce } from '@/lib/run'
import type { AskEvent, ExampleDetail, ExampleEntry } from '@/lib/types'
import { cn } from '@/lib/utils'
/** The recorded held-out answers, A | B | C side by side for one question: each run's
* events folded through the chat's own reducer, so it shows exactly what the chat
* would (trace, answer, sources), plus how the LLM grader graded it and why, under the
* reference answer. Questions chosen by a fixed rule (web/export.py), C's failures
* included. */
export function ExampleCompare({ examples }: { examples: ExampleEntry[] }) {
const [selected, setSelected] = useState<string | null>(null)
const [detail, setDetail] = useState<ExampleDetail | null>(null)
const id = selected ?? examples[0]?.id ?? null
useEffect(() => {
if (!id) return
let current = true // a slower, older fetch must not replace the newer pick
fetch(`/data/examples/${id}.json`)
.then((r) => (r.ok ? r.json() : null))
.then((d: ExampleDetail | null) => current && setDetail(d))
.catch(() => current && setDetail(null))
return () => {
current = false
}
}, [id])
const runs = useMemo(
() => (detail ? Object.fromEntries(ORDER.map((s) => [s, (detail.runs[s].events as AskEvent[]).reduce(reduce, idle)])) : null),
[detail],
)
return (
<div className="space-y-4">
<div className="flex flex-wrap gap-1.5" role="tablist" aria-label="Example question">
{examples.map((e) => (
<button
key={e.id}
role="tab"
aria-selected={e.id === id}
onClick={() => setSelected(e.id)}
title={e.question}
className={cn(
'rounded-full border px-3 py-1 text-xs',
e.id === id ? 'border-primary bg-accent font-medium text-accent-foreground' : 'text-muted-foreground hover:bg-accent',
)}
>
{e.id} 路 {e.label}
</button>
))}
</div>
{detail && runs && detail.id === id && (
<div className="space-y-4">
<div className="rounded-xl bg-muted p-4">
<p className="text-xs text-muted-foreground">
{detail.category} question 路 {detail.n_docs > 0 ? `needs ${detail.n_docs} document${detail.n_docs > 1 ? 's' : ''}` : 'not answerable from the documents'}{' '}
路 policy date {detail.policy_date ?? `none stated (run on ${detail.day})`}
</p>
<p className="mt-1 text-[15px] font-medium">{detail.question}</p>
<details className="mt-2 text-sm">
<summary className="cursor-pointer text-xs font-medium text-primary">Reference answer (held-out set, LLM-written)</summary>
<p className="mt-1 whitespace-pre-wrap">{detail.reference.answer}</p>
<p className="mt-1 text-xs break-words text-muted-foreground">Sources: {detail.reference.sources.join(', ') || 'none (unanswerable)'}</p>
</details>
</div>
<div className="grid gap-4 lg:grid-cols-3">
{ORDER.map((s) => {
const r = detail.runs[s]
return (
<div key={s} className="min-w-0 space-y-2 rounded-xl border p-4">
<div className="flex items-center justify-between gap-2">
<span className="flex items-center gap-1.5 text-sm font-medium">
<span className="size-2.5 rounded-sm" style={{ background: SERIES[s] }} aria-hidden />
System {s.toUpperCase()}
</span>
<span className={cn('rounded-full px-2 py-0.5 text-xs font-medium', isGood(r.outcome) ? 'bg-emerald-50 text-emerald-800' : 'bg-red-50 text-red-800')}>
{outcomeLabel(r.outcome)}
</span>
</div>
{r.note && <p className="text-xs text-muted-foreground">Grader: {r.note}</p>}
<AnswerBody run={runs[s]} lastEventAt={0} recorded />
</div>
)
})}
</div>
</div>
)}
</div>
)
}