|
| 1 | +# MIT License |
| 2 | + |
| 3 | +# Copyright (c) 2024 The HuggingFace Team |
| 4 | + |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | + |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | + |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | + |
| 24 | +import logging |
| 25 | + |
| 26 | +from aenum import extend_enum |
| 27 | + |
| 28 | +from lighteval.metrics.dynamic_metrics import multilingual_extractive_match_metric |
| 29 | +from lighteval.metrics.metrics import Metrics |
| 30 | +from lighteval.metrics.utils.extractive_match_utils import IndicesExtractionConfig |
| 31 | +from lighteval.tasks.lighteval_task import LightevalTaskConfig |
| 32 | +from lighteval.tasks.requests import Doc |
| 33 | +from lighteval.utils.language import Language |
| 34 | + |
| 35 | + |
| 36 | +logger = logging.getLogger(__name__) |
| 37 | + |
| 38 | + |
| 39 | +ZEROSHOT_QA_INSTRUCTION = """ |
| 40 | +Answer the following multiple-choice question by selecting only one letter: A, B, C, or D. Do not explain your answer. |
| 41 | +""" |
| 42 | + |
| 43 | +ZEROSHOT_QA_USER_PROMPT = ( |
| 44 | + ZEROSHOT_QA_INSTRUCTION |
| 45 | + + """ |
| 46 | +Question: {question} |
| 47 | +
|
| 48 | +Choices: |
| 49 | +{options} |
| 50 | +
|
| 51 | +Answer: |
| 52 | +""" |
| 53 | +) |
| 54 | + |
| 55 | + |
| 56 | +def yourbench_prompt(line, task_name: str = ""): |
| 57 | + options = "\n".join(f"{chr(65 + i)}. {choice}" for i, choice in enumerate(line["choices"])) |
| 58 | + |
| 59 | + gold_raw = line["gold"][0] |
| 60 | + |
| 61 | + if isinstance(gold_raw, str) and gold_raw.strip().isalpha(): |
| 62 | + gold_index = ord(gold_raw.strip().upper()) - ord("A") |
| 63 | + elif isinstance(gold_raw, int): |
| 64 | + gold_index = gold_raw |
| 65 | + else: |
| 66 | + raise ValueError(f"Unexpected gold label format: {gold_raw!r}") |
| 67 | + |
| 68 | + return Doc( |
| 69 | + instruction=ZEROSHOT_QA_INSTRUCTION, |
| 70 | + task_name=task_name, |
| 71 | + query=ZEROSHOT_QA_USER_PROMPT.format(question=line["question"], options=options), |
| 72 | + choices=line["choices"], |
| 73 | + gold_index=gold_index, |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +yourbench_metrics = multilingual_extractive_match_metric( |
| 78 | + language=Language.ENGLISH, |
| 79 | + gold_extraction_target=[IndicesExtractionConfig(prefix_for_extraction="NativeLetters")], |
| 80 | + pred_extraction_target=[IndicesExtractionConfig(prefix_for_extraction="NativeLetters")], |
| 81 | + precision=6, |
| 82 | +) |
| 83 | + |
| 84 | +extend_enum(Metrics, "yourbench_metrics", yourbench_metrics) |
| 85 | + |
| 86 | +yourbench_mcq = LightevalTaskConfig( |
| 87 | + name="HF_TASK_NAME", # noqa: F821 |
| 88 | + suite=["custom"], |
| 89 | + prompt_function=yourbench_prompt, |
| 90 | + hf_repo="HF_DATASET_NAME", # noqa: F821 |
| 91 | + hf_subset="lighteval", |
| 92 | + hf_avail_splits=["train"], |
| 93 | + evaluation_splits=["train"], |
| 94 | + few_shots_split=None, |
| 95 | + few_shots_select=None, |
| 96 | + generation_size=8192, |
| 97 | + metric=[Metrics.yourbench_metrics], |
| 98 | + trust_dataset=True, |
| 99 | + version=0, |
| 100 | +) |
| 101 | + |
| 102 | +TASKS_TABLE = [yourbench_mcq] |
0 commit comments