text
stringlengths
0
15.3k
return text
def process_docs(dataset: datasets.Dataset) -> datasets.Dataset:
def _process_doc(doc):
choices = [preprocess(doc['Incorrect Answer 1']), preprocess(doc['Incorrect Answer 2']), preprocess(doc['Incorrect Answer 3']), preprocess(doc['Correct Answer'])]
random.shuffle(choices)
correct_answer_index = choices.index(preprocess(doc['Correct Answer']))
out_doc = {'choice1': choices[0], 'choice2': choices[1], 'choice3': choices[2], 'choice4': choices[3], 'answer': f'({chr(65 + correct_answer_index)})'}
return out_doc
return dataset.map(_process_doc)
# File: lm-evaluation-harness-main/lm_eval/tasks/hellaswag/utils.py
import re
import datasets
def preprocess(text):
text = text.strip()
text = text.replace(' [title]', '. ')
text = re.sub('\\[.*?\\]', '', text)
text = text.replace(' ', ' ')
return text
def process_docs(dataset: datasets.Dataset) -> datasets.Dataset:
def _process_doc(doc):
ctx = doc['ctx_a'] + ' ' + doc['ctx_b'].capitalize()
out_doc = {'query': preprocess(doc['activity_label'] + ': ' + ctx), 'choices': [preprocess(ending) for ending in doc['endings']], 'gold': int(doc['label'])}
return out_doc
return dataset.map(_process_doc)
# File: lm-evaluation-harness-main/lm_eval/tasks/hendrycks_ethics/utils.py
import random
def _preproc_doc(doc):
rnd = random.Random(doc['activity'])
scenarios = [doc['activity'], doc['baseline']]
ordering = [0, 1]
rnd.shuffle(ordering)
doc = {'scenarios': [scenarios[ordering[0]], scenarios[ordering[1]]], 'label': int(ordering.index(0) == 0)}
return doc
def doc_to_text(doc) -> str:
doc = _preproc_doc(doc)
return f"Scenario 1: {doc['scenarios'][0]}\nScenario 2: {doc['scenarios'][1]}\nQuestion: Is Scenario 1 preferable?\nAnswer:"
def doc_to_target(doc):
doc = _preproc_doc(doc)
return doc['label']
# File: lm-evaluation-harness-main/lm_eval/tasks/hendrycks_math/utils.py
from typing import Dict, List
import datasets
def process_docs(dataset: datasets.Dataset) -> datasets.Dataset:
def _process_doc(doc: dict) -> dict:
out_doc = {'problem': doc['problem'], 'solution': doc['solution'], 'answer': remove_boxed(last_boxed_only_string(doc['solution']))}
return out_doc
return dataset.map(_process_doc)
def process_results(doc: dict, results: List[str]) -> Dict[str, int]:
retval = 0
indices = [pos for (pos, char) in enumerate(results[0]) if char == '$']
if len(indices) <= 1:
answer = results[0]
else:
answer = results[0][indices[0] + 1:indices[-1]]
if is_equiv(answer, remove_boxed(last_boxed_only_string(doc['solution']))):
retval = 1
results = {'exact_match': retval}
return results
def is_equiv(str1, str2, verbose=False):
if str1 is None and str2 is None:
print('WARNING: Both None')
return True
if str1 is None or str2 is None:
return False
try:
ss1 = strip_string(str1)
ss2 = strip_string(str2)
if verbose:
print(ss1, ss2)
return ss1 == ss2
except Exception:
return str1 == str2
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')