-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGPT_T2T_generation.py
464 lines (374 loc) · 18.9 KB
/
GPT_T2T_generation.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import sys
import json
import openai
import random
import asyncio
import argparse
import platform
from tenacity import retry, stop_after_attempt, wait_random_exponential
sys.path.append('')
from utils import *
if platform.system()=='Windows':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
@retry(wait=wait_random_exponential(min=5, max=60), stop=stop_after_attempt(3))
async def generate_response(prompt, engine):
try:
if engine == CHAT_GPT or engine == GPT4:
response = openai.ChatCompletion.create(
messages=[{'role': 'user', 'content': prompt}],
model=engine,
temperature=args.temperature,
top_p=args.top_p,
)
else:
response = openai.Completion.create(
model=engine,
prompt=prompt,
temperature=args.temperature,
top_p=args.top_p,
max_tokens=1000,
)
return response
except openai.error.APIError as e:
if e.status == 429:
print("Rate limited. Waiting and retrying...")
asyncio.sleep(1)
else:
raise
async def call_api_direct(prompt_list, engine):
semaphore = asyncio.Semaphore(20)
tasks = []
for prompt in prompt_list:
async with semaphore:
task = asyncio.create_task(generate_response(prompt, engine))
tasks.append(task)
responses = await asyncio.gather(*tasks)
return responses
@retry(wait=wait_random_exponential(min=5, max=60), stop=stop_after_attempt(5))
async def call_api_improve(prompt_list, engine):
if engine == CHAT_GPT or engine == GPT4:
async_responses = [
openai.ChatCompletion.acreate(
messages=[{'role': 'user', 'content': prompt}],
model=engine,
temperature=args.temperature,
top_p=args.top_p,
)
for prompt in prompt_list
]
else:
async_responses = [
openai.Completion.acreate(
model=engine,
prompt=prompt,
temperature=args.temperature,
top_p=args.top_p,
max_tokens=1000,
)
for prompt in prompt_list
]
return await asyncio.gather(*async_responses)
# if random shuffle needed
def shuffle_sents_labels(sents, labels, seed=2):
combine = list(zip(sents, labels))
random.seed(seed)
random.shuffle(combine)
shuffle_sents, shuffle_labels = zip(*combine)
return shuffle_sents, shuffle_labels
def LogicNLG_direct(engine, direct_mode, data_path, prompt_path, num_limit, output_path):
if data_path == None:
data_path = LOGICNLG_PATH
data = read_json(data_path=data_path)
csv_id_list= []
prompt_list = []
count = 0
if prompt_path == None:
prompt_path = 'prompts/GPT/LogicNLG/prompt_LogicNLG_table2text_%s.txt' %direct_mode
with open(prompt_path, 'r', encoding='utf-8') as load_file:
prompt_template = load_file.read()
for cur_id, current in data.items():
if count >= num_limit:
break
print('Processing Table ' + str(count + 1) + '...')
csv_id_list.append(current['csv_id'])
title = current['title']
table = current['table_text'].replace('<br>', '\n')
prompt = prompt_template.replace('{title}', title).replace('{table}', table)
prompt_list.append(prompt)
count += 1
loop = asyncio.get_event_loop()
response_list = loop.run_until_complete(call_api_direct(prompt_list=prompt_list, engine=engine))
keywords = ["Claim 1: ", "Claim 2: ", "Claim 3: ", "Claim 4: ", "Claim 5: "]
write_data = direct_postprocess(engine=engine, table_id_list=csv_id_list, response_list=response_list, keywords=keywords)
output_path = get_exact_output_path(output_path=output_path, engine=engine, dataset='LogicNLG', mode='direct', direct_mode=direct_mode)
json.dump(write_data, open(output_path, "w"), indent=4)
def LogicNLG_improve(engine, finetuned_model_path, prompt_path, num_limit, output_path, num_paths):
data = read_json(data_path=finetuned_model_path)
output_data = {}
count = 0
if prompt_path == None:
prompt_path = 'prompts/GPT/LogicNLG/prompt_LogicNLG_improve.txt'
with open(prompt_path, 'r', encoding='utf-8') as load_file:
prompt_template = load_file.read()
for cur_id, current in data.items():
if count >= num_limit:
break
print('Processing Table ' + str(count + 1) + '...')
title = current['title']
table = current['table_text'].replace('<br>', '\n')
csv_id = current['csv_id']
sentences = current['sentences']
data[cur_id]['sentences'] = sentences
feedback_list = []
for sent in sentences:
prompt = prompt_template.replace('{title}', title).replace('{table}', table).replace('{sent}', sent)
prompt_list = []
for _ in range(num_paths):
prompt_list.append(prompt) # the same prompt for num_paths times
responses = asyncio.run(call_api_improve(prompt_list=prompt_list, engine=engine))
keywords = ["New claim:", "new claim:","new Claim:","New Claim:", "modified claim:", "modified Claim:" "Modified Claim:", "Modified claim:","Edited Claim:","edited Claim:","edited claim:","Edited claim:", "updated claim:", "Updated claim:","updated Claim:","Updated Claim:"]
pred_vote = improve_postprocess(engine=engine, responses=responses, keywords=keywords, ori_sent=sent)
if len(pred_vote['Entailed']) > len(pred_vote['Refuted']):
feedback_list.append(pred_vote['Entailed'][0].replace('\n',''))
else:
feedback_list.append(pred_vote['Refuted'][0].replace('\n',''))
output_data[csv_id] = feedback_list
count += 1
finetuned_model_names = ['GPT2', 'r2d2', 't5-base', 'plog-t5-large', 'loft']
for finetuned in finetuned_model_names:
if finetuned in finetuned_model_path:
finetuned_model = finetuned
break
output_path = get_exact_output_path(output_path=output_path, engine=engine, dataset='LogicNLG', mode='improve', finetuned_model=finetuned_model)
json.dump(output_data, open(output_path, "w"), indent=4)
def FeTaQA_direct(engine, direct_mode, data_path, prompt_path, num_limit, output_path):
if data_path == None:
data_path = FETAQA_PATH
data = read_json(data_path=data_path)
feta_id_list= []
prompt_list = []
count = 0
if prompt_path == None:
prompt_path = 'prompts/GPT/FeTaQA/prompt_FeTaQA_table2text_%s.txt' %direct_mode
with open(prompt_path, 'r', encoding='utf-8') as load_file:
prompt_template = load_file.read()
for cur_id, current in data.items():
if count >= num_limit:
break
print('Processing Table ' + str(count + 1) + '...')
feta_id_list.append(current['feta_id'])
page_title = current['page_title']
section_title = current['section_title']
question = current['question']
table = current['table_text'].replace('<br>', '\n')
prompt = prompt_template.replace('{page_title}', page_title).replace('{section_title}', section_title).replace('{question}', question).replace('{table}', table)
prompt_list.append(prompt)
count += 1
loop = asyncio.get_event_loop()
response_list = loop.run_until_complete(call_api_direct(prompt_list=prompt_list, engine=engine))
keywords = ["Answer: ", "answer: "]
write_data = direct_postprocess(engine=engine, table_id_list=feta_id_list, response_list=response_list, keywords=keywords)
if 'CoT' in direct_mode:
write_data = FeTaQA_F2WTQ_CoT_clean(write_data)
output_path = get_exact_output_path(output_path=output_path, engine=engine, dataset='FeTaQA', mode='direct', direct_mode=direct_mode)
json.dump(write_data, open(output_path, "w"), indent=4)
def FeTaQA_improve(engine, finetuned_model_path, prompt_path, num_limit, output_path, num_paths):
answer_data = read_json(data_path=finetuned_model_path)
table_data = read_json(data_path=FETAQA_PATH)
output_data = {}
count = 0
for cur_id, current in table_data.items():
if count >= num_limit:
break
print('Processing Table ' + str(count + 1) + '...')
page_title = current['page_title']
section_title = current['section_title']
question = current['question']
table = current['table_text'].replace('<br>', '\n')
feta_id = str(current['feta_id'])
answer = answer_data[feta_id]
if prompt_path == None:
prompt_path = 'prompts/GPT/FeTaQA/prompt_FeTaQA_improve.txt'
with open(prompt_path, 'r', encoding='utf-8') as load_file:
prompt_template = load_file.read()
prompt = prompt_template.replace('{page_title}', page_title).replace('{section_title}', section_title).replace('{question}', question).replace('{answer}', answer).replace('{table}', table)
prompt_list = []
for _ in range(num_paths):
prompt_list.append(prompt) # the same prompt for num_paths times
responses = asyncio.run(call_api_improve(prompt_list=prompt_list, engine=engine))
keywords = ["New answer:", "new answer:", "new Answer:", "New Answer:", "modified answer:", "modified Answer:", "Modified Answer:", "Modified answer:", "Edited Answer:", "edited Answer:", "edited answer:", "Edited answer:", "updated answer:", "Updated answer:", "updated Answer:", "Updated Answer:"]
pred_vote = improve_postprocess(engine=engine, responses=responses, keywords=keywords, ori_sent=answer)
if len(pred_vote['Entailed']) > len(pred_vote['Refuted']):
feedback = pred_vote['Entailed'][0].replace('\n','')
else:
feedback = pred_vote['Refuted'][0].replace('\n','')
output_data[feta_id] = feedback
count += 1
finetuned_model_names = ['bart_large', 'flan_t5_large', 'omnitab_large', 'reastap_large', 'tapex_large']
for finetuned in finetuned_model_names:
if finetuned in finetuned_model_path:
finetuned_model = finetuned
break
output_path = get_exact_output_path(output_path=output_path, engine=engine, dataset='FeTaQA', mode='improve', finetuned_model=finetuned_model)
json.dump(output_data, open(output_path, "w"), indent=4)
def F2WTQ_direct(engine, direct_mode, data_path, prompt_path, num_limit, output_path):
if data_path == None:
data_path = F2WTQ_PATH
data = read_json(data_path=data_path)
table_id_list= []
prompt_list = []
count = 0
if prompt_path == None:
prompt_path = 'prompts/GPT/F2WTQ/prompt_F2WTQ_table2text_%s.txt' %direct_mode
with open(prompt_path, 'r', encoding='utf-8') as load_file:
prompt_template = load_file.read()
for cur_id, current in data.items():
if count >= num_limit:
break
print('Processing Table ' + str(count + 1) + '...')
table_id_list.append(current['id'])
question = current['new_question']
table = current['table_text'].replace('<br>', '\n')
prompt = prompt_template.replace('{question}', question).replace('{table}', table)
prompt_list.append(prompt)
count += 1
loop = asyncio.get_event_loop()
response_list = loop.run_until_complete(call_api_direct(prompt_list=prompt_list, engine=engine))
keywords = ["Answer: ", "answer: "]
write_data = direct_postprocess(engine=engine, table_id_list=table_id_list, response_list=response_list, keywords=keywords)
if 'CoT' in direct_mode:
write_data = FeTaQA_F2WTQ_CoT_clean(write_data)
output_path = get_exact_output_path(output_path=output_path, engine=engine, dataset='F2WTQ', mode='direct', direct_mode=direct_mode)
json.dump(write_data, open(output_path, "w"), indent=4)
def LoTNLG_direct(engine, direct_mode, data_path, prompt_path, num_limit, output_path):
if data_path == None:
data_path = LOTNLG_PATH
data = read_json(data_path=data_path)
csv_id_list= []
prompt_list = []
count = 0
if prompt_path == None:
prompt_path = 'prompts/GPT/LoTNLG/prompt_LoTNLG_table2text_%s.txt' %direct_mode
with open(prompt_path, 'r', encoding='utf-8') as load_file:
prompt_template = load_file.read()
for cur_id, current in data.items():
if count >= num_limit:
break
print('Processing Table ' + str(count + 1) + '...')
csv_id_list.append(current['csv_id'])
title = current['title']
table = current['table_text'].replace('<br>', '\n')
logical_labels = current['logical_labels']
logical_labels_str = ''
for k in range(len(logical_labels)):
logical_labels_str += f'Logical label {k+1}: {logical_labels[k]}\n'
prompt = prompt_template.replace('{title}', title).replace('{table}', table).replace('{logical_labels}', logical_labels_str)
prompt_list.append(prompt)
count += 1
loop = asyncio.get_event_loop()
response_list = loop.run_until_complete(call_api_direct(prompt_list=prompt_list, engine=engine))
keywords = ["Claim 1: ", "Claim 2: ", "Claim 3: ", "Claim 4: ", "Claim 5: "]
write_data = direct_postprocess(engine=engine, table_id_list=csv_id_list, response_list=response_list, keywords=keywords)
output_path = get_exact_output_path(output_path=output_path, engine=engine, dataset='LoTNLG', mode='direct', direct_mode=direct_mode)
json.dump(write_data, open(output_path, "w"), indent=4)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--api_org", default=None, type=str, required=True,
help='your organization No. for openai') # org No. for openai
parser.add_argument("--api_key", default=None, type=str, required=True,
help='your api key for openai') # api key for openai
parser.add_argument("--engine", default=None, type=str, required=True,
help='which openai engine to use, can select from ["text-davinci-002", "text-davinci-003", "gpt-3.5-turbo", "gpt-4"]') # openai engine
parser.add_argument("--temperature", type=float, default=0.7,
help='temperature value for openai model, can select from 0 to 2')
parser.add_argument("--top_p", type=float, default=1.0,
help='top_p value for openai model, can select from 0 to 1')
parser.add_argument("--mode", default=None, type=str, required=True,
help='which mode to use, can select from ["direct", "improve"]')
parser.add_argument("--direct_mode", type=str, default='two_shot_CoT',
help='which direct submode to use, only valid when you choose "direct" as mode, can select from ["zero_shot", "one_shot", "two_shot", "one_shot_CoT", "two_shot_CoT"]')
parser.add_argument("--prompt_path", default=None, type=str,
help='which prompt to use, only valid when you choose "direct" as mode. If not specified, we will use the default prompt for each direct mode')
parser.add_argument("--dataset", default=None, type=str, required=True,
help='which dataset to use, can select from ["LogicNLG", "FeTaQA", "F2WTQ", "LoTNLG"]')
parser.add_argument("--data_path", default=None, type=str,
help='original data path for the dataset. If not specified, we will use the default path for each dataset')
parser.add_argument("--finetuned_model_path", type=str,
help='output path of finetuned small model for the dataset, only valid when you choose "improve" as mode')
parser.add_argument("--output_path", type=str, default="outputs",
help='output path for the generated data')
parser.add_argument("--num_limit", type=int, default=10,
help='number of tables used to generate')
parser.add_argument("--num_paths", type=int, default=3,
help='number of paths used to improve using self-consistency mechanism, only valid when you choose "improve" as mode')
args = parser.parse_args()
openai.organization = args.api_org
openai.api_key = args.api_key
if args.dataset == 'LogicNLG':
if args.mode == 'direct':
LogicNLG_direct(
engine=args.engine,
direct_mode=args.direct_mode,
data_path=args.data_path,
prompt_path=args.prompt_path,
num_limit=args.num_limit,
output_path=args.output_path
)
elif args.mode == 'improve':
LogicNLG_improve(
engine=args.engine,
finetuned_model_path=args.finetuned_model_path,
prompt_path=args.prompt_path,
num_limit=args.num_limit,
output_path=args.output_path,
num_paths=args.num_paths
)
else:
raise ValueError("Invalid mode name")
elif args.dataset == 'FeTaQA':
if args.mode == 'direct':
FeTaQA_direct(
engine=args.engine,
direct_mode=args.direct_mode,
data_path=args.data_path,
prompt_path=args.prompt_path,
num_limit=args.num_limit,
output_path=args.output_path
)
elif args.mode == 'improve':
FeTaQA_improve(
engine=args.engine,
finetuned_model_path=args.finetuned_model_path,
prompt_path=args.prompt_path,
num_limit=args.num_limit,
output_path=args.output_path,
num_paths=args.num_paths
)
else:
raise ValueError("Invalid mode name")
elif args.dataset == 'F2WTQ':
if args.mode == 'direct':
F2WTQ_direct(
engine=args.engine,
direct_mode=args.direct_mode,
data_path=args.data_path,
prompt_path=args.prompt_path,
num_limit=args.num_limit,
output_path=args.output_path
)
else:
raise ValueError("Invalid mode name")
elif args.dataset == 'LoTNLG':
if args.mode == 'direct':
LoTNLG_direct(
engine=args.engine,
direct_mode=args.direct_mode,
data_path=args.data_path,
prompt_path=args.prompt_path,
num_limit=args.num_limit,
output_path=args.output_path
)
else:
raise ValueError("Invalid mode name")
else:
raise ValueError("Invalid dataset name")