File size: 3,204 Bytes
06a6a65 2c47022 06a6a65 2c47022 06a6a65 2c47022 06a6a65 2c47022 06a6a65 2c47022 06a6a65 2c47022 ee5eb1b 2c47022 06a6a65 2c47022 | 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 | import re
from lighteval.tasks.lighteval_task import LightevalTaskConfig
from lighteval.tasks.requests import Doc
from lighteval.metrics.metrics import SampleLevelMetric
# ==========================================
# 1. カスタム評価ロジック(任意の長さに対応)
# ==========================================
def numpuzzle_match(predictions: list[str], formatted_doc: dict, **kwargs) -> dict:
"""
モデルが生成したテキスト(predictions)と、正解(formatted_doc)を比較する関数。
"""
# 正解を取得し、その長さ(桁数)を取得
target = str(formatted_doc["target"]).strip()
target_len = len(target) # ★ここがポイント:9固定ではなく正解の長さに依存させる
# モデルの生成結果(通常はリストの最初の要素)
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)
# 正解なら 1.0、不正解なら 0.0 を返す(これをLighteval全体で平均化してAccuracyを出します)
return {"accuracy": 1.0 if is_match else 0.0}
# 評価関数をLightevalのメトリクスとして登録
numpuzzle_metric = SampleLevelMetric(
metric_name="numpuzzle_accuracy",
sample_level_fn=numpuzzle_match,
category="accuracy",
use_case="generative"
)
# ==========================================
# 2. プロンプト生成関数
# ==========================================
def numpuzzle_prompt(line, task_name: str = None):
"""
データセットの1行(line)を受け取り、モデルに入力するプロンプト形式を作ります。
※ line['input'] は、あなたのデータセットの「問題文」の列名に合わせて変更してください。
"""
return Doc(
task_name=task_name,
query=f"{line['input']}\nAnswer:", # 例: "1+1=?\nAnswer:"
choices=[str(line["target"])],
gold_index=0,
instruction="",
)
# ==========================================
# 3. タスク設定(Lightevalへ登録)
# ==========================================
task = LightevalTaskConfig(
name="numpuzzle-easy",
prompt_function=numpuzzle_prompt,
suite=["custom"],
hf_repo="your-username/numpuzzle-dataset", # ★Hugging Face上のあなたのデータセットIDに変更してください
hf_subset="default",
hf_avail_splits=["test"], # 評価に使用するスプリット(testやtrain)
evaluation_splits=["test"],
few_shots_split=None,
few_shots_select=None,
generation_size=65536, # モデルに生成させる最大トークン数(長すぎると遅くなります)
metric=[numpuzzle_metric], # 上で定義したカスタム評価関数をセット
)
# Lightevalがこのファイルを読み込んだ時にタスクを認識できるようにする
TASKS_TABLE = [task] |