SpeedOfMagic commited on
Commit
73599cf
1 Parent(s): e861e55

Upload build_dataset.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. build_dataset.py +611 -0
build_dataset.py ADDED
@@ -0,0 +1,611 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from functools import partial
2
+
3
+ import datasets
4
+
5
+
6
+ def prepare_base(
7
+ dataset, input_column, output_column, prompt=None
8
+ ) -> tuple[list[str], list[str], dict[str, list[str]]]:
9
+ x, y = dataset[input_column], dataset[output_column]
10
+ if prompt:
11
+ for i in range(len(x)):
12
+ x[i] = prompt.format(text=x[i])
13
+ return x, y
14
+
15
+
16
+ def prepare_babi_qa(dataset, input_column, output_column, prompt):
17
+ x, y = [], []
18
+ for inst in dataset:
19
+ inst = inst["story"]
20
+ context = ""
21
+ for text, answer in zip(inst[input_column], inst[output_column]):
22
+ if answer == "":
23
+ context += text + " "
24
+ else:
25
+ x.append(prompt.format(context=context.strip(), question=text))
26
+ y.append(answer)
27
+ return x, y
28
+
29
+
30
+ def prepare_coqa(
31
+ dataset, input_column, output_column, description, prompt, few_shot_prompt, instruct
32
+ ):
33
+ def doc_to_text(doc, prompt, i=0):
34
+ # Given a passage p, the conversation history {q1, a1, . . . qi−1, ai−1}
35
+ # and a question qi, the task is to predict the answer ai
36
+ doc_text = ""
37
+ for q, a in zip(doc["questions"][:i], doc["answers"]["input_text"][:i]):
38
+ doc_text += "\n\n" + prompt.format(question=q, answer=a)
39
+ return doc_text
40
+
41
+ x, y = [], []
42
+ for inst in dataset:
43
+ formatted_description = description.format(story=inst["story"])
44
+ for j, (question, answer) in enumerate(
45
+ zip(inst[input_column], inst[output_column]["input_text"])
46
+ ):
47
+ if instruct:
48
+ assert (
49
+ few_shot_prompt is not None
50
+ ), "separate few_shot_prompt must be provided for instruction mode."
51
+ few_shot_section = doc_to_text(inst, few_shot_prompt, j)
52
+ if few_shot_section != "":
53
+ few_shot_section = (
54
+ "\n\nHere are a few examples of questions and answers:"
55
+ + few_shot_section
56
+ + "\n\nNow answer the following question in the same format.\n\n"
57
+ )
58
+ else:
59
+ few_shot_section = "\n\n"
60
+ else:
61
+ few_shot_section = doc_to_text(inst, prompt, j) + "\n\n"
62
+ formatted_prompt = (
63
+ formatted_description
64
+ + few_shot_section
65
+ + prompt.format(
66
+ question=question,
67
+ answer="",
68
+ )
69
+ )
70
+ x.append(formatted_prompt)
71
+ y.append(answer)
72
+ return x, y
73
+
74
+
75
+ def prepare_mmlu(
76
+ dataset,
77
+ output_column,
78
+ prompt,
79
+ description,
80
+ mmlu_max_subject_size,
81
+ n_shot,
82
+ few_shot_dataset_func,
83
+ few_shot_prompt,
84
+ instruct,
85
+ ):
86
+ import numpy as np
87
+ np.random.seed(1)
88
+
89
+ few_shot_dataset = few_shot_dataset_func()
90
+
91
+ answers = ["A", "B", "C", "D"]
92
+ subjects = np.array(dataset["subject"])
93
+ few_shot_subjects = np.array(few_shot_dataset["subject"])
94
+ x, y = [], []
95
+ for subject in np.unique(subjects):
96
+ formatted_description = description.format(subject=subject.replace("_", " "))
97
+ if n_shot > 0:
98
+ few_shot_subject = few_shot_dataset.select(
99
+ np.argwhere(few_shot_subjects == subject).flatten()
100
+ )
101
+ few_shot_ids = np.random.choice(
102
+ len(few_shot_subject), n_shot, replace=False
103
+ )
104
+ few_shot_data = few_shot_subject.select(few_shot_ids)
105
+ if instruct:
106
+ assert (
107
+ few_shot_prompt is not None
108
+ ), "separate few_shot_prompt must be provided for instruction mode."
109
+ formatted_few_shot_prompt = (
110
+ "Here are a few examples of questions and answers:\n\n"
111
+ )
112
+ for inst in few_shot_data:
113
+ formatted_few_shot_prompt += (
114
+ few_shot_prompt.format(
115
+ choices=inst["choices"],
116
+ question=inst["question"].strip(),
117
+ answer=answers[inst["answer"]],
118
+ )
119
+ + "\n\n"
120
+ )
121
+ formatted_few_shot_prompt += (
122
+ "Now answer the following question in the same format:\n\n"
123
+ )
124
+ else:
125
+ formatted_few_shot_prompt = ""
126
+ for inst in few_shot_data:
127
+ formatted_few_shot_prompt += (
128
+ prompt.format(
129
+ choices=inst["choices"],
130
+ question=inst["question"].strip(),
131
+ answer=answers[inst["answer"]],
132
+ )
133
+ + "\n"
134
+ )
135
+
136
+ subject_data = dataset.select(np.argwhere(subjects == subject).flatten())
137
+
138
+ if len(subject_data) > mmlu_max_subject_size:
139
+ subject_data = subject_data.select(range(mmlu_max_subject_size))
140
+
141
+ for inst in subject_data:
142
+ formatted_prompt = prompt.format(
143
+ choices=inst["choices"],
144
+ question=inst["question"].strip(),
145
+ answer="",
146
+ )
147
+ x.append(
148
+ formatted_description
149
+ + "\n\n"
150
+ + formatted_few_shot_prompt
151
+ + formatted_prompt
152
+ )
153
+ y.append(answers[inst[output_column]])
154
+ return x, y
155
+
156
+
157
+ def prepare_person(dataset, input_column, prompt=""):
158
+ x = dataset[input_column]
159
+ if len(prompt):
160
+ for i in range(len(x)):
161
+ x[i] = prompt.format(text=x[i])
162
+ y = []
163
+ for _ in x:
164
+ y.append("")
165
+ return x, y
166
+
167
+
168
+ def prepare_trivia_qa(
169
+ dataset,
170
+ prompt,
171
+ n_shot,
172
+ few_shot_dataset_func,
173
+ description,
174
+ few_shot_prompt,
175
+ instruct,
176
+ ):
177
+ import numpy as np
178
+ np.random.seed(1)
179
+
180
+ few_shot_dataset = few_shot_dataset_func()
181
+
182
+ x, y = [], []
183
+ formatted_few_shot_prompt = description
184
+ if n_shot > 0:
185
+ few_shot_ids = np.random.choice(len(few_shot_dataset), n_shot, replace=False)
186
+ few_shot_data = few_shot_dataset.select(few_shot_ids)
187
+ if instruct:
188
+ assert (
189
+ few_shot_prompt is not None
190
+ ), "separate few_shot_prompt must be provided for instruction mode."
191
+ formatted_few_shot_prompt += (
192
+ "\n\nHere are a few examples of questions and answers:\n\n"
193
+ )
194
+ for inst in few_shot_data:
195
+ formatted_few_shot_prompt += (
196
+ few_shot_prompt.format(
197
+ question=inst["question"].strip(),
198
+ answer=inst["answer"]["normalized_value"],
199
+ )
200
+ + "\n\n"
201
+ )
202
+ formatted_few_shot_prompt += (
203
+ "Now answer the following question in the same format:\n\n"
204
+ )
205
+ else:
206
+ formatted_few_shot_prompt = ""
207
+ for inst in few_shot_data:
208
+ formatted_few_shot_prompt += (
209
+ prompt.format(
210
+ question=inst["question"].strip(),
211
+ answer=inst["answer"]["normalized_value"],
212
+ )
213
+ + "\n\n"
214
+ )
215
+ else:
216
+ formatted_few_shot_prompt += "\n"
217
+
218
+ for inst in dataset:
219
+ if instruct:
220
+ x.append(
221
+ formatted_few_shot_prompt + prompt.format(question=inst["question"])
222
+ )
223
+ else:
224
+ x.append(
225
+ formatted_few_shot_prompt
226
+ + prompt.format(question=inst["question"], answer="")
227
+ )
228
+ y.append([alias for alias in inst["answer"]["aliases"]])
229
+ return x, y
230
+
231
+
232
+ def prepare_wiki(dataset, input_column, prompt):
233
+ x, y = [], []
234
+ for sample in dataset[input_column]:
235
+ x.append(prompt.format(context=sample["context".strip()]))
236
+ y.append("")
237
+ return x, y
238
+
239
+
240
+ def prepare_wmt(dataset, input_column, output_column, prompt):
241
+ column_lang = {
242
+ "de": "German",
243
+ "fr": "French",
244
+ "en": "English",
245
+ }
246
+ x, y = [], []
247
+ for inst in dataset["translation"]:
248
+ x.append(
249
+ prompt.format(
250
+ source_lang=column_lang[input_column],
251
+ target_lang=column_lang[output_column],
252
+ text=inst[input_column],
253
+ )
254
+ )
255
+ y.append(inst[output_column])
256
+ return x, y
257
+
258
+
259
+ def prepare_allenai(dataset, input_column, output_column):
260
+ x, y = [], []
261
+ for inst in dataset:
262
+ if len(inst[input_column]) <= 1024:
263
+ x.append(inst[input_column])
264
+ y.append(inst[output_column])
265
+ return x, y
266
+
267
+
268
+ def generate_coqa_instruct_config(description, few_shot_prompt):
269
+ return {
270
+ "name": "coqa",
271
+ "train_split": "train",
272
+ "test_split": "validation",
273
+ "prepare_func": partial(
274
+ prepare_coqa,
275
+ input_column="questions",
276
+ output_column="answers",
277
+ description=description,
278
+ prompt="Question: {question}\n",
279
+ few_shot_prompt=few_shot_prompt,
280
+ instruct=True,
281
+ ),
282
+ "is_main_dataset": False,
283
+ }
284
+
285
+
286
+ def generate_mmlu_instruct_config(description, few_shot_prompt):
287
+ return {
288
+ "name": ["cais/mmlu", "all"],
289
+ "train_split": "validation",
290
+ "test_split": "test",
291
+ "prepare_func": partial(
292
+ prepare_mmlu,
293
+ output_column="answer",
294
+ prompt="Q:{question}\nA. {choices[0]}\nB. {choices[1]}\nC. {choices[2]}\nD. {choices[3]}\nAnswer:{answer}",
295
+ description=description,
296
+ mmlu_max_subject_size=100,
297
+ n_shot=5,
298
+ few_shot_dataset_func=partial(
299
+ datasets.load_dataset, path="cais/mmlu", name="all", split="dev"
300
+ ),
301
+ few_shot_prompt=few_shot_prompt,
302
+ instruct=True,
303
+ ),
304
+ "is_main_dataset": False,
305
+ }
306
+
307
+
308
+ def generate_triviaqa_instruct_config(description, few_shot_prompt):
309
+ return {
310
+ "name": ["trivia_qa", "rc.nocontext"],
311
+ "train_split": "train",
312
+ "test_split": "validation",
313
+ "prepare_func": partial(
314
+ prepare_trivia_qa,
315
+ prompt="Question: {question}\n",
316
+ n_shot=5,
317
+ few_shot_dataset_func=partial(
318
+ datasets.load_dataset,
319
+ path="trivia_qa",
320
+ name="rc.nocontext",
321
+ split="train",
322
+ ),
323
+ description=description,
324
+ few_shot_prompt=few_shot_prompt,
325
+ instruct=True,
326
+ ),
327
+ "is_main_dataset": False,
328
+ }
329
+
330
+
331
+ DATASET_CONFIG = {
332
+ "trivia_qa_tiny": {
333
+ "name": "SpeedOfMagic/trivia_qa_tiny",
334
+ "train_split": "train",
335
+ "test_split": "test",
336
+ "prepare_func": partial(
337
+ prepare_base, input_column="question", output_column="answer"
338
+ ),
339
+ },
340
+ "aeslc": {
341
+ "name": "aeslc",
342
+ "train_split": "train",
343
+ "test_split": "test",
344
+ "prepare_func": partial(
345
+ prepare_base,
346
+ input_column="email_body",
347
+ output_column="subject_line",
348
+ # prompt is set but not used in LM-Polygraph for this dataset (bug)
349
+ # prompt="Write a short subject line for the email. Output only the subject line itself.\n\nEmail:\n{text}\n\nSubject line:\n",
350
+ ),
351
+ },
352
+ "babi_qa": {
353
+ "name": ["facebook/babi_qa", "en-10k-qa1"],
354
+ "train_split": "train",
355
+ "test_split": "test",
356
+ "prepare_func": partial(
357
+ prepare_babi_qa,
358
+ input_column="text",
359
+ output_column="answer",
360
+ prompt="Imagine that you are only able to say a single word. Answer the question given a context. You must only output the full name of the location the same way it is mentioned in the text. Do not try to be polite of helpful.\n\nExample:\n\nContext:\nMary moved to the bathroom. John went to the hallway. Daniel went back to the hallway. Sandra moved to the garden. John moved to the office. Sandra journeyed to the bathroom. Mary moved to the hallway. Daniel travelled to the office. John went back to the garden. John moved to the bedroom.\nQuestion:\nWhere is Sandra?\nAnswer:\nbathroom\n\nContext:\n{context}\n\nQuestion:\n{question}\nAnswer:\n",
361
+ ),
362
+ },
363
+ "coqa": {
364
+ "name": "coqa",
365
+ "train_split": "train",
366
+ "test_split": "validation",
367
+ "prepare_func": partial(
368
+ prepare_coqa,
369
+ input_column="questions",
370
+ output_column="answers",
371
+ description="The following are stories and questions about them. Each story is followed by a question and answer to a given question.\n\nStory: {story}",
372
+ prompt="Question: {question}\nAnswer:{answer}",
373
+ few_shot_prompt=None,
374
+ instruct=False,
375
+ ),
376
+ },
377
+ "gsm8k": {
378
+ "name": ["gsm8k", "main"],
379
+ "train_split": "train",
380
+ "test_split": "test",
381
+ "prepare_func": partial(
382
+ prepare_base,
383
+ input_column="question",
384
+ output_column="answer",
385
+ prompt="Q: There are 15 trees in the grove. Grove workers will plant trees in the grove today. After they are done, there will be 21 trees. How many trees did the grove workers plant today?\nA: There are 15 trees originally. Then there were 21 trees after some more were planted. So there must have been 21 - 15 = 6. The answer is 6.\n\nQ: If there are 3 cars in the parking lot and 2 more cars arrive, how many cars are in the parking lot?\nA: There are originally 3 cars. 2 more cars arrive. 3 + 2 = 5. The answer is 5.\n\nQ: Leah had 32 chocolates and her sister had 42. If they ate 35, how many pieces do they have left in total?\nA: Originally, Leah had 32 chocolates. Her sister had 42. So in total they had 32 + 42 = 74. After eating 35, they had 74 - 35 = 39. The answer is 39.\n\nQ: Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. How many lollipops did Jason give to Denny?\nA: Jason started with 20 lollipops. Then he had 12 after giving some to Denny. So he gave Denny 20 - 12 = 8. The answer is 8.\n\nQ: Shawn has five toys. For Christmas, he got two toys each from his mom and dad. How many toys does he have now?\nA: Shawn started with 5 toys. If he got 2 toys each from his mom and dad, then that is 4 more toys. 5 + 4 = 9. The answer is 9.\n\nQ: There were nine computers in the server room. Five more computers were installed each day, from monday to thursday. How many computers are now in the server room?\nA: There were originally 9 computers. For each of 4 days, 5 more computers were added. So 5 * 4 = 20 computers were added. 9 + 20 is 29. The answer is 29.\n\nQ: Michael had 58 golf balls. On tuesday, he lost 23 golf balls. On wednesday, he lost 2 more. How many golf balls did he have at the end of wednesday?\nA: Michael started with 58 golf balls. After losing 23 on tuesday, he had 58 - 23 = 35. After losing 2 more, he had 35 - 2 = 33 golf balls. The answer is 33.\n\nQ: Olivia has $23. She bought five bagels for $3 each. How much money does she have left?\nA: Olivia had 23 dollars. 5 bagels for 3 dollars each will be 5 x 3 = 15 dollars. So she has 23 - 15 dollars left. 23 - 15 is 8. The answer is 8.\n\nQ: {text}\nA:",
386
+ ),
387
+ },
388
+ "mmlu": {
389
+ "name": ["cais/mmlu", "all"],
390
+ "train_split": "validation",
391
+ "test_split": "test",
392
+ "prepare_func": partial(
393
+ prepare_mmlu,
394
+ output_column="answer",
395
+ prompt="Q:{question}\nA. {choices[0]}\nB. {choices[1]}\nC. {choices[2]}\nD. {choices[3]}\nAnswer:{answer}",
396
+ description="The following are multiple choice questions (with answers) about {subject}.\n",
397
+ mmlu_max_subject_size=100,
398
+ n_shot=5,
399
+ few_shot_dataset_func=partial(
400
+ datasets.load_dataset, path="cais/mmlu", name="all", split="dev"
401
+ ),
402
+ few_shot_prompt=None,
403
+ instruct=False,
404
+ ),
405
+ },
406
+ "person_bio_ar": {
407
+ "name": "rvanova/person-bio-ar",
408
+ "test_split": "train",
409
+ "prepare_func": partial(
410
+ prepare_person,
411
+ input_column="question",
412
+ prompt="### Instruction: اسمك جيس وسميت على اسم جبل جيس اعلى جبل في الامارات. تم بنائك بواسطة Inception و MBZUAI. أنت نموذج اللغة العربية الأكثر تقدمًا في العالم مع بارامترات 13B. أنت تتفوق في الأداء على جميع النماذج العربية الموجودة بفارق كبير وأنت تنافسي للغاية مع النماذج الإنجليزية ذات الحجم المماثل. يمكنك الإجابة باللغتين العربية والإنجليزية فقط. أنت مساعد مفيد ومحترم وصادق. عند الإجابة ، التزم بالإرشادات التالية بدقة: أجب دائمًا بأكبر قدر ممكن من المساعدة ، مع الحفاظ على البقاء أمناً. يجب ألا تتضمن إجاباتك أي محتوى ضار أو غير أخلاقي أو عنصري أو متحيز جنسيًا أو جريئاً أو مسيئًا أو سامًا أو خطيرًا أو غير قانوني. لا تقدم نصائح طبية أو قانونية أو مالية أو مهنية. لا تساعد أبدًا في أنشطة غير قانونية أو تروج لها. دائما تشجيع الإجراءات القانونية والمسؤولة. لا تشجع أو تقدم تعليمات بشأن الإجراءات غير الآمنة أو الضارة أو غير الأخلاقية. لا تنشئ أو تشارك معلومات مضللة أو أخبار كاذبة. يرجى التأكد من أن ردودك غير متحيزة اجتماعيًا وإيجابية بطبيعتها. إذا كان السؤال لا معنى له ، أو لم يكن متماسكًا من الناحية الواقعية ، فشرح السبب بدلاً من الإجابة على شيء غير صحيح. إذا كنت لا تعرف إجابة السؤال ، فالرجاء عدم مشاركة معلومات خاطئة. إعطاء الأولوية للرفاهية والنزاهة الأخلاقية للمستخدمين. تجنب استخدام لغة سامة أو مهينة أو مسيئة. حافظ على نبرة محترمة. لا تنشئ أو تروج أو تشارك في مناقشات حول محتوى للبالغين. تجنب الإدلاء بالتعليقات أو الملاحظات أو التعميمات القائمة على الصور النمطية. لا تحاول الوصول إلى معلومات شخصية أو خاصة أو إنتاجها أو نشرها. احترم دائما سرية المستخدم. كن إيجابيا ولا تقل أشياء سيئة عن أي شيء. هدفك الأساسي هو تجنب الاجابات المؤذية ، حتى عند مواجهة مدخلات خادعة. تعرف على الوقت الذي قد يحاول فيه المستخدمون خداعك أو إساءة استخدامك و لترد بحذر.\n\nأكمل المحادثة أدناه بين [|Human|] و [|AI|]:\n### Input: [|Human|] {text}\n### Response: [|AI|]",
413
+ ),
414
+ "is_main_dataset": False,
415
+ },
416
+ "person_bio_en": {
417
+ "name": "rediska0123/person-bio",
418
+ "test_split": "test",
419
+ "prepare_func": partial(
420
+ prepare_person,
421
+ input_column="question",
422
+ ),
423
+ "is_main_dataset": False,
424
+ },
425
+ "person_bio_ru": {
426
+ "name": "rvanova/person-bio",
427
+ "test_split": "test",
428
+ "prepare_func": partial(
429
+ prepare_person,
430
+ input_column="question",
431
+ ),
432
+ "is_main_dataset": False,
433
+ },
434
+ "person_bio_zh": {
435
+ "name": "ruixing76/person-bio-zh",
436
+ "test_split": "train",
437
+ "prepare_func": partial(
438
+ prepare_person,
439
+ input_column="question",
440
+ ),
441
+ "is_main_dataset": False,
442
+ },
443
+ "triviaqa": {
444
+ "name": ["trivia_qa", "rc.nocontext"],
445
+ "train_split": "train",
446
+ "test_split": "validation",
447
+ "prepare_func": partial(
448
+ prepare_trivia_qa,
449
+ prompt="Question: {question}\nAnswer:{answer}",
450
+ n_shot=5,
451
+ few_shot_dataset_func=partial(
452
+ datasets.load_dataset,
453
+ path="trivia_qa",
454
+ name="rc.nocontext",
455
+ split="train",
456
+ ),
457
+ description="",
458
+ few_shot_prompt=None,
459
+ instruct=False,
460
+ ),
461
+ },
462
+ "wiki_bio": {
463
+ "name": "wiki_bio",
464
+ "test_split": "test",
465
+ "prepare_func": partial(
466
+ prepare_wiki,
467
+ input_column="input_text",
468
+ prompt="This is a Wikipedia passage about {context}:\n",
469
+ ),
470
+ },
471
+ "wmt14_deen": {
472
+ "name": ["wmt14", "de-en"],
473
+ "train_split": "train",
474
+ "test_split": "test",
475
+ "prepare_func": partial(
476
+ prepare_wmt,
477
+ input_column="de",
478
+ output_column="en",
479
+ prompt="Here is a sentence in {source_lang} language and its translation in {target_lang} language.\n\nOriginal:\n{text}\nTranslation:\n",
480
+ ),
481
+ "is_main_dataset": False,
482
+ },
483
+ "wmt14_fren": {
484
+ "name": ["wmt14", "fr-en"],
485
+ "train_split": "train",
486
+ "test_split": "test",
487
+ "prepare_func": partial(
488
+ prepare_wmt,
489
+ input_column="fr",
490
+ output_column="en",
491
+ prompt="Here is a sentence in {source_lang} language and its translation in {target_lang} language.\n\nOriginal:\n{text}\nTranslation:\n",
492
+ ),
493
+ "is_main_dataset": False,
494
+ },
495
+ "wmt19_deen": {
496
+ "name": ["wmt19", "de-en"],
497
+ "train_split": "train",
498
+ "test_split": "validation",
499
+ "prepare_func": partial(
500
+ prepare_wmt,
501
+ input_column="de",
502
+ output_column="en",
503
+ prompt="Here is a sentence in {source_lang} language and its translation in {target_lang} language.\n\nOriginal:\n{text}\nTranslation:\n",
504
+ ),
505
+ "is_main_dataset": False,
506
+ },
507
+ "xsum": {
508
+ "name": "xsum",
509
+ "train_split": "train",
510
+ "test_split": "validation",
511
+ "prepare_func": partial(
512
+ prepare_base,
513
+ input_column="document",
514
+ output_column="summary",
515
+ prompt="Here's the text and it's short one-sentence summary.\n\nText:\n{text}\n\nSummary (one sentence):\n",
516
+ ),
517
+ },
518
+ # instruct datasets
519
+ "coqa_ling_1s": generate_coqa_instruct_config(
520
+ description="Here's a short story:\n\n{story} (End of story)\n\nProvide your best guess for the following question based on this story, and describe how likely it is that your guess is correct as one of the following expressions:\n\nAlmost Certain\nHighly Likely\nVery Good Chance\nWe Beleive\nProbably\nProbable\nLikely\nBetter than Even\nAbout Even\nProbably Not\nWe Doubt\nUnlikely\nLittle Chance\nChances Are Slight\nImprobable\nHighly Unlikely\nAlmost No Chance\n\nGive ONLY the guess and your confidence, no other words or explanation. For example:\n\nGuess: <most likely guess, as short as possible; not a complete sentence, just the guess!>\nConfidence: <description of confidence, without any extra commentary whatsoever; just a short phrase!>",
521
+ few_shot_prompt="Question: {question}\nGuess: {answer}\nConfidence: <appropriate level of confidence in this guess>",
522
+ ),
523
+ "coqa_verb_1s_top1": generate_coqa_instruct_config(
524
+ description="Here's a short story:\n\n{story} (End of story)\n\nProvide your best guess and the probability that it is correct (0.0 to 1.0) for the following question. Give ONLY the guess and probability, no other words or explanation. For example:\n\nGuess: <most likely guess, as short as possible; not a complete sentence, just the guess!>\nProbability: <the probability between 0.0 and 1.0 that your guess is correct, without any extra commentary whatsoever; just the probability!>",
525
+ few_shot_prompt="Question: {question}\nGuess: {answer}\nProbability: <number between 0.0 and 1.0 reflecting confidence in the guess>",
526
+ ),
527
+ "coqa_verb_1s_topk": generate_coqa_instruct_config(
528
+ description="Here's a short story:\n\n{story} (End of story)\n\nProvide your ${topk} best guesses and the probability that each is correct (0.0 to 1.0) for the following question. Give ONLY the guesses and probabilities, no other words or explanation. For example:\n\nG1: <first most likely guess, as short as possible; not a complete sentence, just the guess!>\nP1: <the probability between 0.0 and 1.0 that G1 is correct, without any extra commentary whatsoever; just the probability!>\n...\nG${topk}: <${topk}-th most likely guess, as short as possible; not a complete sentence, just the guess!>\nP${topk}: <the probability between 0.0 and 1.0 that G${topk} is correct, without any extra commentary whatsoever; just the probability!>",
529
+ few_shot_prompt="Question: {question}\nG1: {answer}\nP1: <number between 0.0 and 1.0 reflecting confidence in this guess>\n...\nG${topk}: <other guess>\nP${topk}: <probability of this guess>",
530
+ ),
531
+ "coqa_verb_2s_cot": generate_coqa_instruct_config(
532
+ description="Here's a short story:\n\n{story} (End of story)\n\nProvide your best guess for the following question. Before giving your answer, provide a step-by-step explanation of your thought process. Then on a new line give the guess with no other words or explanation.\n\nFor example:\n\nExplanation: <one sentence step-by-step explanation of your thought process>\nGuess: <most likely guess, as short as possible; not a complete sentence, just the guess!>",
533
+ few_shot_prompt="Question: {question}\nExplanation: <step-by-step explanation of your thought process>\nGuess: {answer}",
534
+ ),
535
+ "coqa_verb_2s_top1": generate_coqa_instruct_config(
536
+ description="Here's a short story:\n\n{story} (End of story)\n\nProvide your best guess for the following question. Give ONLY the guess, no other words or explanation.\n\nFor example:\n\nGuess: <most likely guess, as short as possible; not a complete sentence, just the guess!>",
537
+ few_shot_prompt="Question: {question}\nGuess: {answer}",
538
+ ),
539
+ "coqa_verb_2s_topk": generate_coqa_instruct_config(
540
+ description="Here's a short story:\n\n{story} (End of story)\n\nProvide your ${topk} best guesses for the following question. Give ONLY the guesses, no other words or explanation. For example:\n\nG1: <first most likely guess, as short as possible; not a complete sentence, just the guess!>\n...\nG${topk}: <${topk}-th most likely guess, as short as possible; not a complete sentence, just the guess!>",
541
+ few_shot_prompt="Question: {question}\nG1: {answer}\n...\nG${topk}: <other guess>",
542
+ ),
543
+ "mmlu_ling_1s": generate_mmlu_instruct_config(
544
+ description="Provide your best guess for the following question about {subject} selecting one of the options, and describe how likely it is that your guess is correct as one of the following expressions:\n\nAlmost Certain\nHighly Likely\nVery Good Chance\nWe Beleive\nProbably\nProbable\nLikely\nBetter than Even\nAbout Even\nProbably Not\nWe Doubt\nUnlikely\nLittle Chance\nChances Are Slight\nImprobable\nHighly Unlikely\nAlmost No Chance\n\nGive ONLY the guess and your confidence, no other words or explanation. For example:\n\nGuess: <most likely guess, only the selected option letter; not a complete sentence, just the guess!>\nConfidence: <description of confidence, without any extra commentary whatsoever; just a short phrase!>",
545
+ few_shot_prompt="Q:{question}\nA. {choices[0]}\nB. {choices[1]}\nC. {choices[2]}\nD. {choices[3]}\nGuess:{answer}\nConfidence: <appropriate level of confidence in this guess>",
546
+ ),
547
+ "mmlu_verb_1s_top1": generate_mmlu_instruct_config(
548
+ description="Provide your best guess for the following question about {subject} selecting one of the options and the probability that it is correct (0.0 to 1.0). Give ONLY the guess and probability, no other words or explanation. For example:\n\nGuess: <most likely guess, only the selected option letter; not a complete sentence, just the guess!>\nProbability: <the probability between 0.0 and 1.0 that your guess is correct, without any extra commentary whatsoever; just the probability!>",
549
+ few_shot_prompt="Q:{question}\nA. {choices[0]}\nB. {choices[1]}\nC. {choices[2]}\nD. {choices[3]}\nGuess:{answer}\nProbability: <number between 0.0 and 1.0 reflecting confidence in the guess>",
550
+ ),
551
+ "mmlu_verb_1s_topk": generate_mmlu_instruct_config(
552
+ description="Provide your ${topk} best guesses for the following question about {subject} selecting one of the options and the probability that each guess is correct (0.0 to 1.0). Give ONLY the guesses and probabilities, no other words or explanation. For example:\n\nG1: <first most likely guess, only the selected option letter; not a complete sentence, just the guess!>\nP1: <the probability between 0.0 and 1.0 that G1 is correct, without any extra commentary whatsoever; just the probability!>\n...\nG${topk}: <${topk}-th most likely guess, as short as possible; not a complete sentence, just the guess!>\nP${topk}: <the probability between 0.0 and 1.0 that G${topk} is correct, without any extra commentary whatsoever; just the probability!>",
553
+ few_shot_prompt="Q:{question}\nA. {choices[0]}\nB. {choices[1]}\nC. {choices[2]}\nD. {choices[3]}\nG1: {answer}\nP1: <number between 0.0 and 1.0 reflecting confidence in this guess>\n...\nG${topk}: <other guess>\nP${topk}: <probability of this guess>",
554
+ ),
555
+ "mmlu_verb_2s_cot": generate_mmlu_instruct_config(
556
+ description="Provide your best guess for the following question about {subject} selecting one of the options. Before giving your answer, provide a step-by-step explanation of your thought process. Then on a new line give the guess with no other words or explanation.\n\nFor example:\n\nExplanation: <one sentence step-by-step explanation of your thought process>\nGuess: <most likely guess, as short as possible; not a complete sentence, just the guess!>",
557
+ few_shot_prompt="Q:{question}\nA. {choices[0]}\nB. {choices[1]}\nC. {choices[2]}\nD. {choices[3]}\nExplanation: <step-by-step explanation of your thought process>\nGuess:{answer}",
558
+ ),
559
+ "mmlu_verb_2s_top1": generate_mmlu_instruct_config(
560
+ description="Provide your best guess for the following question about {subject} selecting one of the options. Give ONLY the guess, no other words or explanation.\n\nFor example:\n\nGuess: <most likely guess, only the selected option letter; not a complete sentence, just the guess!>",
561
+ few_shot_prompt="Q:{question}\nA. {choices[0]}\nB. {choices[1]}\nC. {choices[2]}\nD. {choices[3]}\nGuess:{answer}",
562
+ ),
563
+ "mmlu_verb_2s_topk": generate_mmlu_instruct_config(
564
+ description="Provide your ${topk} best guesses for the following question about {subject} selecting one of the options. Give ONLY the guesses, no other words or explanation. For example:\n\nG1: <first most likely guess, only the selected option letter; not a complete sentence, just the guess!>\n...\nG${topk}: <${topk}-th most likely guess, as short as possible; not a complete sentence, just the guess!>",
565
+ few_shot_prompt="Q:{question}\nA. {choices[0]}\nB. {choices[1]}\nC. {choices[2]}\nD. {choices[3]}\nG1: {answer}\n...\nG${topk}: <other guess>",
566
+ ),
567
+ "triviaqa_ling_1s": generate_triviaqa_instruct_config(
568
+ description="Provide your best guess for the following question, and describe how likely it is that your guess is correct as one of the following expressions:\n\nAlmost Certain\nHighly Likely\nVery Good Chance\nWe Beleive\nProbably\nProbable\nLikely\nBetter than Even\nAbout Even\nProbably Not\nWe Doubt\nUnlikely\nLittle Chance\nChances Are Slight\nImprobable\nHighly Unlikely\nAlmost No Chance\n\nGive ONLY the guess and your confidence, no other words or explanation. For example:\n\nGuess: <most likely guess, as short as possible; not a complete sentence, just the guess!>\nConfidence: <description of confidence, without any extra commentary whatsoever; just a short phrase!>",
569
+ few_shot_prompt="Question: {question}\nGuess: {answer}\nConfidence: <appropriate level of confidence in this guess>",
570
+ ),
571
+ "triviaqa_verb_1s_top1": generate_triviaqa_instruct_config(
572
+ description="Provide your best guess and the probability that it is correct (0.0 to 1.0) for the following question. Give ONLY the guess and probability, no other words or explanation. For example:\n\nGuess: <most likely guess, as short as possible; not a complete sentence, just the guess!>\nProbability: <the probability between 0.0 and 1.0 that your guess is correct, without any extra commentary whatsoever; just the probability!>",
573
+ few_shot_prompt="Question: {question}\nGuess: {answer}\nProbability: <number between 0.0 and 1.0 reflecting confidence in the guess>",
574
+ ),
575
+ "triviaqa_verb_1s_topk": generate_triviaqa_instruct_config(
576
+ description="Provide your ${topk} best guesses and the probability that each is correct (0.0 to 1.0) for the following question. Give ONLY the guesses and probabilities, no other words or explanation. For example:\n\nG1: <first most likely guess, as short as possible; not a complete sentence, just the guess!>\nP1: <the probability between 0.0 and 1.0 that G1 is correct, without any extra commentary whatsoever; just the probability!>\n...\nG${topk}: <${topk}-th most likely guess, as short as possible; not a complete sentence, just the guess!>\nP${topk}: <the probability between 0.0 and 1.0 that G${topk} is correct, without any extra commentary whatsoever; just the probability!>",
577
+ few_shot_prompt="Question: {question}\nG1: {answer}\nP1: <number between 0.0 and 1.0 reflecting confidence in this guess>\n...\nG${topk}: <other guess>\nP${topk}: <probability of this guess>",
578
+ ),
579
+ "triviaqa_verb_2s_cot": generate_triviaqa_instruct_config(
580
+ description="Provide your best guess for the following question. Before giving your answer, provide a step-by-step explanation of your thought process. Then on a new line give the guess with no other words or explanation.\n\nFor example:\n\nExplanation: <one sentence step-by-step explanation of your thought process>\nGuess: <most likely guess, as short as possible; not a complete sentence, just the guess!>",
581
+ few_shot_prompt="Question: {question}\nExplanation: <step-by-step explanation of your thought process>\nGuess: {answer}",
582
+ ),
583
+ "triviaqa_verb_2s_top1": generate_triviaqa_instruct_config(
584
+ description="Provide your best guess for the following question. Give ONLY the guess, no other words or explanation.\n\nFor example:\n\nGuess: <most likely guess, as short as possible; not a complete sentence, just the guess!>",
585
+ few_shot_prompt="Question: {question}\nGuess: {answer}",
586
+ ),
587
+ "triviaqa_verb_2s_topk": generate_triviaqa_instruct_config(
588
+ description="Provide your ${topk} best guesses for the following question. Give ONLY the guesses, no other words or explanation. For example:\n\nG1: <first most likely guess, as short as possible; not a complete sentence, just the guess!>\n...\nG${topk}: <${topk}-th most likely guess, as short as possible; not a complete sentence, just the guess!>",
589
+ few_shot_prompt="Question: {question}\nG1: {answer}\n...\nG${topk}: <other guess>",
590
+ ),
591
+ }
592
+
593
+
594
+ def build_dataset(dataset_name):
595
+ config = DATASET_CONFIG[dataset_name]
596
+ if isinstance(config["name"], list):
597
+ dataset = datasets.load_dataset(*config["name"], trust_remote_code=True, num_proc=4)
598
+ else:
599
+ dataset = datasets.load_dataset(config["name"], trust_remote_code=True, num_proc=4)
600
+
601
+ def prepare_dataset(split):
602
+ x, y = config["prepare_func"](dataset=dataset[config[f"{split}_split"]])
603
+ result_dataset = datasets.Dataset.from_dict({"input": x, "output": y})
604
+ return result_dataset
605
+
606
+ result = {}
607
+ if "train_split" in config:
608
+ result["train"] = prepare_dataset("train")
609
+ if "test_split" in config:
610
+ result["test"] = prepare_dataset("test")
611
+ return datasets.DatasetDict(result)