feat: mmlu parser
Browse files- llmdataparser/mmlu_parser.py +81 -0
llmdataparser/mmlu_parser.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from dataclasses import dataclass
|
2 |
+
from typing import Any
|
3 |
+
|
4 |
+
from llmdataparser.base_parser import HuggingFaceDatasetParser, ParseEntry
|
5 |
+
from llmdataparser.prompts import MMLU_SYSTEM_PROMPT
|
6 |
+
|
7 |
+
|
8 |
+
@dataclass(frozen=True)
|
9 |
+
class MMLUParseEntry(ParseEntry):
|
10 |
+
"""
|
11 |
+
Custom entry class for MMLU, with fields specific to this dataset parser.
|
12 |
+
"""
|
13 |
+
|
14 |
+
prompt: str
|
15 |
+
answer_letter: str
|
16 |
+
|
17 |
+
@classmethod
|
18 |
+
def create(cls, prompt: str, answer_letter: str) -> "MMLUParseEntry":
|
19 |
+
if answer_letter not in {"A", "B", "C", "D"}:
|
20 |
+
raise ValueError(
|
21 |
+
f"Invalid answer_letter '{answer_letter}'; must be one of 'A', 'B', 'C', 'D'."
|
22 |
+
)
|
23 |
+
return cls(prompt=prompt, answer_letter=answer_letter)
|
24 |
+
|
25 |
+
|
26 |
+
class MMLUDatasetParser(HuggingFaceDatasetParser[MMLUParseEntry]):
|
27 |
+
_data_source = "cais/mmlu"
|
28 |
+
|
29 |
+
def __init__(self, system_prompt: str = MMLU_SYSTEM_PROMPT):
|
30 |
+
super().__init__() # Properly initialize the base class
|
31 |
+
self.parsed_data: list[MMLUParseEntry] = []
|
32 |
+
self.task_names: list[str] = []
|
33 |
+
self.subject_list: set[str] = set()
|
34 |
+
self.system_prompt: str = system_prompt
|
35 |
+
super().__init__()
|
36 |
+
|
37 |
+
def parse(self, split_names: str | list[str] | None = None, **kwargs: Any) -> None:
|
38 |
+
self.parsed_data.clear()
|
39 |
+
if self.raw_data is None:
|
40 |
+
raise ValueError("No data loaded. Please load the dataset first.")
|
41 |
+
|
42 |
+
if split_names is None:
|
43 |
+
split_names = self.task_names
|
44 |
+
elif isinstance(split_names, str):
|
45 |
+
split_names = [split_names]
|
46 |
+
|
47 |
+
for split_name in split_names:
|
48 |
+
if split_name not in self.task_names:
|
49 |
+
raise ValueError(f"Task '{split_name}' not found in the dataset.")
|
50 |
+
|
51 |
+
dataset_split = self.raw_data[split_name]
|
52 |
+
for index, entry in enumerate(dataset_split, start=1):
|
53 |
+
data_entry = self.process_entry(entry, **kwargs)
|
54 |
+
self._parsed_data.append(data_entry)
|
55 |
+
self.subject_list.add(entry.get("subject", "Unknown"))
|
56 |
+
print(f"Parsed {index} data points from task '{split_name}'.")
|
57 |
+
|
58 |
+
print(
|
59 |
+
f"Number of subjects: {len(self.subject_list)}. "
|
60 |
+
"For more details, please check the `self.subject_list` attribute."
|
61 |
+
)
|
62 |
+
|
63 |
+
def process_entry(self, row: dict[str, Any], **kwargs) -> MMLUParseEntry:
|
64 |
+
"""
|
65 |
+
Generate a prompt and expected answer from the given row.
|
66 |
+
|
67 |
+
Args:
|
68 |
+
row (dict[str, Any]): A data point to be formatted.
|
69 |
+
|
70 |
+
Returns:
|
71 |
+
MMLUParseEntry: The formatted entry object.
|
72 |
+
"""
|
73 |
+
choices = "\n".join(
|
74 |
+
f"{chr(65 + i)}. {choice}" for i, choice in enumerate(row["choices"])
|
75 |
+
)
|
76 |
+
prompt = (
|
77 |
+
f"{self.system_prompt}\nQuestion: {row['question']}\n{choices}\nAnswer:"
|
78 |
+
)
|
79 |
+
answer_letter = chr(65 + row["answer"]) # Convert index to 'A', 'B', 'C', 'D'
|
80 |
+
|
81 |
+
return MMLUParseEntry.create(prompt, answer_letter)
|