#!/usr/bin/env python3 """Build the prompt sets from the cached GSM8K (offline). prompts.jsonl N test questions with their gold answers prefix.txt a few-shot prefix of worked train examples (~3,000 tokens), shared by every request of the prefix-cache runs """ import json, os, sys os.environ.setdefault("HF_DATASETS_OFFLINE", "1") from datasets import load_dataset n = int(sys.argv[1]) if len(sys.argv) > 1 else 40 out = sys.argv[2] if len(sys.argv) > 2 else "." test = load_dataset("openai/gsm8k", "main", split="test") train = load_dataset("openai/gsm8k", "main", split="train") with open(f"{out}/prompts.jsonl", "w") as f: for i in range(n): q, a = test[i]["question"], test[i]["answer"] f.write(json.dumps({"id": f"t{i:03d}", "answer": a.split("####")[-1].strip().replace(",", ""), "prompt": q + "\nSolve it step by step, then give the final answer as a number on the last line."}) + "\n") parts = ["You solve grade-school math problems. Worked examples follow.\n"] chars = 0 for i in range(200): ex = f"\nProblem: {train[i]['question']}\nSolution: {train[i]['answer'].replace('####', 'Final answer:')}\n" parts.append(ex) chars += len(ex) if chars > 12000: # ~3,000 tokens break open(f"{out}/prefix.txt", "w").write("".join(parts)) print("prompts", n, "prefix chars", chars)