code / build_harvey_prompts_jsonl.py
anonymous
[code] Reproduction bundle.
2e511b5
Raw
History Blame Contribute Delete
3.52 kB
"""Build the published Harvey prompt-metadata JSONL from the VAULT exports.
Each Harvey VAULT_REVIEW export carries the as-configured per-column prompt in
its question headers ("1. 1. Case ID (<prompt>)"). This script extracts the
value-column header of each of the 12 question blocks (same block layout as
``legex.harvey``) and writes one record per (run, field):
{"model": "harvey", "inference_date": "2026-05-18", "field": ..., "prompt": ...}
``prompts_harvey.jsonl`` holds both dates of the paper run (2026-05-18 and
2026-06-30 — the 30 June re-created tables carry platform-rephrased prompts);
``prompts_harvey_2.jsonl`` holds the 2026-08-05 transparency run.
python build_harvey_prompts_jsonl.py --raw-dir ../data/raw \\
--out-dir inference-results/prompts
"""
import argparse
import json
import re
from pathlib import Path
import openpyxl
from legex.harvey import HARVEY_FIELDS_ORDER
# run -> [(inference_date, export file)]
RUNS: dict[str, tuple[tuple[str, str], ...]] = {
"harvey": (
("2026-05-18", "harvey_2026-05-18.xlsx"),
("2026-06-30", "harvey_2026-06-30.xlsx"),
),
"harvey-2": (("2026-08-05", "harvey_2026-08-05.xlsx"),),
}
_FIRST_ANSWER_COL = 3 # after Name, Folder, Document Classification
_TITLE_PREFIX_RE = re.compile(r"^\s*(?:\d+\.\s*)+")
def _parse_header(header: str) -> str:
"""The prompt is the parenthetical after the column title; parens may nest."""
start = header.find("(")
if start == -1 or not header.rstrip().endswith(")"):
raise ValueError(f"header without a prompt parenthetical: {header[:80]!r}")
prompt = header[start + 1 : header.rindex(")")]
return " ".join(prompt.split())
def read_prompts(xlsx: Path) -> dict[str, str]:
"""Return ``{field: prompt}`` from the value-column headers of one export."""
wb = openpyxl.load_workbook(xlsx, read_only=True)
ws = wb["Sheet1"]f
header = next(ws.iter_rows(min_row=1, max_row=1, values_only=True))
wb.close()
width = (len(header) - _FIRST_ANSWER_COL) // len(HARVEY_FIELDS_ORDER)
if width < 1:
raise ValueError(f"unexpected Harvey sheet width in {xlsx.name}: {len(header)} columns")
out: dict[str, str] = {}
for j, field in enumerate(HARVEY_FIELDS_ORDER):
cell = header[_FIRST_ANSWER_COL + j * width]
out[field] = _parse_header(str(cell))
return out
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
parser.add_argument("--raw-dir", type=Path, default=Path("../data/raw"),
help="Directory holding the harvey_<date>.xlsx exports.")
parser.add_argument("--out-dir", type=Path, default=Path("inference-results/prompts"))
args = parser.parse_args(argv)
args.out_dir.mkdir(parents=True, exist_ok=True)
for run, exports in RUNS.items():
dst = args.out_dir / f"prompts_{run.replace('-', '_')}.jsonl"
n = 0
with dst.open("w", encoding="utf-8") as f:
for inference_date, filename in exports:
for field, prompt in read_prompts(args.raw_dir / filename).items():
record = {"model": run, "inference_date": inference_date,
"field": field, "prompt": prompt}
f.write(json.dumps(record, ensure_ascii=False) + "\n")
n += 1
print(f"wrote {n} prompt record(s) to {dst}")
return 0
if __name__ == "__main__":
raise SystemExit(main())