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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
"instruct": True,
|
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 |
+
"instruct": True,
|
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 |
+
"instruct": True,
|
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 |
+
},
|
415 |
+
"person_bio_en": {
|
416 |
+
"name": "rediska0123/person-bio",
|
417 |
+
"test_split": "test",
|
418 |
+
"prepare_func": partial(
|
419 |
+
prepare_person,
|
420 |
+
input_column="question",
|
421 |
+
),
|
422 |
+
},
|
423 |
+
"person_bio_ru": {
|
424 |
+
"name": "rvanova/person-bio",
|
425 |
+
"test_split": "test",
|
426 |
+
"prepare_func": partial(
|
427 |
+
prepare_person,
|
428 |
+
input_column="question",
|
429 |
+
),
|
430 |
+
},
|
431 |
+
"person_bio_zh": {
|
432 |
+
"name": "ruixing76/person-bio-zh",
|
433 |
+
"test_split": "train",
|
434 |
+
"prepare_func": partial(
|
435 |
+
prepare_person,
|
436 |
+
input_column="question",
|
437 |
+
),
|
438 |
+
},
|
439 |
+
"triviaqa": {
|
440 |
+
"name": ["trivia_qa", "rc.nocontext"],
|
441 |
+
"train_split": "train",
|
442 |
+
"test_split": "validation",
|
443 |
+
"prepare_func": partial(
|
444 |
+
prepare_trivia_qa,
|
445 |
+
prompt="Question: {question}\nAnswer:{answer}",
|
446 |
+
n_shot=5,
|
447 |
+
few_shot_dataset_func=partial(
|
448 |
+
datasets.load_dataset,
|
449 |
+
path="trivia_qa",
|
450 |
+
name="rc.nocontext",
|
451 |
+
split="train",
|
452 |
+
),
|
453 |
+
description="",
|
454 |
+
few_shot_prompt=None,
|
455 |
+
instruct=False,
|
456 |
+
),
|
457 |
+
},
|
458 |
+
"wiki_bio": {
|
459 |
+
"name": "wiki_bio",
|
460 |
+
"test_split": "test",
|
461 |
+
"prepare_func": partial(
|
462 |
+
prepare_wiki,
|
463 |
+
input_column="input_text",
|
464 |
+
prompt="This is a Wikipedia passage about {context}:\n",
|
465 |
+
),
|
466 |
+
},
|
467 |
+
"wmt14_deen": {
|
468 |
+
"name": ["wmt14", "de-en"],
|
469 |
+
"train_split": "train",
|
470 |
+
"test_split": "test",
|
471 |
+
"prepare_func": partial(
|
472 |
+
prepare_wmt,
|
473 |
+
input_column="de",
|
474 |
+
output_column="en",
|
475 |
+
prompt="Here is a sentence in {source_lang} language and its translation in {target_lang} language.\n\nOriginal:\n{text}\nTranslation:\n",
|
476 |
+
),
|
477 |
+
},
|
478 |
+
"wmt14_fren": {
|
479 |
+
"name": ["wmt14", "fr-en"],
|
480 |
+
"train_split": "train",
|
481 |
+
"test_split": "test",
|
482 |
+
"prepare_func": partial(
|
483 |
+
prepare_wmt,
|
484 |
+
input_column="fr",
|
485 |
+
output_column="en",
|
486 |
+
prompt="Here is a sentence in {source_lang} language and its translation in {target_lang} language.\n\nOriginal:\n{text}\nTranslation:\n",
|
487 |
+
),
|
488 |
+
},
|
489 |
+
"wmt19_deen": {
|
490 |
+
"name": ["wmt19", "de-en"],
|
491 |
+
"train_split": "train",
|
492 |
+
"test_split": "validation",
|
493 |
+
"prepare_func": partial(
|
494 |
+
prepare_wmt,
|
495 |
+
input_column="de",
|
496 |
+
output_column="en",
|
497 |
+
prompt="Here is a sentence in {source_lang} language and its translation in {target_lang} language.\n\nOriginal:\n{text}\nTranslation:\n",
|
498 |
+
),
|
499 |
+
},
|
500 |
+
"xsum": {
|
501 |
+
"name": "xsum",
|
502 |
+
"train_split": "train",
|
503 |
+
"test_split": "validation",
|
504 |
+
"prepare_func": partial(
|
505 |
+
prepare_base,
|
506 |
+
input_column="document",
|
507 |
+
output_column="summary",
|
508 |
+
prompt="Here's the text and it's short one-sentence summary.\n\nText:\n{text}\n\nSummary (one sentence):\n",
|
509 |
+
),
|
510 |
+
},
|
511 |
+
# instruct datasets
|
512 |
+
"coqa_ling_1s": generate_coqa_instruct_config(
|
513 |
+
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!>",
|
514 |
+
few_shot_prompt="Question: {question}\nGuess: {answer}\nConfidence: <appropriate level of confidence in this guess>",
|
515 |
+
),
|
516 |
+
"coqa_verb_1s_top1": generate_coqa_instruct_config(
|
517 |
+
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!>",
|
518 |
+
few_shot_prompt="Question: {question}\nGuess: {answer}\nProbability: <number between 0.0 and 1.0 reflecting confidence in the guess>",
|
519 |
+
),
|
520 |
+
"coqa_verb_1s_topk": generate_coqa_instruct_config(
|
521 |
+
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!>",
|
522 |
+
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>",
|
523 |
+
),
|
524 |
+
"coqa_verb_2s_cot": generate_coqa_instruct_config(
|
525 |
+
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!>",
|
526 |
+
few_shot_prompt="Question: {question}\nExplanation: <step-by-step explanation of your thought process>\nGuess: {answer}",
|
527 |
+
),
|
528 |
+
"coqa_verb_2s_top1": generate_coqa_instruct_config(
|
529 |
+
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!>",
|
530 |
+
few_shot_prompt="Question: {question}\nGuess: {answer}",
|
531 |
+
),
|
532 |
+
"coqa_verb_2s_topk": generate_coqa_instruct_config(
|
533 |
+
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!>",
|
534 |
+
few_shot_prompt="Question: {question}\nG1: {answer}\n...\nG${topk}: <other guess>",
|
535 |
+
),
|
536 |
+
"mmlu_ling_1s": generate_mmlu_instruct_config(
|
537 |
+
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!>",
|
538 |
+
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>",
|
539 |
+
),
|
540 |
+
"mmlu_verb_1s_top1": generate_mmlu_instruct_config(
|
541 |
+
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!>",
|
542 |
+
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>",
|
543 |
+
),
|
544 |
+
"mmlu_verb_1s_topk": generate_mmlu_instruct_config(
|
545 |
+
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!>",
|
546 |
+
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>",
|
547 |
+
),
|
548 |
+
"mmlu_verb_2s_cot": generate_mmlu_instruct_config(
|
549 |
+
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!>",
|
550 |
+
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}",
|
551 |
+
),
|
552 |
+
"mmlu_verb_2s_top1": generate_mmlu_instruct_config(
|
553 |
+
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!>",
|
554 |
+
few_shot_prompt="Q:{question}\nA. {choices[0]}\nB. {choices[1]}\nC. {choices[2]}\nD. {choices[3]}\nGuess:{answer}",
|
555 |
+
),
|
556 |
+
"mmlu_verb_2s_topk": generate_mmlu_instruct_config(
|
557 |
+
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!>",
|
558 |
+
few_shot_prompt="Q:{question}\nA. {choices[0]}\nB. {choices[1]}\nC. {choices[2]}\nD. {choices[3]}\nG1: {answer}\n...\nG${topk}: <other guess>",
|
559 |
+
),
|
560 |
+
"triviaqa_ling_1s": generate_triviaqa_instruct_config(
|
561 |
+
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!>",
|
562 |
+
few_shot_prompt="Question: {question}\nGuess: {answer}\nConfidence: <appropriate level of confidence in this guess>",
|
563 |
+
),
|
564 |
+
"triviaqa_verb_1s_top1": generate_triviaqa_instruct_config(
|
565 |
+
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!>",
|
566 |
+
few_shot_prompt="Question: {question}\nGuess: {answer}\nProbability: <number between 0.0 and 1.0 reflecting confidence in the guess>",
|
567 |
+
),
|
568 |
+
"triviaqa_verb_1s_topk": generate_triviaqa_instruct_config(
|
569 |
+
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!>",
|
570 |
+
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>",
|
571 |
+
),
|
572 |
+
"triviaqa_verb_2s_cot": generate_triviaqa_instruct_config(
|
573 |
+
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!>",
|
574 |
+
few_shot_prompt="Question: {question}\nExplanation: <step-by-step explanation of your thought process>\nGuess: {answer}",
|
575 |
+
),
|
576 |
+
"triviaqa_verb_2s_top1": generate_triviaqa_instruct_config(
|
577 |
+
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!>",
|
578 |
+
few_shot_prompt="Question: {question}\nGuess: {answer}",
|
579 |
+
),
|
580 |
+
"triviaqa_verb_2s_topk": generate_triviaqa_instruct_config(
|
581 |
+
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!>",
|
582 |
+
few_shot_prompt="Question: {question}\nG1: {answer}\n...\nG${topk}: <other guess>",
|
583 |
+
),
|
584 |
+
}
|
585 |
+
|
586 |
+
|
587 |
+
def build_dataset(dataset_name):
|
588 |
+
config = DATASET_CONFIG[dataset_name]
|
589 |
+
if isinstance(config["name"], list):
|
590 |
+
dataset = datasets.load_dataset(*config["name"], trust_remote_code=True)
|
591 |
+
else:
|
592 |
+
dataset = datasets.load_dataset(config["name"], trust_remote_code=True)
|
593 |
+
|
594 |
+
def prepare_dataset(split):
|
595 |
+
x, y = config["prepare_func"](dataset=dataset[config[f"{split}_split"]])
|
596 |
+
result_dataset = datasets.Dataset.from_dict({"input": x, "output": y})
|
597 |
+
return result_dataset
|
598 |
+
|
599 |
+
return datasets.DatasetDict({
|
600 |
+
"train": prepare_dataset("train"),
|
601 |
+
"test": prepare_dataset("test"),
|
602 |
+
})
|