-
Notifications
You must be signed in to change notification settings - Fork 8
/
create_squad_dataset.py
178 lines (161 loc) · 5.59 KB
/
create_squad_dataset.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import json
from dataclasses import dataclass, field
from textwrap import dedent
from types import SimpleNamespace
from typing import Optional
import yaml
from datasets import DatasetDict, load_dataset
from transformers import HfArgumentParser
@dataclass
class ScriptArguments:
prompt: Optional[str] = field(
default="single_turn",
metadata={"help": "single_turn, multi_turn"},
)
validation_ratio: Optional[float] = field(
default=0.005,
metadata={"help": "Validation ratio"},
)
seed: Optional[int] = field(
default=42,
metadata={"help": "Seed for the random number generator"},
)
config = SimpleNamespace(**yaml.safe_load(open("config.yaml")))
# Llama 3 has several <|reserved_special_token_...|> that could be used instead
REASONING = (
"".join([f"<blah_{i}>" for i in range(config.num_reasoning_tokens)])
if config.multiple_reasoning_tokens
else "".join(["<blah>"] * config.num_reasoning_tokens)
)
NO_RESPONSE = "?"
def is_exact_match(model_answer, answers):
return model_answer is not None and model_answer in answers
def get_single_turn_prompt_and_response(item, all_answers=False):
context = item["context"]
question = item["question"]
answers = item["answers"]["text"]
if len(answers) == 0:
answers = [NO_RESPONSE]
if not all_answers:
answers = answers[0]
answers = json.dumps(answers)
return {
"messages": [
{"role": "system", "content": config.system_prompt},
{
"role": "user",
"content": dedent(
f"""\
Extract from the following context the minimal span word for word that best answers the question. Think step by step and explain your reasoning. Then give the answer in JSON format as follows:
```json
{{
"answer": ...
}}
```
If the answer is not in the context, the answer should be "{NO_RESPONSE}".
Context: {context}
Question: {question}"""
),
},
{
"role": "assistant",
"content": dedent(
f"""\
{REASONING}
```json
{{
"answer": {answers}
}}
```"""
),
},
]
}
def get_multi_turn_prompt_and_response(item, all_answers=False):
context = item["context"]
question = item["question"]
answers = item["answers"]["text"]
if len(answers) == 0:
answer = False
answers = [NO_RESPONSE]
else:
answer = True
if not all_answers:
answers = answers[0]
answers = json.dumps(answers)
return {
"messages": [
{"role": "system", "content": config.system_prompt},
{
"role": "user",
"content": dedent(
f"""\
Does the the following context contain enough information to be able to answer the question? Think step by step and explain your reasoning. Then give the answer in JSON format as follows:
```json
{{
"answer": ... # "yes" or "no"
}}
```
Context: {context}
Question: {question}"""
),
},
{
"role": "assistant",
"content": dedent(
f"""\
{REASONING}
```json
{{
"answer": "{'yes' if answer else 'no'}"
}}
```"""
),
},
{
"role": "user",
"content": dedent(
f"""\
If the question has an answer in the context, extract the minimal span word for word from the context that best answers the question, otherwise the answer should be "{NO_RESPONSE}". Think step by step and explain your reasoning. Then give the answer in JSON format as follows:
```json
{{
"answer": ...
}}
```"""
),
},
{
"role": "assistant",
"content": dedent(
f"""\
{REASONING}
```json
{{
"answer": {answers}
}}
```"""
),
},
]
}
if __name__ == "__main__":
parser = HfArgumentParser(ScriptArguments)
script_args = parser.parse_args_into_dataclasses()[0]
instruction = {
"single_turn": get_single_turn_prompt_and_response,
"multi_turn": get_multi_turn_prompt_and_response,
}[script_args.prompt]
squad_dataset = load_dataset("squad_v2")
dataset = squad_dataset["train"].train_test_split(
test_size=script_args.validation_ratio,
seed=script_args.seed,
)
train_dataset = dataset["train"].map(instruction)
val_dataset = dataset["test"].map(instruction, fn_kwargs={"all_answers": True})
test_dataset = squad_dataset["validation"].map(
instruction, fn_kwargs={"all_answers": True}
)
dataset = DatasetDict(
{"train": train_dataset, "val": val_dataset, "test": test_dataset}
)
dataset.save_to_disk(config.dataset_name)