File size: 2,018 Bytes
43e7de8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import argparse
import json
from pathlib import Path
from typing import Any, Dict, List

from src.evaluation.schemas import BENCHMARK_INPUT, BENCHMARK_JSONL, expected_ref_from_sample, resolve_benchmark_input, write_jsonl


def prepare_samples(data: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
    out: List[Dict[str, Any]] = []
    for idx, raw in enumerate(data, start=1):
        sample = dict(raw)
        sample_id = str(sample.get("id") or f"q{idx:03d}")
        out.append(
            {
                "id": sample_id,
                "question": str(sample.get("question") or ""),
                "ground_truth": str(sample.get("ground_truth") or ""),
                "reference": str(sample.get("reference") or sample.get("ground_truth") or ""),
                "expected_context_refs": [expected_ref_from_sample(sample)],
                "source_excerpt": str(sample.get("source_excerpt") or ""),
            }
        )
    return out


def prepare_benchmark(input_path: str | Path = BENCHMARK_INPUT, output_path: str | Path = BENCHMARK_JSONL) -> List[Dict[str, Any]]:
    resolved_input = resolve_benchmark_input(input_path)
    data = json.loads(resolved_input.read_text(encoding="utf-8"))
    if not isinstance(data, list):
        raise ValueError("Benchmark input must be a JSON array.")
    samples = prepare_samples([dict(item) for item in data])
    write_jsonl(output_path, samples)
    return samples


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--input", default=str(BENCHMARK_INPUT))
    parser.add_argument("--output", default=str(BENCHMARK_JSONL))
    args = parser.parse_args()

    resolved_input = resolve_benchmark_input(args.input)
    samples = prepare_benchmark(resolved_input, args.output)
    if str(resolved_input) != str(Path(args.input)):
        print(f"Using benchmark input: {resolved_input}")
    print(f"Prepared {len(samples)} samples -> {args.output}")


if __name__ == "__main__":
    main()