Files
stack/dev/scripts/lex_dump_api.py

85 lines
2.6 KiB
Python

"""Dump one family's policy API slices to files, for the probe's fixture mode.
bash -c 'cd /home/kert/stack && set -a && . ./.env && set +a && \\
uv run --no-sync python dev/scripts/lex_dump_api.py --family HOME-VISITS --out DIR'
Reads through ``llm.policy``'s in-process seam (``load_slice``,
``load_contents``, ``load_fact``: the same bytes the API serves) and writes
DIR/contents.json
DIR/dossier/<section>.json every section, the facts index included
DIR/fact/<id>.json every fact the index lists
``dev/scripts/lex_probe.py --fixture DIR`` serves these to a real Chromium in
place of the API (lex S1 D13). Read-only: it writes nothing but DIR.
"""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
from typing import Any, Callable
#: llm.policy_slices.SECTIONS (a test keeps the two equal)
SECTIONS = (
"header",
"timeline",
"valuation",
"payment",
"politics",
"sources",
"facts",
)
def _write(path: Path, body: Any) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(
json.dumps(body, ensure_ascii=False, separators=(",", ":")), encoding="utf-8"
)
def dump(
out: Path,
family: str,
*,
load_slice: Callable[[str, str], dict[str, Any]],
load_contents: Callable[[], dict[str, Any]],
load_fact: Callable[[str, str], dict[str, Any]],
) -> dict[str, int]:
_write(out / "contents.json", load_contents())
slices = {}
for section in SECTIONS:
slices[section] = load_slice(family, section)
_write(out / "dossier" / f"{section}.json", slices[section])
ids = [f["id"] for f in slices["facts"]["facts"]]
for fid in ids:
_write(out / "fact" / f"{fid}.json", load_fact(family, fid))
return {"sections": len(SECTIONS), "facts": len(ids)}
def main(argv: list[str] | None = None) -> int:
ap = argparse.ArgumentParser(description=__doc__.split("\n", 1)[0])
ap.add_argument("--family", required=True, help="family key, e.g. HOME-VISITS")
ap.add_argument("--out", type=Path, required=True, help="directory to write")
args = ap.parse_args(argv)
from llm import config, policy
cfg = config.load()
n = dump(
args.out,
args.family,
load_slice=lambda family, section: policy.load_slice(cfg, family, section),
load_contents=lambda: policy.load_contents(cfg),
load_fact=lambda family, fid: policy.load_fact(cfg, family, fid),
)
print(f"wrote {args.out}: contents, {n['sections']} sections, {n['facts']} facts")
return 0
if __name__ == "__main__":
sys.exit(main())