"""Shared emit/validate helpers for gen_* dataset modules. Each gen_.py module defines generator functions returning (state: str, qs: list[question-dict]) where a question-dict is: {"qid": str, # unique within the request "qtype": "choice"|"noul"|"score", "instructions": str, "options": [str, ...], # choice/score option texts; [] for noul "target": [float, ...] or float} # choice/score: distribution over # options (must sum to ~1); noul: P(yes) and exposes FAMILIES = {name: fn} and WEIGHTS = {name: w}. Rules for all generators: - stdlib only (random, math, json, fractions, datetime) - use the module-level `rng = random.Random()` seeded by generate(seed) - closed-form exact q whenever the event is stochastic; soft q allowed for judgment tasks; never leave q ambiguous without intent - keep total request under ~400 tokens - vary surface phrasing — several templates per family """ import json def emit(family, idx, state, qs): questions, targets = {}, {} for q in qs: questions[q["qid"]] = {"type": q["qtype"], "instructions": q["instructions"]} if q["qtype"] in ("choice", "score"): questions[q["qid"]]["criteria"] = { str(j): o for j, o in enumerate(q["options"])} if q["qtype"] == "regress" and q.get("unit"): questions[q["qid"]]["unit"] = q["unit"] targets[q["qid"]] = q["target"] return {"id": f"{family}-{idx}", "family": family, "state": state, "questions": questions, "targets": targets} def q_choice(qid, instructions, options, q): return {"qid": qid, "qtype": "choice", "instructions": instructions, "options": options, "target": q} def q_noul(qid, instructions, q): return {"qid": qid, "qtype": "noul", "instructions": instructions, "options": [], "target": float(q)} def q_score(qid, instructions, levels, q): return {"qid": qid, "qtype": "score", "instructions": instructions, "options": levels, "target": q} def q_regress(qid, instructions, quantiles19, unit=None): """quantiles19: 19 monotone values at taus .05,.10,...,.95.""" return {"qid": qid, "qtype": "regress", "instructions": instructions, "options": [], "target": list(quantiles19), "unit": unit} def validate_row(row): for qid, spec in row["questions"].items(): t = row["targets"][qid] if spec["type"] in ("choice", "score"): n = len(spec["criteria"]) assert len(t) == n, f"{row['id']}:{qid} target len != options" s = sum(t) assert abs(s - 1.0) < 1e-3, f"{row['id']}:{qid} q sums to {s}" assert all(0 <= x <= 1 for x in t) elif spec["type"] == "regress": assert len(t) == 19, f"{row['id']}:{qid} regress target needs 19 quantiles" assert all(t[i] <= t[i + 1] + 1e-9 for i in range(18)), \ f"{row['id']}:{qid} quantiles not monotone" else: assert 0.0 <= t <= 1.0, f"{row['id']}:{qid} noul target {t}" def write_jsonl(rows, path): with open(path, "w", encoding="utf-8") as fh: for r in rows: fh.write(json.dumps(r, ensure_ascii=False) + "\n") def generate(families, weights, n, seed, rng): """Sample n rows. `rng` must be the module's own Random so seeding works.""" rng.seed(seed) fams, ws = zip(*weights.items()) rows, i = [], 0 while len(rows) < n: f = rng.choices(fams, ws)[0] try: state, qs = families[f]() except Exception: continue row = emit(f, i, state, qs) try: validate_row(row) except AssertionError: continue rows.append(row) i += 1 return rows