Files
stack/tests/llm/test_vocab.py
kert 2462685ab8
Some checks failed
CI / lint (push) Successful in 1m32s
CI / notebooks-smoke (push) Successful in 2m46s
Deploy / changes (push) Successful in 35s
CI / test (push) Failing after 8m25s
Infra CI / notebooks (push) Successful in 3m22s
Infra CI / zotero (push) Successful in 51s
Infra CI / docs (push) Successful in 49s
Infra CI / api (push) Successful in 5m16s
Infra CI / bcda (push) Successful in 5m44s
Infra CI / mc (push) Successful in 57s
Deploy / notebooks (push) Has been skipped
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Infra CI / llm (push) Successful in 2m49s
Deploy / api (push) Successful in 2m12s
Deploy / bcda (push) Successful in 2m30s
Deploy / mc (push) Has been skipped
Deploy / llm (push) Successful in 1m51s
Deploy / report (push) Successful in 41s
feat(llm): theme vocabulary v3 for hospital dockets; eval-tags gates held-out v3
Vocab v3 adds six hospital-side themes the golden set showed missing:
opps-asc-payment, ipps-drg-payment, hospital-wage-index,
non-opioid-pain (NOPAIN), hospital-quality-programs and
hospital-conditions-of-participation. Every v2 theme's text is
unchanged and the file declares compatible_with: [2]; the tagger reuses
all stored v2 verdicts and judges a new theme only when it enters a
shortlist, so the corpus need not be re-judged.

Golden v3 is golden v2 relabelled for v3 (50 entries; 3 genuinely
off-topic comments remain). Live, 14b, top 12, floor 0.45:
  v2 held-out F1 0.74 (P 0.95 R 0.61, abstain 23%)
  v3 held-out F1 0.76 (P 0.89 R 0.67, abstain 14%) - gate PASS
  IPPS 0.29 -> 0.70, OPPS 0.27 -> 0.70; tune F1 0.81.
Two new definitions were narrowed after both halves showed the same
false positives (CoP on hospital-quality comments, NOPAIN on
opioid-sparing letters).

stack llm eval-tags with no flags now runs that gate: golden v3,
held-out half, micro-F1 >= 0.75, per-theme precision >= 0.6; a golden
file without halves is scored whole.
2026-11-11 16:08:36 -05:00

129 lines
4.0 KiB
Python

"""llm.vocab — the closed theme vocabulary (#574)."""
from __future__ import annotations
import pytest
from llm.vocab import Theme, Vocab, VocabError, is_llm_tag, load, parse, split_tags
GOOD = """
version: 3
themes:
- slug: telehealth
label: Telehealth
definition: Medicare telehealth services.
synonyms: [telehealth, audio-only]
sections: [Medicare Telehealth Services]
- slug: care-management
definition: Monthly care management codes.
retired: [old-theme]
"""
class TestParse:
def test_reads_themes_and_version(self):
v = parse(GOOD, source="t")
assert isinstance(v, Vocab) and v.version == 3 and len(v) == 2
assert v.slugs == ("telehealth", "care-management")
t = v.get("telehealth")
assert isinstance(t, Theme) and t.synonyms == ("telehealth", "audio-only")
assert (
v.get("care-management").label == "care-management"
) # label defaults to slug
assert v.retired == ("old-theme",) and "telehealth" in v and "nope" not in v
def test_cards_and_tags(self):
v = parse(GOOD)
assert (
v.cards["telehealth"]
== "Telehealth. Medicare telehealth services. Also: telehealth, audio-only."
)
assert (
v.cards["care-management"]
== "care-management. Monthly care management codes."
)
assert (
v.tag("telehealth") == "llm:telehealth"
and v.get("telehealth").tag == "llm:telehealth"
)
@pytest.mark.parametrize(
"text, msg",
[
(
"version: 1\nthemes:\n - slug: Bad Slug\n definition: x\n",
"kebab-case",
),
(
"version: 1\nthemes:\n - slug: a\n definition: ''\n",
"definition is empty",
),
(
"version: 1\nthemes:\n - slug: a\n definition: x\n - slug: a\n definition: y\n",
"duplicate slug",
),
(
"version: 0\nthemes:\n - slug: a\n definition: x\n",
"version must be >= 1",
),
("version: x\nthemes: []\n", "version must be an integer"),
("version: 1\nthemes: []\n", "no themes"),
("- a\n- b\n", "expected a mapping"),
(
"version: 1\nthemes:\n - slug: a\n definition: x\nretired: [a]\n",
"retired slugs still active",
),
],
)
def test_rejects_bad_files(self, text, msg):
with pytest.raises(VocabError, match=msg):
parse(text, source="t")
class TestPackagedVocab:
def test_loads_and_is_sane(self):
v = load()
assert v.version >= 1 and 40 <= len(v) <= 150
assert all(t.definition.endswith(".") for t in v.themes)
assert all(len(t.synonyms) >= 1 for t in v.themes)
for must in (
"telehealth",
"care-management",
"conversion-factor",
"practice-expense",
"palliative-care",
"skin-substitutes",
):
assert must in v
def test_load_from_path(self, tmp_path):
p = tmp_path / "v.yaml"
p.write_text(GOOD)
assert load(p).source == str(p)
class TestTagHelpers:
def test_split(self):
ours, rest = split_tags(
["llm:telehealth", "topic:palliative", "llm:care-management", "docket:x"]
)
assert ours == ["llm:telehealth", "llm:care-management"] and rest == [
"topic:palliative",
"docket:x",
]
assert is_llm_tag("llm:x") and not is_llm_tag("llmx:y")
def test_compatible_with_lists_versions_whose_verdicts_still_hold():
from llm.vocab import parse
v = parse(
"version: 3\ncompatible_with: [2]\nthemes:\n"
" - {slug: telehealth, label: T, definition: d}\n"
)
assert v.compatible_with == (2,)
assert (
parse("version: 2\nthemes:\n - {slug: a, definition: d}\n").compatible_with
== ()
)