File size: 2,860 Bytes
396e441
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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())