-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathrun_svamp_zs.py
93 lines (79 loc) · 2.73 KB
/
run_svamp_zs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import openai
from time import sleep
from tool import *
from typing import Dict, Any
from datetime import datetime
from tqdm import tqdm
import os
import json
import argparse
from collections import Counter
parser = argparse.ArgumentParser()
parser.add_argument("--key", default='OPENAI_KEY', type=str)
parser.add_argument("--dry_run", default=False, action='store_true')
parser.add_argument("--start", default=0, type=int)
parser.add_argument("--end", default=-1, type=int)
args = parser.parse_args()
def create_reader_request(example: Dict[str, Any]) -> str:
if not example["Body"].endswith('.'):
example["Body"] += '.'
string = f'# Question: {example["Body"]} {example["Question"]}'
return string
with open('data/SVAMP.json') as f:
svamp_data = json.load(f)
if __name__ == "__main__":
now = datetime.now()
dt_string = now.strftime("%m_%d_%H_%M")
correct, wrong = 0, 0
svamp_data = svamp_data[args.start:args.end]
filename = f'outputs/svamp_zs_s{args.start}_e{args.end}_{dt_string}.jsonl'
print(filename)
writer = open(filename, 'w')
for example in tqdm(svamp_data):
full_prompt = f"""
import math
import numpy as np
{create_reader_request(example)}
# Answer this question by implementing a solver() function.
def solver():
# Let's think step by step to derive the answer, and then return the answer
# According to the question, we can define the variables:
"""
if args.dry_run:
print(full_prompt)
print('=======================')
continue
# greedy decoding
got_result = False
while not got_result:
try:
result = openai.Completion.create(
engine='code-davinci-002',
prompt=full_prompt,
api_key=os.getenv(args.key),
max_tokens=300,
temperature=0.0,
top_p=1,
n=1,
stop=['\n\n'],
logprobs=1,
logit_bias={"1303": -2}
)
got_result = True
except Exception:
sleep(3)
ans = safe_execute(program)
prediction = floatify_ans(ans)
gt_ans = float(example['Answer'])
if finqa_equal(prediction, gt_ans):
correct += 1
else:
wrong += 1
print(program)
print(prediction, '$', gt_ans, '$', correct / (correct + wrong))
tmp = {'question': example['Question'], 'passage': example['Body'],
'executed': prediction, 'generated': program, 'answer': gt_ans}
writer.write(json.dumps(tmp) + '\n')
writer.close()
print()
print(correct / (correct + wrong))