File size: 2,276 Bytes
09af6cc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #!/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()
|