Skip to content

Commit 022c8b0

Browse files
authored
add gsm8k prompt example (#1063)
1 parent 9b06d45 commit 022c8b0

File tree

8 files changed

+990
-13
lines changed

8 files changed

+990
-13
lines changed

llm/prompt/cot/evaluate_gsm8k.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import re
2+
import mindspore
3+
import argparse
4+
import jsonlines
5+
import numpy as np
6+
import datasets
7+
from datasets import load_from_disk, load_dataset
8+
from mindnlp.transformers import AutoModelForCausalLM, AutoTokenizer
9+
from mindnlp.transformers.generation import GenerationConfig
10+
11+
12+
ANS_RE = re.compile(r"#### (\-?[0-9\.\,]+)")
13+
INVALID_ANS = "[invalid]"
14+
15+
16+
def doc_to_text(doc):
17+
return (
18+
fewshot_prompt
19+
+ "\nQuestion: "
20+
+ doc["question"]
21+
+ "\nLet's think step by step\n"
22+
)
23+
24+
25+
def decode(tokens_list, tokenizer, raw_text_len):
26+
sents = []
27+
# print(len(tokens_list))
28+
for tokens in tokens_list:
29+
tokens = tokens.asnumpy().tolist()
30+
sent = tokenizer.decode(tokens[raw_text_len:])
31+
sent = sent.split("<|endoftext|>")[0]
32+
sent = sent.split("\n\n\n")[0]
33+
sent = sent.split("\n\n")[0]
34+
sent = sent.split("Question:")[0]
35+
sents.append(sent)
36+
return sents
37+
38+
39+
def generate_sample(model, tokenizer, input_txt):
40+
input_ids = tokenizer.encode(input_txt)
41+
raw_text_len = len(input_ids)
42+
context_enc = mindspore.tensor([input_ids])
43+
print(f"Input text: {input_txt}\n")
44+
outputs = model.generate(context_enc)
45+
output_text = decode(outputs, tokenizer, raw_text_len)[0]
46+
print(f"\nOutput text: {output_text}\n")
47+
return output_text
48+
49+
50+
def extract_answer_hf(completion):
51+
match = ANS_RE.search(completion)
52+
if match:
53+
match_str = match.group(1).strip()
54+
match_str = match_str.replace(",", "")
55+
return eval(match_str)
56+
else:
57+
return INVALID_ANS
58+
59+
60+
def extract_answer(completion):
61+
try:
62+
last_number = re.findall(r"\d+", completion)[-1]
63+
return eval(last_number)
64+
except:
65+
return INVALID_ANS
66+
67+
68+
def is_correct(completion, answer):
69+
gold = extract_answer_hf(answer)
70+
assert gold != INVALID_ANS, "No ground truth answer found in the document."
71+
return extract_answer(completion) == gold
72+
73+
74+
if __name__ == "__main__":
75+
parser = argparse.ArgumentParser(description="Test HF checkpoint.")
76+
parser.add_argument(
77+
"-c",
78+
"--checkpoint-path",
79+
type=str,
80+
help="Checkpoint path",
81+
default="Qwen/Qwen1.5-7B",
82+
)
83+
parser.add_argument("-f", "--sample-input-file", type=str, default=None)
84+
parser.add_argument(
85+
"-o", "--sample-output-file", type=str, default="gsm8k_res.jsonl"
86+
)
87+
88+
args = parser.parse_args()
89+
90+
fewshot_prompt = open("gsm8k_prompt.txt").read()
91+
if args.sample_input_file is not None:
92+
dataset = load_from_disk(args.sample_input_file)
93+
else:
94+
config = datasets.DownloadConfig(resume_download=True, max_retries=100)
95+
dataset = load_dataset("gsm8k", "main", download_config=config)
96+
97+
test = dataset["test"]
98+
99+
print("Loading tokenizer ...")
100+
tokenizer = AutoTokenizer.from_pretrained(
101+
args.checkpoint_path
102+
)
103+
104+
print("Loading model ...")
105+
model = AutoModelForCausalLM.from_pretrained(
106+
args.checkpoint_path
107+
).set_train(False)
108+
model.generation_config = GenerationConfig.from_pretrained(
109+
args.checkpoint_path
110+
)
111+
model.generation_config.do_sample = False
112+
113+
f_output = jsonlines.Writer(open(args.sample_output_file, "w", encoding="utf-8"))
114+
tot_length = test.num_rows
115+
acc_res = []
116+
for doc in test:
117+
context = doc_to_text(doc)
118+
completion = generate_sample(model, tokenizer, context)
119+
answer = doc["answer"]
120+
acc = is_correct(completion, answer)
121+
doc["completion"] = completion
122+
doc["acc"] = acc
123+
f_output.write(doc)
124+
acc_res.append(acc)
125+
126+
f_output.close()
127+
print("Acc: ", np.mean(acc_res))

llm/prompt/cot/gsm8k_prompt.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Question: In 2004, there were 60 kids at a cookout. In 2005, half the number of kids came to the cookout as compared to 2004. In 2006, 2/3 as many kids came to the cookout as in 2005. How many kids came to the cookout in 2006?
2+
Let's think step by step
3+
In 2005, 60/2=30 kids came to the cookout.
4+
In 2006, 30/3*2=20 kids came to the cookout.
5+
The answer is 20
6+
7+
Question: Zilla spent 7% of her monthly earnings on rent, half of it on her other monthly expenses, and put the rest in her savings. If she spent $133 on her rent, how much does she deposit into her savings account in a month?
8+
Let's think step by step
9+
Since $133 is equal to 7% of her earnings, then 1% is equal to $133/7 = $19.
10+
The total monthly earning of Zilla is represented by 100%, so $19 x 100 = $1900 is her monthly earnings.
11+
So, $1900/2 = $950 is spent on her other monthly expenses.
12+
The total amount spent on the rent and other monthly expenses is $133 + $950 = $1083.
13+
Hence, she saves $1900 - $1083 = $817 per month.
14+
The answer is 817
15+
16+
Question: If Buzz bought a pizza with 78 slices at a restaurant and then decided to share it with the waiter in the ratio of 5:8, with Buzz's ratio being 5, what's twenty less the number of slices of pizza that the waiter ate?
17+
Let's think step by step
18+
The total ratio representing the slices of pizza that Buzz bought is 5+8=13
19+
If he shared the slices of pizza with the waiter, the waiter received a fraction of 8/13 of the total number of slices, which totals 8/13 * 78 = 48 slices
20+
Twenty less the number of slices of pizza that the waiter ate is 48-20 = 28
21+
The answer is 28
22+
23+
Question: Jame gets a raise to $20 per hour and works 40 hours a week. His old job was $16 an hour for 25 hours per week. How much more money does he make per year in his new job than the old job if he works 52 weeks a year?
24+
Let's think step by step
25+
He makes 20*40=$800 per week
26+
He used to make 16*25=$400 per week
27+
So his raise was 800-400=$400 per week
28+
So he makes 400*52=$20,800 per year more
29+
The answer is 20800
30+
31+
Question: Mr. Gardner bakes 20 cookies, 25 cupcakes, and 35 brownies for his second-grade class of 20 students. If he wants to give each student an equal amount of sweet treats, how many sweet treats will each student receive?
32+
Let's think step by step
33+
Mr. Gardner bakes a total of 20 + 25 + 35 = 80 sweet treats
34+
Each student will receive 80 / 20 = 4 sweet treats
35+
The answer is 4
36+
37+
Question: A used car lot has 24 cars and motorcycles (in total) for sale. A third of the vehicles are motorcycles, and a quarter of the cars have a spare tire included. How many tires are on the used car lot’s vehicles in all?
38+
Let's think step by step
39+
The used car lot has 24 / 3 = 8 motorcycles with 2 tires each.
40+
The lot has 24 - 8 = 16 cars for sale
41+
There are 16 / 4 = 4 cars with a spare tire with 5 tires each.
42+
The lot has 16 - 4 = 12 cars with 4 tires each.
43+
Thus, the used car lot’s vehicles have 8 * 2 + 4 * 5 + 12 * 4 = 16 + 20 + 48 = 84 tires in all.
44+
The answer is 84
45+
46+
Question: Norma takes her clothes to the laundry. She leaves 9 T-shirts and twice as many sweaters as T-shirts in the washer. When she returns she finds 3 sweaters and triple the number of T-shirts. How many items are missing?
47+
Let's think step by step
48+
Norma left 9 T-shirts And twice as many sweaters, she took 9 * 2= 18 sweaters
49+
Adding the T-shirts and sweaters, Norma left 9 + 18 = 27 clothes
50+
When she came back, she found 3 sweaters And triple the number of T-shirts, she found 3 * 3 = 9 T-shirts
51+
Adding the T-shirts and sweaters, Norma found 3 + 9 = 12 clothes
52+
Subtracting the clothes she left from the clothes she found, 27 - 12 = 15 clothes are missing
53+
The answer is 15
54+
55+
Question: Adam has an orchard. Every day for 30 days he picks 4 apples from his orchard. After a month, Adam has collected all the remaining apples, which were 230. How many apples in total has Adam collected from his orchard?
56+
Let's think step by step
57+
During 30 days Adam picked 4 * 30 = 120 apples.
58+
So in total with all the remaining apples, he picked 120 + 230 = 350 apples from his orchard.
59+
The answer is 350

0 commit comments

Comments
 (0)