| from __future__ import annotations |
|
|
| import argparse |
| import json |
| import random |
| from pathlib import Path |
|
|
|
|
| SYSTEM_PROMPT = ( |
| "Solve exact-answer reasoning problems. " |
| "Inside the think block, reason in pseudocode only. " |
| "Do not use English prose inside the think block. " |
| "After thinking, end with exactly one line formatted as ANSWER: <answer>." |
| ) |
|
|
|
|
| USER_INSTRUCTION = ( |
| "Inside the think block, reason in pseudocode only. " |
| "Do not use English prose inside the think block. " |
| "Prefer lines like `x = 51`, `x = x + 34`, `state -> x = 85`." |
| ) |
|
|
|
|
| def parse_args() -> argparse.Namespace: |
| parser = argparse.ArgumentParser() |
| parser.add_argument("--output-dir", required=True) |
| parser.add_argument("--train", type=int, default=2000) |
| parser.add_argument("--valid", type=int, default=200) |
| parser.add_argument("--test", type=int, default=200) |
| parser.add_argument("--seed", type=int, default=20260406) |
| return parser.parse_args() |
|
|
|
|
| def make_arithmetic_task(rng: random.Random, task_id: str) -> dict[str, str]: |
| a = rng.randint(14, 49) |
| b = rng.randint(5, 17) |
| c = rng.randint(3, 11) |
| d = rng.randint(2, 9) |
| e = rng.randint(6, 15) |
| start = rng.randint(20, 80) |
| s1 = start + a |
| s2 = s1 + b |
| s3 = s2 * c |
| sub = d * e |
| answer = s3 - sub |
| prompt = ( |
| f"Start with {start}. Add {a}. Add {b}. Multiply the result by {c}. " |
| f"Subtract {d} times {e}. What number do you get?" |
| ) |
| think = "\n".join( |
| [ |
| f"x = {start}", |
| f"x = x + {a} -> {s1}", |
| f"x = x + {b} -> {s2}", |
| f"x = x * {c} -> {s3}", |
| f"y = {d} * {e} -> {sub}", |
| f"x = x - y -> {answer}", |
| ] |
| ) |
| return {"id": task_id, "kind": "arithmetic", "prompt": prompt, "answer": str(answer), "think": think} |
|
|
|
|
| def make_state_task(rng: random.Random, task_id: str) -> dict[str, str]: |
| base_x = rng.randint(2, 8) |
| base_y = rng.randint(3, 9) |
| base_z = rng.randint(4, 10) |
| x = base_x |
| y = base_y |
| z = base_z |
| steps: list[str] = [ |
| f"x = {base_x}", |
| f"y = {base_y}", |
| f"z = {base_z}", |
| ] |
|
|
| x = x + y |
| steps.append(f"x = x + y -> {x}") |
| y = y * 2 |
| steps.append(f"y = y * 2 -> {y}") |
| z = z + x - 1 |
| steps.append(f"z = z + x - 1 -> {z}") |
| x = x * z |
| steps.append(f"x = x * z -> {x}") |
| y = y + z |
| steps.append(f"y = y + z -> {y}") |
| answer = x - y |
| steps.append(f"result = x - y -> {answer}") |
|
|
| prompt = ( |
| "Run this exact program and report the final value of x - y.\n\n" |
| f"x = {base_x}\n" |
| f"y = {base_y}\n" |
| f"z = {base_z}\n" |
| "x = x + y\n" |
| "y = y * 2\n" |
| "z = z + x - 1\n" |
| "x = x * z\n" |
| "y = y + z" |
| ) |
| think = "\n".join(steps) |
| return {"id": task_id, "kind": "state", "prompt": prompt, "answer": str(answer), "think": think} |
|
|
|
|
| def make_task(rng: random.Random, task_id: str) -> dict[str, str]: |
| if rng.random() < 0.5: |
| return make_arithmetic_task(rng, task_id) |
| return make_state_task(rng, task_id) |
|
|
|
|
| def make_record(task: dict[str, str]) -> dict: |
| assistant = f"<think>\n{task['think']}\n</think>\n\nANSWER: {task['answer']}" |
| user = f"{USER_INSTRUCTION}\n\nProblem:\n{task['prompt']}" |
| return { |
| "messages": [ |
| {"role": "system", "content": SYSTEM_PROMPT}, |
| {"role": "user", "content": user}, |
| {"role": "assistant", "content": assistant}, |
| ] |
| } |
|
|
|
|
| def write_split(path: Path, count: int, seed: int) -> None: |
| rng = random.Random(seed) |
| with path.open("w", encoding="utf-8") as handle: |
| for index in range(count): |
| task = make_task(rng, f"{path.stem}-{index:05d}") |
| handle.write(json.dumps(make_record(task)) + "\n") |
|
|
|
|
| def main() -> None: |
| args = parse_args() |
| output_dir = Path(args.output_dir) |
| output_dir.mkdir(parents=True, exist_ok=False) |
| write_split(output_dir / "train.jsonl", args.train, args.seed) |
| write_split(output_dir / "valid.jsonl", args.valid, args.seed + 1) |
| write_split(output_dir / "test.jsonl", args.test, args.seed + 2) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|