-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathquestion_answer_generation.py
303 lines (261 loc) · 10.6 KB
/
question_answer_generation.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import argparse
import logging
import os.path
import warnings
from functools import partial
from pathlib import Path
import datasets
import torch
from datasets import Dataset, DatasetDict
from sklearn.model_selection import train_test_split
from transformers import AutoModelForCausalLM, AutoTokenizer, PreTrainedModel, PreTrainedTokenizer
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
DEVICE = "cuda:0" if torch.cuda.is_available() else "cpu"
TEST_SIZE = 0.2
QA_MODEL = "Qwen/Qwen2.5-7B-Instruct"
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Generate question answer pairs from the dataset of passages")
parser.add_argument(
"--dataset_path",
type=str,
default=None,
help="dataset path in the local dir. Can be huggingface dataset directory or a csv file.",
required=True,
)
parser.add_argument(
"--title_column_name",
type=str,
default="Title",
help="This title is used to identify passages from the same text",
)
parser.add_argument(
"--passage_column_name",
type=str,
default="Abstract",
help="name of the passage column",
)
parser.add_argument(
"--batch_size",
type=int,
default=1000,
help="Batch size (per device) for generating question answer pairs.",
)
parser.add_argument(
"--sample_size",
type=int,
default=1000,
help="Number of examples to process",
)
parser.add_argument(
"--output_dir",
type=str,
help="Output directory. Without '/' at the end",
required=True,
)
parser.add_argument(
"--as_csv",
action="store_true",
help="Save the files as CSV. If False, will save them as a dataset directory via [`~Dataset.save_to_disk`]",
)
parser.add_argument(
"--max_input_tokens",
type=int,
default=512,
help="Maximum number of input tokens for the model.",
)
args = parser.parse_args()
return args
def generate_question_answer_pairs(
documents: dict, model: PreTrainedModel, tokenizer: PreTrainedTokenizer, passage_column_name: str, max_input_tokens: int
) -> dict:
"""Generate question answer pairs"""
texts = documents[passage_column_name]
example_passage = (
"Dense retrieval models are essential for embedding-based information "
"retrieval systems, as they map queries and documents into a shared "
"embedding space where their relevance can be computed. By using in-batch "
"negative contrastive learning, these models can be trained more efficiently, "
"as each batch contains not only positive examples but also negative samples "
"from unrelated queries or documents. This approach helps optimize the model's "
"ability to retrieve the most relevant information in real-world applications, "
"such as question-answering systems, where precision is critical."
)
example_question = (
"What role does in-batch negative contrastive learning play in training dense "
"retrieval models, particularly in optimizing the retrieval of relevant information "
"across different applications?"
)
prompt_template = (
"Read the following passage and generate a single, relevant question based "
"on its content. The question should be less than 100 words and more than 10 "
"words. Do not generate anything other than the question itself. Avoid any tokens, "
"explanations, or formatting. Do not use words like 'Question:', 'Answer:', 'Example:', or 'Passage:'. "
"Ensure there are no line breaks in the output. The output should be the question only, nothing more.\n\n"
"Example:\nPassage: {example_passage}\n{example_question}\n\nNow, do the same for the next "
"passage:\n{passage}\n"
)
system_message = {
"role": "system",
"content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."
}
batch_messages = [
[system_message, {"role": "user", "content": prompt_template.format(
example_passage=example_passage,
example_question=example_question,
passage=passage)}]
for passage in texts
]
batch_texts = tokenizer.apply_chat_template(
batch_messages,
tokenize=False,
add_generation_prompt=True
)
model_inputs = tokenizer(batch_texts, return_tensors="pt", padding=True, truncation=True, max_length=max_input_tokens).to(model.device)
generated_ids = model.generate(
**model_inputs,
max_new_tokens=512
)
generated_ids = [
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
responses = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
results = []
for response in responses:
question = response.strip()
results.append({"Question": question, "Answer": ""})
return {
"Question": [r["Question"] for r in results],
"Answer": [r["Answer"] for r in results]
}
def filter_malformed_questions(record: dict) -> bool:
question = record["Question"]
return (
question is not None and
question != "" and
question != "-" and
len(question.split()) >= 5 and
not question.startswith("<") and
"instruction" not in question.lower() and
"question" not in question.lower() and
"answer" not in question.lower() and
"Answer:" not in question and
"Question:" not in question
)
def split_dataset(
shuffled_dataset: datasets.Dataset, title_column_name: str, test_size: float = TEST_SIZE
) -> datasets.DatasetDict:
unique_titles = set(shuffled_dataset[title_column_name])
train_titles, test_titles = train_test_split(list(unique_titles), test_size=test_size, random_state=42)
train_dataset = shuffled_dataset.filter(lambda example: example[title_column_name] in train_titles)
test_dataset = shuffled_dataset.filter(lambda example: example[title_column_name] in test_titles)
return datasets.DatasetDict(
{
"train": train_dataset,
"test": test_dataset,
}
)
def generate_qa_from_dataset(
dataset: Dataset, passage_column_name: str, title_column_name: str, sample_size: int, batch_size: int, max_input_tokens: int, load_in_8bit: bool = True
) -> DatasetDict:
logger.info(f"Generating question answer pairs with batch size: {batch_size}")
tokenizer = AutoTokenizer.from_pretrained(QA_MODEL)
model = AutoModelForCausalLM.from_pretrained(QA_MODEL, torch_dtype="auto", device_map="auto")
# shuffle data
dataset.shuffle(seed=42)
# select a subset
num_samples = min(sample_size, len(dataset))
small_dataset = dataset.select(range(num_samples))
# train-test split
small_dataset_splits = split_dataset(small_dataset, title_column_name)
logger.info(
f"Train dataset size: {len(small_dataset_splits['train'])}, "
f"Test dataset size: {len(small_dataset_splits['test'])}"
)
qa_gen_map = partial(
generate_question_answer_pairs, model=model, tokenizer=tokenizer, passage_column_name=passage_column_name, max_input_tokens=max_input_tokens
)
processed_data = small_dataset_splits.map(qa_gen_map, batched=True, batch_size=batch_size)
# Print all questions from the test split before filtering
print("All questions from test split before filtering:")
for i, example in enumerate(processed_data['test']):
print(f"{i + 1}: {example['Question']}")
filtered_data = processed_data.filter(filter_malformed_questions)
# Print all questions from the test split after filtering
# print("All questions from test split after filtering:")
# for i, example in enumerate(filtered_data['test']):
# print(f"{i + 1}: {example['Question']}")
logger.info(
f"Malformed question answer pairs: "
f"(train: {len(processed_data['train']) - len(filtered_data['train'])} "
f"test: {len(processed_data['test']) - len(filtered_data['test'])})"
)
return filtered_data
def _load_dataset_from_path(dataset_path: str) -> Dataset:
if dataset_path.endswith(".csv"):
dataset = Dataset.from_csv(dataset_path)
elif not os.path.splitext(dataset_path)[-1]:
if os.path.isdir(dataset_path):
dataset = datasets.load_from_disk(dataset_path)
else:
dataset = datasets.load_dataset(dataset_path)
if isinstance(dataset, DatasetDict):
if "train" in dataset:
key = "train"
elif "training" in dataset:
key = "training"
else:
key = next(iter(dataset))
warnings.warn(f"Found multiple keys in dataset. Generating qa for split {key}", stacklevel=0)
dataset = dataset[key]
else:
raise ValueError(
"dataset-path must be one of csv, dataset directory "
"(ie saved using [`~Dataset.save_to_disk`] or a dataset on the huggingface hub"
)
return dataset
def generate_qa_from_disk(
dataset_path: str,
passage_column_name: str,
title_column_name: str,
sample_size: int,
batch_size: int,
output_dir: str,
as_csv: bool,
max_input_tokens: int,
) -> None:
dataset = _load_dataset_from_path(dataset_path)
qa_gen_data = generate_qa_from_dataset(dataset, passage_column_name, title_column_name, sample_size, batch_size, max_input_tokens)
output_path = Path(output_dir)
output_path.mkdir(exist_ok=True)
for split_name, split_ds in qa_gen_data.items():
full_path = f"{output_path}/question_answer_pairs_{split_name}"
if as_csv:
full_path = f"{full_path}.csv"
split_ds.to_csv(full_path)
else:
split_ds.save_to_disk(full_path)
logger.info(f"Saving split {split_name} to {full_path}")
def main() -> None:
args = parse_args()
generate_qa_from_disk(
args.dataset_path,
args.passage_column_name,
args.title_column_name,
args.sample_size,
args.batch_size,
args.output_dir,
args.as_csv,
args.max_input_tokens,
)
if __name__ == "__main__":
main()
"""
python question_answer_generation.py \
--dataset_path=knowledge_dataset.csv \
--batch_size=8 \
--sample_size=50 \
--output_dir=out \
--max_input_tokens=512
"""