forked from ChelseaKR/sprout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_conformance_fixtures.py
More file actions
98 lines (83 loc) · 3.59 KB
/
Copy pathgenerate_conformance_fixtures.py
File metadata and controls
98 lines (83 loc) · 3.59 KB
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
95
96
97
98
"""Generate the cross-language conformance fixtures for the TypeScript port (EXP-08).
For every case in ``eval/suites/*.yaml`` (the same question set the Python eval harness
scores against), run the real Python :class:`~sprout.answer.Assistant` and record its
answer. ``web-static/test/conformance.test.ts`` replays every question through the
TypeScript port and asserts byte-identical output — this fixture file *is* the
conformance test's spine (per the ideation shape: "dual-implementation drift is the big
one — the conformance test is the deliverable's spine").
Usage: ``uv run python scripts/generate_conformance_fixtures.py`` (run after
``make ingest``). Writes ``web-static/test/fixtures/conformance.json``.
"""
from __future__ import annotations
import json
from pathlib import Path
import yaml
from sprout.answer import Assistant
from sprout.config import load_config
from sprout.store import VectorStore
ROOT = Path(__file__).resolve().parent.parent
SUITE_DIR = ROOT / "eval" / "suites"
OUT_PATH = ROOT / "web-static" / "test" / "fixtures" / "conformance.json"
def _load_questions() -> list[dict[str, str]]:
cases: list[dict[str, str]] = []
seen_ids: set[str] = set()
for suite_path in sorted(SUITE_DIR.glob("*.yaml")):
data = yaml.safe_load(suite_path.read_text(encoding="utf-8"))
for case in data.get("cases", []):
case_id = case["id"]
if case_id in seen_ids:
# A handful of cases are intentionally duplicated across suites (e.g. a
# safety case mirrored into refusal); keep the fixture set to one entry
# per unique question+language pair.
continue
seen_ids.add(case_id)
cases.append(
{
"id": case_id,
"question": case["question"],
"language": case.get("language", "en"),
"suite": suite_path.stem,
}
)
return cases
def main() -> None:
cfg = load_config(ROOT / "config" / "sprout.yaml")
store = VectorStore.load(cfg.store.path)
assistant = Assistant.from_store(cfg, store)
fixtures = []
for case in _load_questions():
answer = assistant.answer(case["question"], language=case["language"])
fixtures.append(
{
"id": case["id"],
"suite": case["suite"],
"question": case["question"],
"language_requested": case["language"],
"expected": {
"language": answer.language,
"refused": answer.refused,
"refusal_reason": answer.refusal_reason,
"text": answer.text,
"display_text": answer.display_text,
"citations": [c.chunk_id for c in answer.citations],
"confidence": answer.confidence,
"low_confidence": answer.low_confidence,
"abstained": answer.abstained,
"is_safety_query": answer.is_safety_query,
"safety_notice": answer.safety_notice,
"disclosure": answer.disclosure,
"as_of": answer.as_of,
},
}
)
OUT_PATH.parent.mkdir(parents=True, exist_ok=True)
OUT_PATH.write_text(
json.dumps(
{"format_version": 1, "cases": fixtures}, ensure_ascii=False, sort_keys=True, indent=2
)
+ "\n",
encoding="utf-8",
)
print(f"Wrote {len(fixtures)} fixture cases to {OUT_PATH}")
if __name__ == "__main__":
main()