pinball-benchmarks / src /make_promptfoo_tests.py
Zambonilli's picture
lite-100 (#2)
396e441
Raw History Blame Contribute Delete
2.86 kB
import argparse
import csv
import random
from pathlib import Path
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="make_promptfoo_tests",
description=(
"Sample 2 random rows from each data CSV and write them as a "
"promptfoo CSV test file (benchmarks/tests.csv)."
),
)
parser.add_argument(
"--data-dir",
type=Path,
default=Path("data"),
help="Directory containing the source CSV files.",
)
parser.add_argument(
"--output",
type=Path,
default=Path("benchmarks/tests.csv"),
help="Output promptfoo tests.csv path.",
)
parser.add_argument(
"--per-file",
type=int,
default=2,
help="Number of random rows to pull from each source CSV.",
)
parser.add_argument(
"--seed",
type=int,
default=None,
help="Optional random seed for reproducible output.",
)
return parser
def build_expected(answer: str, source_quote: str) -> str:
rubric = (
'Compare the model\'s answer to the reference answer "{answer}". '
'Supporting quote: "{source_quote}". '
"Pass if the answer matches (exact, numeric, or paraphrased equivalent); "
"fail otherwise. One-line reasoning."
).format(answer=answer, source_quote=source_quote)
return f"llm-rubric:{rubric}"
def main(args: argparse.Namespace) -> None:
if args.seed is not None:
random.seed(args.seed)
source_paths = sorted(args.data_dir.glob("*.csv"))
output_name = args.output.name
source_paths = [p for p in source_paths if p.name != output_name]
rows = []
for csv_path in source_paths:
with open(csv_path, mode="r", newline="", encoding="utf-8") as f:
reader = csv.DictReader(f)
file_rows = [dict(row) for row in reader]
picked = random.sample(file_rows, min(args.per_file, len(file_rows)))
rows.extend(picked)
args.output.parent.mkdir(exist_ok=True)
with open(args.output, mode="w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerow(
["__test", "__expected", "answer", "source_quote", "section", "manufacturer", "game"]
)
for row in rows:
writer.writerow(
[
row["question"],
build_expected(row["answer"], row["source_quote"]),
row["answer"],
row["source_quote"],
row["section"],
row["manufacturer"],
row["game"],
]
)
print(f"wrote {len(rows)} tests from {len(source_paths)} files to {args.output}")
if __name__ == "__main__":
main(build_parser().parse_args())