reza2ei's picture
Duplicate from reza2ei/Conditional_Causal_Difficulty_for_LLMs
a0d1de2
Raw History Blame Contribute Delete
2.69 kB
#!/usr/bin/env python
"""Pick one cached base-prompt solver solution per job, for
``fragility_report.py --solution-file``.
Reads the completions cache written by ``collect_base_probes_audited.py``
(keys ``sha(format_prompt(base_text)):<sample>``) -- the same base-prompt
samples the text_probe features summarize, so no new solver calls and no
twin is ever evaluated.
``--pick first`` takes sample 0. ``--pick modal`` takes the lowest-index
sample whose extracted answer equals the modal answer across the job's
samples (the solver's typical solution, chosen without looking at the gold
answer or any twin).
Usage:
python scripts/build_solution_texts.py \
--dataset data_audited_multiop_mix/gsmic_adv_m9_multiop_mix_4omini_labeled_pairs.jsonl \
--cache outputs_probes/gsmic_adv_m9_mix_n24/completions_cache.json \
--pick modal --out outputs_probes/gsmic_adv_m9_mix_n24/solutions_modal.json
"""
from __future__ import annotations
import argparse
import json
from collections import Counter
from pathlib import Path
from causal_difficulty.eval.estimands import build_job_targets
from causal_difficulty.eval.loading import load_pairs
from causal_difficulty.llm_cache import extract_number, format_prompt, sha
def main() -> None:
ap = argparse.ArgumentParser()
ap.add_argument("--dataset", required=True)
ap.add_argument("--cache", required=True)
ap.add_argument("--pick", choices=("first", "modal"), default="modal")
ap.add_argument("--style", default="gsm")
ap.add_argument("--min-obs", type=int, default=4)
ap.add_argument("--out", required=True)
args = ap.parse_args()
cache = json.loads(Path(args.cache).read_text())
pairs, _ = load_pairs(args.dataset)
jobs = build_job_targets(pairs, solvable_only=True, min_obs=args.min_obs)
out, n_missing = {}, 0
for j in jobs:
key = sha(format_prompt(j.base_text, style=args.style))
samples = []
s = 0
while f"{key}:{s}" in cache:
samples.append(cache[f"{key}:{s}"])
s += 1
if not samples:
n_missing += 1
continue
if args.pick == "first":
out[j.base_id] = samples[0]
continue
answers = [extract_number(t) for t in samples]
counts = Counter(a for a in answers if a is not None)
modal = counts.most_common(1)[0][0] if counts else None
idx = next((i for i, a in enumerate(answers) if a == modal), 0)
out[j.base_id] = samples[idx]
Path(args.out).write_text(json.dumps(out))
print(f"wrote {len(out)} solutions ({n_missing} jobs without cached samples) -> {args.out}")
if __name__ == "__main__":
main()