-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_api.py
246 lines (215 loc) · 8.14 KB
/
run_api.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import json
import random
import time
from tqdm import tqdm
import multiprocessing
from concurrent.futures import ThreadPoolExecutor
import openai
import os
import argparse
from prompts import *
class ChatGPTProcessor:
def __init__(self):
self.lock = multiprocessing.Lock()
openai.api_key = ""
openai.api_base = ""
def read_jsonl(self, input_file):
with open(input_file, 'r', encoding='utf-8') as f:
lines = f.readlines()
return list(map(json.loads, tqdm(lines, desc='Reading...')))
def write_to_json(self, data, file_path):
with self.lock:
with open(file_path, 'a', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False)
file.write('\n')
def multiple_gpt(self, payload):
while True:
try:
chat_completion = openai.ChatCompletion.create(model=payload['model'], temperature=0, messages=payload['messages'])
data = payload.copy()
data['messages'] = payload['messages']
data['answer'] = payload['answer']
data['output'] = chat_completion.choices[0].message.content
break
except Exception as e:
time.sleep(random.randint(1, 3))
self.write_to_json(data, payload['save_path'])
time.sleep(random.randint(1, 3))
def format_prompt_4(d, args):
if args.language == 'zh':
cA = d['选项A'].replace("A. ", "")
cB = d['选项B'].replace("B. ", "")
cC = d['选项C'].replace("C. ", "")
cD = d['选项D'].replace("D. ", "")
choices = [cA, cB, cC, cD]
random.shuffle(choices)
prompt = UserEvaluatePrompt4Choices_zh.format(story=d['故事'], question=d['问题'], choice_a=choices[0], choice_b=choices[1], choice_c=choices[2], choice_d=choices[3])
map = {"A": "", "B": "", "C": "", "D": ""}
if choices[0] == cA:
map['A'] = 'A'
elif choices[0] == cB:
map['A'] = 'B'
elif choices[0] == cC:
map['A'] = 'C'
elif choices[0] == cD:
map['A'] = 'D'
if choices[1] == cA:
map['B'] = 'A'
elif choices[1] == cB:
map['B'] = 'B'
elif choices[1] == cC:
map['B'] = 'C'
elif choices[1] == cD:
map['B'] = 'D'
if choices[2] == cA:
map['C'] = 'A'
elif choices[2] == cB:
map['C'] = 'B'
elif choices[2] == cC:
map['C'] = 'C'
elif choices[2] == cD:
map['C'] = 'D'
if choices[3] == cA:
map['D'] = 'A'
elif choices[3] == cB:
map['D'] = 'B'
elif choices[3] == cC:
map['D'] = 'C'
elif choices[3] == cD:
map['D'] = 'D'
else:
cA = d['OPTION-A'].replace("A. ", "")
cB = d['OPTION-B'].replace("B. ", "")
cC = d['OPTION-C'].replace("C. ", "")
cD = d['OPTION-D'].replace("D. ", "")
choices = [cA, cB, cC, cD]
random.shuffle(choices)
prompt = UserEvaluatePrompt4Choices_en.format(story=d['STORY'], question=d['QUESTION'], choice_a=choices[0], choice_b=choices[1], choice_c=choices[2], choice_d=choices[3])
map = {"A": "", "B": "", "C": "", "D": ""}
if choices[0] == cA:
map['A'] = 'A'
elif choices[0] == cB:
map['A'] = 'B'
elif choices[0] == cC:
map['A'] = 'C'
elif choices[0] == cD:
map['A'] = 'D'
if choices[1] == cA:
map['B'] = 'A'
elif choices[1] == cB:
map['B'] = 'B'
elif choices[1] == cC:
map['B'] = 'C'
elif choices[1] == cD:
map['B'] = 'D'
if choices[2] == cA:
map['C'] = 'A'
elif choices[2] == cB:
map['C'] = 'B'
elif choices[2] == cC:
map['C'] = 'C'
elif choices[2] == cD:
map['C'] = 'D'
if choices[3] == cA:
map['D'] = 'A'
elif choices[3] == cB:
map['D'] = 'B'
elif choices[3] == cC:
map['D'] = 'C'
elif choices[3] == cD:
map['D'] = 'D'
return map, prompt
def format_prompt_2(d, args):
if args.language == 'zh':
cA = d['选项A'].replace("A. ", "")
cB = d['选项B'].replace("B. ", "")
choices = [cA, cB]
random.shuffle(choices)
prompt = UserEvaluatePrompt2Choices_zh.format(story=d['故事'], question=d['问题'], choice_a=choices[0], choice_b=choices[1])
map = {"A": "", "B": "", "C": "", "D": ""}
if choices[0] == cA:
map['A'] = 'A'
elif choices[0] == cB:
map['A'] = 'B'
if choices[1] == cA:
map['B'] = 'A'
elif choices[1] == cB:
map['B'] = 'B'
else:
cA = d['OPTION-A'].replace("A. ", "")
cB = d['OPTION-B'].replace("B. ", "")
choices = [cA, cB]
random.shuffle(choices)
prompt = UserEvaluatePrompt2Choices_en.format(story=d['STORY'], question=d['QUESTION'], choice_a=choices[0], choice_b=choices[1])
map = {"A": "", "B": "", "C": "", "D": ""}
if choices[0] == cA:
map['A'] = 'A'
elif choices[0] == cB:
map['A'] = 'B'
if choices[1] == cA:
map['B'] = 'A'
elif choices[1] == cB:
map['B'] = 'B'
return map, prompt
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--task", type=str, default="")
parser.add_argument("--model_name", type=str, default="")
parser.add_argument("--api_base", type=str, default="")
parser.add_argument("--api_key", type=str, default="")
parser.add_argument("--language", type=str, default="zh")
parser.add_argument("--try_times", type=int, default=5)
parser.add_argument("--cot", type=bool, default=False)
parser.add_argument('--output_path', type=str, default="./results")
parser.add_argument("--seed", type=int, default=42)
args = parser.parse_args()
random.seed(args.seed)
processor = ChatGPTProcessor()
files = os.listdir("./data")
if args.task != "":
files = [args.task]
for file in files:
task = file.split(".")[0]
try:
with open(f"data/{file}", "r", encoding='utf-8') as f:
data = [json.loads(line) for line in f.readlines()]
except:
continue
payloads = []
for i, d in enumerate(data):
for j in range(args.try_times):
if d['选项C'] != None:
maps, prompt = format_prompt_4(d, args)
else:
maps, prompt = format_prompt_2(d, args)
system_prompt = ""
if args.language == "zh":
if args.cot == False:
system_prompt = SystemEvaluatePrompt_zh
else:
system_prompt = SystemEvaluatePrompt_zh_cot
else:
if args.cot == False:
system_prompt = SystemEvaluatePrompt_en
else:
system_prompt = SystemEvaluatePrompt_en_cot
payload = {
"model": args.model_name,
"stream": False,
"top_p": 0.99,
"temperature": 0,
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt}
],
"idx" : i,
"number" : j,
"data" : d,
"map" : maps,
"answer" : d['答案\nANSWER'],
"save_path" : f"{args.output_path}/{task}_{args.model_name}_results.jsonl"
}
payloads.append(payload)
with ThreadPoolExecutor(max_workers=32) as executor:
for payload in payloads:
executor.submit(processor.multiple_gpt, payload)