| import re |
| from lighteval.tasks.lighteval_task import LightevalTaskConfig |
| from lighteval.tasks.requests import Doc |
| from lighteval.metrics.metrics import SampleLevelMetric |
|
|
| |
| |
| |
| def numpuzzle_match(predictions: list[str], formatted_doc: dict, **kwargs) -> dict: |
| """ |
| モデルが生成したテキスト(predictions)と、正解(formatted_doc)を比較する関数。 |
| """ |
| |
| target = str(formatted_doc["target"]).strip() |
| target_len = len(target) |
| |
| |
| prediction_raw = str(predictions[0]) |
| |
| |
| prediction_digits = re.sub(r'\D', '', prediction_raw) |
| |
| |
| if len(prediction_digits) >= target_len: |
| final_pred = prediction_digits[-target_len:] |
| else: |
| final_pred = prediction_digits |
| |
| is_match = (final_pred == target) |
| |
| |
| return {"accuracy": 1.0 if is_match else 0.0} |
|
|
| |
| numpuzzle_metric = SampleLevelMetric( |
| metric_name="numpuzzle_accuracy", |
| sample_level_fn=numpuzzle_match, |
| category="accuracy", |
| use_case="generative" |
| ) |
|
|
| |
| |
| |
| def numpuzzle_prompt(line, task_name: str = None): |
| """ |
| データセットの1行(line)を受け取り、モデルに入力するプロンプト形式を作ります。 |
| ※ line['input'] は、あなたのデータセットの「問題文」の列名に合わせて変更してください。 |
| """ |
| return Doc( |
| task_name=task_name, |
| query=f"{line['input']}\nAnswer:", |
| choices=[str(line["target"])], |
| gold_index=0, |
| instruction="", |
| ) |
|
|
| |
| |
| |
| task = LightevalTaskConfig( |
| name="numpuzzle-easy", |
| prompt_function=numpuzzle_prompt, |
| suite=["custom"], |
| hf_repo="your-username/numpuzzle-dataset", |
| hf_subset="default", |
| hf_avail_splits=["test"], |
| evaluation_splits=["test"], |
| few_shots_split=None, |
| few_shots_select=None, |
| generation_size=65536, |
| metric=[numpuzzle_metric], |
| ) |
|
|
| |
| TASKS_TABLE = [task] |