DSPy: The framework for programming—not prompting—language models
Python
minor fixes to gsm8k (#1869)
Chen Qian9138b64678dd
diff --git a/dspy/datasets/gsm8k.py b/dspy/datasets/gsm8k.pyindex 328d1aee8..ec1acf6a4 100644--- a/dspy/datasets/gsm8k.py+++ b/dspy/datasets/gsm8k.py@@ -5,36 +5,35 @@ from datasets import load_datasetclass GSM8K:- def __init__(self) -> None:- super().__init__()+ def __init__(self):self.do_shuffle = False- dataset = load_dataset("gsm8k", 'main')+ dataset = load_dataset("gsm8k", "main")- hf_official_train = dataset['train']- hf_official_test = dataset['test']+ hf_official_train = dataset["train"]+ hf_official_test = dataset["test"]official_train = []official_test = []for example in tqdm.tqdm(hf_official_train):- question = example['question']+ question = example["question"]- answer = example['answer'].strip().split()- assert answer[-2] == '####'-- gold_reasoning = ' '.join(answer[:-2])- answer = str(int(answer[-1].replace(',', '')))+ answer = example["answer"].strip().split()+ assert answer[-2] == "####"++ gold_reasoning = " ".join(answer[:-2])+ answer = str(int(answer[-1].replace(",", "")))official_train.append(dict(question=question, gold_reasoning=gold_reasoning, answer=answer))for example in tqdm.tqdm(hf_official_test):- question = example['question']+ question = example["question"]++ answer = example["answer"].strip().split()+ assert answer[-2] == "####"- answer = example['answer'].strip().split()- assert answer[-2] == '####'-- gold_reasoning = ' '.join(answer[:-2])- answer = str(int(answer[-1].replace(',', '')))+ gold_reasoning = " ".join(answer[:-2])+ answer = str(int(answer[-1].replace(",", "")))official_test.append(dict(question=question, gold_reasoning=gold_reasoning, answer=answer))@@ -50,35 +49,29 @@ class GSM8K:import dspy- trainset = [dspy.Example(**x).with_inputs('question') for x in trainset]- devset = [dspy.Example(**x).with_inputs('question') for x in devset]- testset = [dspy.Example(**x).with_inputs('question') for x in testset]-- # print(f"Trainset size: {len(trainset)}")- # print(f"Devset size: {len(devset)}")- # print(f"Testset size: {len(testset)}")+ trainset = [dspy.Example(**x).with_inputs("question") for x in trainset]+ devset = [dspy.Example(**x).with_inputs("question") for x in devset]+ testset = [dspy.Example(**x).with_inputs("question") for x in testset]self.train = trainsetself.dev = devsetself.test = testset-def parse_integer_answer(answer, only_first_line=True):try:if only_first_line:- answer = answer.strip().split('\n')[0]+ answer = answer.strip().split("\n")[0]# find the last token that has a number in itanswer = [token for token in answer.split() if any(c.isdigit() for c in token)][-1]- answer = answer.split('.')[0]- answer = ''.join([c for c in answer if c.isdigit()])+ answer = answer.split(".")[0]+ answer = "".join([c for c in answer if c.isdigit()])answer = int(answer)except (ValueError, IndexError):- # print(answer)answer = 0-+return answer