-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmath_lighteval_agent.py
More file actions
119 lines (90 loc) · 3.62 KB
/
math_lighteval_agent.py
File metadata and controls
119 lines (90 loc) · 3.62 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
Preprocess the math dataset to json format for agent task
"""
import os
import argparse
import datasets
def remove_boxed(s):
if "\\boxed " in s:
left = "\\boxed "
assert s[: len(left)] == left
return s[len(left) :]
left = "\\boxed{"
assert s[: len(left)] == left
assert s[-1] == "}"
return s[len(left) : -1]
def last_boxed_only_string(string):
idx = string.rfind("\\boxed")
if "\\boxed " in string:
return "\\boxed " + string.split("\\boxed ")[-1].split("$")[0]
if idx < 0:
idx = string.rfind("\\fbox")
if idx < 0:
return None
i = idx
right_brace_idx = None
num_left_braces_open = 0
while i < len(string):
if string[i] == "{":
num_left_braces_open += 1
if string[i] == "}":
num_left_braces_open -= 1
if num_left_braces_open == 0:
right_brace_idx = i
break
i += 1
if right_brace_idx is None:
retval = None
else:
retval = string[idx : right_brace_idx + 1]
return retval
def extract_solution(solution_str):
return remove_boxed(last_boxed_only_string(solution_str))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--input_dir", default=None)
parser.add_argument("--local_dir", default="~/data/math")
args = parser.parse_args()
data_source = "DigitalLearningGmbH/MATH-lighteval"
data_dir = (
"DigitalLearningGmbH/MATH-lighteval"
if args.input_dir is None
else args.input_dir
)
dataset = datasets.load_dataset(data_dir)
train_dataset = dataset["train"]
test_dataset = dataset["test"]
instruction_following = (
"Let's think step by step and output the final answer within \\boxed{}."
"You must use the `mathlighteval_reward` tool after step by step solving the question"
)
# add a row to each data item that represents a unique id
def make_map_fn(split):
def process_fn(example, idx):
question = example.pop("problem")
question = question + " " + instruction_following
answer = example.pop("solution")
solution = extract_solution(answer)
system_prompt = (
"You are a math expert. You are given a question and you need to solve it step by step. "
"Reasoning step by step before any tool call. "
"You should use the `mathlighteval_reward` tool after step by step solving the question, "
"before generate final answer at least once and refine your answer if necessary. "
"Put your final answer within \\boxed{}."
)
data = {
"agent_name": "matheval_agent",
"agent_cfg_path": "template/agent/math_eval.yaml",
"data_source": data_source,
"prompt": [{"role": "system", "content": system_prompt}, {"role": "user", "content": question}],
"ability": "math",
"reward_model": {"style": "rule", "ground_truth": solution},
"extra_info": {"split": split, "index": idx},
}
return data
return process_fn
train_dataset = train_dataset.map(function=make_map_fn("train"), with_indices=True)
test_dataset = test_dataset.map(function=make_map_fn("test"), with_indices=True)
local_dir = args.local_dir
train_dataset.to_json(os.path.join(local_dir, "train_agent.json"))
test_dataset.to_json(os.path.join(local_dir, "test_agent.json"))