#!/usr/bin/env python3 """Build the prompt-only DFlash cache from complete rows in a raw JSONL.""" from __future__ import annotations import argparse import json import os from pathlib import Path def _format_livecodebench(row: dict) -> str: prompt = ( "You are an expert Python programmer. You will be given a question " "(problem specification) and will generate a correct Python program " "that matches the specification and passes all tests. You will NOT " "return anything except for the program\n\n" f"### Question:\n{row['question_content']}\n\n" ) if row.get("starter_code"): prompt += ( "### Format: Use the following code structure:\n" f"```python\n{row['starter_code']}\n```\n\n" ) else: prompt += ( "### Format: Write your code in the following format:\n" "```python\n# YOUR CODE HERE\n```\n\n" ) return prompt + "### Answer: (use the provided format with backticks)" def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("--input", type=Path, required=True) parser.add_argument("--output", type=Path, required=True) parser.add_argument("--max-samples", type=int, required=True) args = parser.parse_args() args.output.parent.mkdir(parents=True, exist_ok=True) tmp_path = args.output.with_name(f".{args.output.name}.tmp.{os.getpid()}") count = 0 try: with args.input.open() as source, tmp_path.open("w") as target: for line in source: if count >= args.max_samples: break try: row = json.loads(line) except json.JSONDecodeError: break turns = [_format_livecodebench(row)] target.write(json.dumps({"turns": turns}) + "\n") count += 1 if count != args.max_samples: raise RuntimeError( f"requested {args.max_samples} complete rows, found {count}" ) os.replace(tmp_path, args.output) finally: tmp_path.unlink(missing_ok=True) print(f"[cached] {args.output} ({count} samples)") if __name__ == "__main__": main()