-
Notifications
You must be signed in to change notification settings - Fork 0
/
infer_critique.py
161 lines (133 loc) · 6.02 KB
/
infer_critique.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import argparse
import json
import os
from evaluate import evaluate_critique
from utils import infer, launch_locally
def format_prompt(prompt_base, item):
reasoning = "\n".join(["{:d}. {:s}".format(i + 1, x) for i, x in enumerate(item['response']['reasoning'])])
prompt = prompt_base.replace("{{{QUESTION}}}", item['question']). \
replace("{{{ANSWER}}}", str(item['response']['answer'])).replace("{{{REASONING}}}", reasoning)
prompt_lines = prompt.splitlines()
final_prompt_lines = []
for line in prompt_lines:
if '{{{REPEAT_BY_N_STEP}}}' in line:
for i in range(len(item['response']['reasoning'])):
final_prompt_lines.append(line.replace('{{{REPEAT_BY_N_STEP}}}', str(i + 1)))
else:
final_prompt_lines.append(line)
prompt = "\n".join(final_prompt_lines)
return prompt
def format_response(response, n_steps):
if isinstance(response, list) or isinstance(response, tuple):
response_history = response[:-1]
response_orig = response = response[-1]
else:
response_history = None
response_orig = response
format_error = False
if not isinstance(response, str):
ret = {'response': response, 'formatted': None, 'format_error': True}
if response_history is not None:
ret['response_history'] = response_history
return ret
response = response.replace('\_', '_').replace('\\', '\\\\')
try:
response = response.split('```json')[-1].split('```')[0]
response = json.loads(response)
assert isinstance(response, dict)
except:
try:
response = '{' + "{".join(response.split('{')[1:])
response = "}".join(response.split('}')[:-1]) + '}'
response = json.loads(response)
assert isinstance(response, dict)
except:
response = {}
format_error = True
def to_true_or_false(x):
if isinstance(x, bool):
return x
elif isinstance(x, str):
x = x.lower().strip()
if x == 'correct' or x == 'yes' or x == 'true':
return True
elif x == 'incorrect' or x == 'no' or x == 'false':
return False
return None
def process_dict_key(x):
if isinstance(x, dict):
return {k.strip().lower(): process_dict_key(v) for k, v in x.items()}
return x
response = process_dict_key(response)
formatted = {}
formatted['answer_correctness'] = None
if 'answer_correctness' in response:
if isinstance(response['answer_correctness'], dict):
if 'correctness' in response['answer_correctness']:
formatted['answer_correctness'] = to_true_or_false(response['answer_correctness']['correctness'])
else:
formatted['answer_correctness'] = to_true_or_false(response['answer_correctness'])
if formatted['answer_correctness'] is None:
format_error = True
formatted['reasoning_correctness'] = [None for _ in range(n_steps)]
formatted['reasoning_critic'] = [None for _ in range(n_steps)]
for i in range(n_steps):
if 'step_{:d}'.format(i + 1) in response:
step_response = response['step_{:d}'.format(i + 1)]
if isinstance(step_response, dict) and 'correctness' in step_response:
formatted['reasoning_correctness'][i] = to_true_or_false(step_response['correctness'])
if 'explanation' in step_response:
formatted['reasoning_critic'][i] = str(step_response['explanation'])
if formatted['reasoning_correctness'][i] is None or formatted['reasoning_critic'][i] is None:
format_error = True
ret = {'response': response_orig, 'formatted': formatted, 'format_error': format_error}
if response_history is not None:
ret['response_history'] = response_history
return ret
def main(args):
with open(args.input) as f:
data = [json.loads(line) for line in f]
prompt_fname = os.path.join(os.path.dirname(__file__), 'prompts/critique.txt')
with open(prompt_fname) as f:
PROMPT = f.read()
prompts = [format_prompt(PROMPT, item) for item in data]
print("\n--- Example prompt")
print(prompts[0])
images = [item['image'] for item in data]
responses = infer(prompts, images, args)
responses = [format_response(response, len(item['response']['reasoning']))
for response, item in zip(responses, data)]
for i in range(5):
print("\n--- Example parse:\n")
print(responses[i]['response'])
print("\n--->\n")
print(json.dumps(responses[i]['formatted'], indent=2))
if args.output is not None:
print("Save outputs to", args.output)
os.makedirs(os.path.dirname(args.output), exist_ok=True)
with open(args.output, 'w') as f:
for r in responses:
f.write(json.dumps(r) + '\n')
evaluate_critique(data, responses)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# model and inference parameters
parser.add_argument('--model', default="gpt-4o-2024-08-06") # auto if we're using a locally served model
# openai api-based
parser.add_argument('--api_key', default='YOUR_API_KEY')
parser.add_argument('--base_url', default=None)
parser.add_argument('--n_proc', default=16, type=int)
parser.add_argument('--launch_locally', default=None, choices=['lmdeploy', 'vllm', 'sglang'])
# input output
parser.add_argument('--input', default='test.jsonl')
parser.add_argument('--output', default=None)
args = parser.parse_args()
if args.launch_locally:
process, port = launch_locally(args.launch_locally, args.model)
args.model = 'auto'
args.base_url = f'http://0.0.0.0:{port}/v1'
try:
main(args)
finally:
if args.launch_locally:
process.kill()