feat(llm): eval-tags scores a golden split per docket with set thresholds
Some checks failed
CI / lint (push) Successful in 1m29s
CI / notebooks-smoke (push) Successful in 2m8s
Deploy / changes (push) Successful in 20s
CI / test (push) Successful in 4m40s
Infra CI / zotero (push) Successful in 17s
Infra CI / notebooks (push) Successful in 54s
Infra CI / docs (push) Successful in 45s
Infra CI / api (push) Successful in 1m37s
Infra CI / bcda (push) Successful in 2m4s
Infra CI / llm (push) Successful in 1m19s
Infra CI / mc (push) Successful in 46s
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Successful in 1m37s
Deploy / notebooks (push) Has started running
Deploy / report (push) Has been cancelled
Deploy / llm (push) Has been cancelled
Deploy / bcda (push) Has been cancelled
Deploy / mc (push) Has been cancelled
Deploy / api (push) Has been cancelled

Golden v2 spans dockets with a tune/heldout split and off-vocabulary
entries (expected: [], so abstaining is right). eval-tags gains
--split, --docket, --min-f1 and --min-precision, and prints per-docket
F1 when the golden set spans dockets. The v1 gate is unchanged by
default; the design's held-out gate is --split heldout --min-f1 0.75
--min-precision 0.6.
This commit is contained in:
kert
2026-11-11 14:49:00 -05:00
parent 39e0800d51
commit 7ae3b02682
3 changed files with 112 additions and 8 deletions

View File

@@ -365,6 +365,14 @@ def eval_tags(
"--dump",
help="With --live: write every comment's shortlist, verdicts and confidences to this JSON file.",
),
split: str = typer.Option(
"all", "--split", help="Golden v2 half to score: all | tune | heldout."
),
docket: str = typer.Option("", "--docket", help="Score one docket's entries."),
min_f1: float = typer.Option(0.70, "--min-f1", help="Gate: micro-F1 floor."),
min_precision: float = typer.Option(
0.50, "--min-precision", help="Gate: per-theme precision floor (support >= 5)."
),
) -> None:
"""Score the theme tagger against the golden set (P35 gate, #577):
per-theme precision/recall, micro-F1 and the abstain rate, then the
@@ -379,11 +387,14 @@ def eval_tags(
live_predictions,
load_golden,
score,
select,
stored_predictions,
)
from llm.vocab import load as load_vocab
g = load_golden(golden)
if split not in ("all", "tune", "heldout"):
raise typer.BadParameter("--split is all, tune or heldout")
g = select(load_golden(golden), split=split, docket=docket)
v = load_vocab()
if g.vocab_version != v.version:
typer.echo(
@@ -435,8 +446,19 @@ def eval_tags(
from llm.tageval import Golden
report = score(Golden(g.vocab_version, g.docket, tuple(entries)), preds)
reasons = gate(report)
reasons = gate(report, min_f1=min_f1, min_precision=min_precision)
typer.echo(format_report(report, reasons=reasons))
dockets = sorted({e.docket for e in entries if e.docket})
if len(dockets) > 1:
for d in dockets:
sub = score(
Golden(g.vocab_version, d, tuple(e for e in entries if e.docket == d)),
preds,
)
typer.echo(
f" {d:16s} n={sub.scored:3d} F1 {sub.f1:.2f} "
f"P {sub.precision:.2f} R {sub.recall:.2f} abstain {sub.abstain_rate:.0%}"
)
if reasons:
raise typer.Exit(1)

View File

@@ -34,6 +34,12 @@ class GoldenEntry:
comment_id: str
expected: tuple[str, ...]
acceptable: tuple[str, ...] = ()
#: v2 golden: the entry's docket, its half (``tune``/``heldout``), and
#: the topic when no vocabulary theme fits (``expected`` is then empty,
#: so abstaining is the right answer).
docket: str = ""
split: str = ""
off_vocab: str = ""
@dataclass(frozen=True)
@@ -108,6 +114,9 @@ def load_golden(path: Path | str) -> Golden:
comment_id=str(e["comment_id"]),
expected=tuple(e.get("expected") or []),
acceptable=tuple(e.get("acceptable") or []),
docket=str(e.get("docket") or data.get("docket") or ""),
split=str(e.get("split") or ""),
off_vocab=str(e.get("off_vocab") or ""),
)
for e in data.get("entries") or []
)
@@ -116,6 +125,18 @@ def load_golden(path: Path | str) -> Golden:
)
def select(golden: Golden, *, split: str = "all", docket: str = "") -> Golden:
"""The entries of one ``split`` (``all``, ``tune``, ``heldout``) and/or
one ``docket``. The gate reads only ``heldout``; thresholds are tuned
on ``tune``."""
entries = tuple(
e
for e in golden.entries
if (split == "all" or e.split == split) and (not docket or e.docket == docket)
)
return Golden(golden.vocab_version, docket or golden.docket, entries)
def score(golden: Golden, predictions: Mapping[str, Sequence[str] | None]) -> Report:
"""*predictions*: comment id → predicted slugs, or ``None`` when the
comment could not be scored (not indexed / not found) — those are
@@ -143,18 +164,25 @@ def score(golden: Golden, predictions: Mapping[str, Sequence[str] | None]) -> Re
return Report(len(golden.entries), scored, tuple(missing), per, abstained)
def gate(report: Report) -> list[str]:
"""Every reason the gate fails (empty = pass)."""
def gate(
report: Report,
*,
min_f1: float = MIN_MICRO_F1,
min_precision: float = MIN_THEME_PRECISION,
) -> list[str]:
"""Every reason the gate fails (empty = pass). Defaults are the v1
gate (#577); the v2 held-out gate passes ``min_f1=0.75,
min_precision=0.6``."""
reasons: list[str] = []
if report.scored == 0:
return ["nothing scored"]
if report.f1 < MIN_MICRO_F1:
reasons.append(f"micro-F1 {report.f1:.2f} < {MIN_MICRO_F1:.2f}")
if report.f1 < min_f1:
reasons.append(f"micro-F1 {report.f1:.2f} < {min_f1:.2f}")
for s in sorted(report.per_slug.values(), key=lambda s: s.slug):
p = s.precision
if s.support >= MIN_SUPPORT and p is not None and p < MIN_THEME_PRECISION:
if s.support >= MIN_SUPPORT and p is not None and p < min_precision:
reasons.append(
f"{s.slug}: precision {p:.2f} < {MIN_THEME_PRECISION:.2f} (support {s.support})"
f"{s.slug}: precision {p:.2f} < {min_precision:.2f} (support {s.support})"
)
return reasons

View File

@@ -148,3 +148,57 @@ class TestPredictionSources:
)
assert live == {"D-1": ["care-management"], "D-2": [], "D-9": None}
s.close()
class TestGoldenV2:
"""Multi-docket golden with a tune/heldout split (2026-11-11 design)."""
def _golden(self, tmp_path):
from llm.tageval import load_golden
p = tmp_path / "g2.yaml"
p.write_text(
"vocab_version: 2\n"
"entries:\n"
" - {comment_id: A-1, docket: A, split: heldout, expected: [telehealth]}\n"
" - {comment_id: A-2, docket: A, split: tune, expected: [drugs]}\n"
" - {comment_id: B-1, docket: B, split: heldout, expected: [], off_vocab: price transparency}\n"
)
return load_golden(p)
def test_loads_docket_split_and_off_vocab(self, tmp_path):
g = self._golden(tmp_path)
e = {x.comment_id: x for x in g.entries}
assert e["A-1"].docket == "A" and e["A-1"].split == "heldout"
assert e["B-1"].off_vocab == "price transparency" and e["B-1"].expected == ()
def test_split_filter(self, tmp_path):
from llm.tageval import select
g = self._golden(tmp_path)
assert [e.comment_id for e in select(g, split="heldout").entries] == [
"A-1",
"B-1",
]
assert [e.comment_id for e in select(g, split="all").entries] == [
"A-1",
"A-2",
"B-1",
]
assert [e.comment_id for e in select(g, docket="A").entries] == ["A-1", "A-2"]
def test_abstaining_on_off_vocab_is_not_an_error(self, tmp_path):
from llm.tageval import score, select
g = select(self._golden(tmp_path), split="heldout")
r = score(g, {"A-1": ["telehealth"], "B-1": []})
assert r.f1 == 1.0 and r.fp == 0
def test_gate_thresholds_are_parameters(self, tmp_path):
from llm.tageval import gate, score
g = self._golden(tmp_path)
r = score(g, {"A-1": ["telehealth"], "A-2": ["telehealth"], "B-1": []})
assert round(r.f1, 2) == 0.5
assert gate(r) and gate(r, min_f1=0.75)
assert gate(r, min_f1=0.5) == []