"""Reasoning conversations for the pool, across all four reasoning strengths. python tools/gen_reasoning.py --scale 8 The problems come from `pipeline/reasoning.py` and `pipeline/reasoning_extra.py`: about twenty families of graphics and numerical problems whose answers are *computed* by the generator, not written by a model. That is what makes them usable as calibration — the arithmetic in the reasoning trace is real. What this file adds is the reasoning-strength axis. The target model takes `Reasoning strength: low|medium|high|xhigh` in its system block and the length of its own reasoning turn follows that setting, so a corpus rendered at one level would calibrate only one of the four behaviours: low no reasoning turn at all; the assistant answers directly medium the opening of the chain, cut at a paragraph boundary high the full chain as generated xhigh the full chain behind an explicit approach-selection preamble Levels are assigned round-robin over distinct problems rather than by rendering one problem four times, which would put four near-identical documents in the pool. """ from __future__ import annotations import argparse import os import random import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) sys.path.insert(0, os.path.join(os.path.dirname(HERE), "pipeline")) import poollib as P SEED = 20260810 GENERATOR_VERSION = "gen_reasoning/1.0" SYSTEM_PROMPTS = [ "You are a careful technical assistant. Work problems out step by step and " "state the assumptions you rely on.", "You are a graphics engineer. Answer with the actual numbers, and say which " "convention you are using when one exists.", "You are a technical assistant. Show the derivation, then give the result. " "If a step depends on a convention, name it.", "You answer engineering questions precisely. Prefer exact values, and flag " "where floating point will bite.", ] LEVELS = ("low", "medium", "high", "xhigh") def _halve(think: str) -> str: """The opening of a chain, cut at a paragraph boundary. A chain cut mid-sentence would be incoherent text, so the cut lands on the last blank line before the halfway mark, and falls back to the whole chain when there is no paragraph structure to cut on. """ paras = think.split("\n\n") if len(paras) < 2: return think target = len(think) // 2 acc, keep = 0, [] for p in paras: if acc and acc + len(p) > target: break keep.append(p) acc += len(p) + 2 return "\n\n".join(keep) if keep else paras[0] def _preamble(topic: str, question: str) -> str: """The approach-selection turn that distinguishes xhigh from high. Assembled from the problem's own topic and text; nothing is invented. """ subject = topic.replace("-", " ") return (f"Before computing: this is a {subject} question, so the result depends on " f"the convention I pick and on where floating point enters. Let me settle " f"both before touching numbers.\n\n" f"What is actually being asked: {question.strip().splitlines()[0]}\n\n" f"Plan: derive the quantity symbolically first so the arithmetic is a " f"substitution rather than the argument, then evaluate, then check the " f"result against the invariant the quantity is supposed to satisfy.") def build_messages(q: str, think: str, answer: str, topic: str, level: str, rng: random.Random) -> list[dict]: system = rng.choice(SYSTEM_PROMPTS) msgs = [{"role": "system", "content": system}, {"role": "user", "content": q}] if level == "low": msgs.append({"role": "assistant", "content": answer}) elif level == "medium": msgs.append({"role": "assistant", "reasoning_content": _halve(think), "content": answer}) elif level == "high": msgs.append({"role": "assistant", "reasoning_content": think, "content": answer}) else: msgs.append({"role": "assistant", "reasoning_content": _preamble(topic, q) + "\n\n" + think, "content": answer}) return msgs def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("--scale", type=int, default=8, help="multiplier on each generator family's instance count") ap.add_argument("--seed", type=int, default=SEED) args = ap.parse_args() import reasoning as R from reasoning_extra import EXTRA_GENERATORS rng = random.Random(args.seed) families = R.GENERATORS + EXTRA_GENERATORS print(f" {len(families)} problem families, scale {args.scale}") problems, seen = [], set() for gen, count in families: for _ in range(count * args.scale): q, think, answer, topic = gen(rng) key = P.sha256_text(q + "\x00" + answer) if key in seen: continue seen.add(key) problems.append((q, think, answer, topic)) print(f" {len(problems)} distinct problems " f"({count * args.scale * len(families) - len(problems)} exact repeats dropped)") records, by_level = [], {} for i, (q, think, answer, topic) in enumerate(problems): level = LEVELS[i % len(LEVELS)] by_level[level] = by_level.get(level, 0) + 1 msgs = build_messages(q, think, answer, topic, level, rng) records.append(P.Record( category="reasoning", domain="reasoning", source=f"synthetic/reasoning:{topic}", license="CC0-1.0 (generated)", path=f"reasoning/{topic}/{level}/{i:05d}", lang="chat", origin="synth_reasoning", messages=msgs, reasoning_strength=level, render="chat", synthetic=True, provenance={"generator": GENERATOR_VERSION, "base": "pipeline/reasoning.py + reasoning_extra.py", "seed": args.seed, "topic": topic, "level": level}, )) print(f" reasoning strength distribution: {dict(sorted(by_level.items()))}") if set(by_level) != set(LEVELS): raise SystemExit(f"coverage gate failed: levels present {sorted(by_level)}") n = P.write_jsonl(os.path.join(P.POOL_ROOT, "reasoning", "synthetic", "conversations.jsonl"), records) print(f" pool/reasoning/synthetic/conversations.jsonl {n} conversations") return 0 if __name__ == "__main__": sys.exit(main())