|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""Prepare Arena Hard (v0.1) benchmark data. |
| 16 | +
|
| 17 | +Downloads the arena-hard-auto v0.1 question set and the single |
| 18 | +``gpt-4-0314`` baseline model-answer file, joins them by ``uid``, and |
| 19 | +writes one row per question with the fields the ``arena_judge`` |
| 20 | +resources server consumes at the top level (``question``, |
| 21 | +``baseline_answer``, ``uid``): |
| 22 | +
|
| 23 | +- Questions: |
| 24 | + https://github.com/lmarena/arena-hard-auto/blob/main/data/arena-hard-v0.1/question.jsonl |
| 25 | +- Baseline (gpt-4-0314): |
| 26 | + https://github.com/lmarena/arena-hard-auto/blob/main/data/arena-hard-v0.1/model_answer/gpt-4-0314.jsonl |
| 27 | +
|
| 28 | +Note: arena-hard v0.1 has no real sub-categories — the upstream |
| 29 | +``category`` field is just the dataset version string ("arena-hard-v0.1"). |
| 30 | +We drop it so ``arena_judge`` falls through to its ``default_category`` |
| 31 | +(``hard_prompt``) and uses the standard arena-hard judge prompt. |
| 32 | +""" |
| 33 | + |
| 34 | +import json |
| 35 | +import urllib.request |
| 36 | +from pathlib import Path |
| 37 | + |
| 38 | + |
| 39 | +BENCHMARK_DIR = Path(__file__).parent |
| 40 | +DATA_DIR = BENCHMARK_DIR / "data" |
| 41 | +OUTPUT_FPATH = DATA_DIR / "arena_hard_benchmark.jsonl" |
| 42 | + |
| 43 | +URL_QUESTIONS = "https://raw.githubusercontent.com/lmarena/arena-hard-auto/main/data/arena-hard-v0.1/question.jsonl" |
| 44 | +URL_BASELINE = ( |
| 45 | + "https://raw.githubusercontent.com/lmarena/arena-hard-auto/main/data/arena-hard-v0.1/model_answer/gpt-4-0314.jsonl" |
| 46 | +) |
| 47 | + |
| 48 | + |
| 49 | +def _extract_answer_text(data: dict) -> str: |
| 50 | + """Extract the assistant answer from a baseline model's JSONL row. |
| 51 | +
|
| 52 | + The arena-hard-auto baseline files use both shapes for the assistant |
| 53 | + ``content``: a plain string or a dict with an ``answer`` key. |
| 54 | + """ |
| 55 | + for msg in data["messages"]: |
| 56 | + if msg["role"] == "assistant": |
| 57 | + content = msg["content"] |
| 58 | + return content["answer"] if isinstance(content, dict) else content |
| 59 | + raise ValueError("No assistant message found in the baseline row.") |
| 60 | + |
| 61 | + |
| 62 | +def prepare() -> Path: |
| 63 | + """Download and write ``arena_hard_benchmark.jsonl``. Returns the path.""" |
| 64 | + DATA_DIR.mkdir(parents=True, exist_ok=True) |
| 65 | + |
| 66 | + print(f"Downloading questions from {URL_QUESTIONS} ...") |
| 67 | + questions_fpath = DATA_DIR / "question.jsonl" |
| 68 | + urllib.request.urlretrieve(URL_QUESTIONS, questions_fpath) |
| 69 | + |
| 70 | + print(f"Downloading baseline from {URL_BASELINE} ...") |
| 71 | + baseline_fpath = DATA_DIR / "baseline_gpt-4-0314.jsonl" |
| 72 | + urllib.request.urlretrieve(URL_BASELINE, baseline_fpath) |
| 73 | + |
| 74 | + # uid -> answer_text |
| 75 | + baseline_answers: dict[str, str] = {} |
| 76 | + with open(baseline_fpath, "r", encoding="utf-8") as fin: |
| 77 | + for line in fin: |
| 78 | + row = json.loads(line) |
| 79 | + baseline_answers[row["uid"]] = _extract_answer_text(row) |
| 80 | + |
| 81 | + count = 0 |
| 82 | + with open(questions_fpath, "r", encoding="utf-8") as fin, open(OUTPUT_FPATH, "w", encoding="utf-8") as fout: |
| 83 | + for line in fin: |
| 84 | + row = json.loads(line) |
| 85 | + # arena-hard-auto stores the prompt under ``prompt`` but the |
| 86 | + # resource server + prompt template expect ``question``. |
| 87 | + row["question"] = row.pop("prompt") |
| 88 | + # arena-hard v0.1 has no real sub-categories — the upstream |
| 89 | + # ``category`` field is just the dataset version string. Drop |
| 90 | + # it so ``arena_judge`` falls through to its ``default_category`` |
| 91 | + # (``hard_prompt``) and uses the standard arena-hard judge prompt. |
| 92 | + row.pop("category", None) |
| 93 | + # Fail loudly if a question's baseline answer is missing — a |
| 94 | + # silent skip would shrink the evaluation set. |
| 95 | + row["baseline_answer"] = baseline_answers[row["uid"]] |
| 96 | + fout.write(json.dumps(row) + "\n") |
| 97 | + count += 1 |
| 98 | + |
| 99 | + print(f"Wrote {count} problems to {OUTPUT_FPATH}") |
| 100 | + return OUTPUT_FPATH |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + prepare() |
0 commit comments