"""평가 문항 읽기. 입력은 지시 순응 데이터 JSONL(한 줄에 샘플 하나)이다. 이 도구가 쓰는 필드: sample_id, dialogue_id, turn, split, task_type, messages, reference_response. 문항 = 홀드아웃(split=test_holdout)의 strategy_conditioned_response 샘플. messages 는 정답 발화 직전까지의 대화이고, reference_response 가 정답(선호) 발화다. 세션 = dialogue_id. """ import json from dataclasses import dataclass from pathlib import Path TASK_TYPE = "strategy_conditioned_response" HOLDOUT_SPLIT = "test_holdout" @dataclass(frozen=True) class EvalItem: sample_id: str dialogue_id: str turn: int messages: list reference: str def read_jsonl(path) -> list[dict]: rows = [] with Path(path).open(encoding="utf-8") as f: for line_no, line in enumerate(f, start=1): if not line.strip(): continue try: rows.append(json.loads(line)) except json.JSONDecodeError as e: raise ValueError(f"{path}:{line_no}: JSON 이 아닙니다 ({e.msg})") from e return rows def write_jsonl(path, rows) -> None: with Path(path).open("w", encoding="utf-8") as f: for row in rows: f.write(json.dumps(row, ensure_ascii=False) + "\n") def select_items(samples: list[dict], split: str = HOLDOUT_SPLIT) -> list[EvalItem]: """평가 문항을 고른다.""" items = [] for s in samples: if s.get("task_type") != TASK_TYPE or s.get("split") != split: continue if not s.get("reference_response"): continue items.append(EvalItem(sample_id=s["sample_id"], dialogue_id=s["dialogue_id"], turn=int(s["turn"]), messages=s["messages"], reference=s["reference_response"])) ids = [i.sample_id for i in items] if len(ids) != len(set(ids)): raise ValueError("sample_id 가 중복됩니다") return sorted(items, key=lambda i: (i.dialogue_id, i.turn)) def load_items(path, split: str = HOLDOUT_SPLIT) -> list[EvalItem]: return select_items(read_jsonl(path), split=split)