File size: 4,328 Bytes
23d418f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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>
  )
}