-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.py
174 lines (152 loc) · 5.9 KB
/
generate.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
import argparse
import os
import logging
from src.jsonl import load_all_jsonl, config_log, dump_jsonl
from src.evaluation import eval_recall
from src.model import Gen
from src.data import find_with_question
from src.key import used_keys
from src.model_llama import Gen_llama
llama_path = [
'llama_model/7b-chat',
'llama_model/13b-chat',
'llama_model/7b-chat-hf',
'llama_model/13b-chat-hf'
]
api_args = {
'engine':'',
'api_key':used_keys,
'temperature':0,
'max_tokens':300
}
def evaluate_recall(result):
recall, len = eval_recall(result, 'response')
return {
"recall": recall,
"length": len
}
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# Required parameters
parser.add_argument("--dataset", default=None, type=str, required=True,
help="dataset name: [nq, tqa, webq, wizard, fever, fm2]",
)
parser.add_argument("--type", default=None, type=str, required=True,
help="type: [Gen, RAGen, RAGen_full, RAGen_completion], should be either 1 or 2",
)
parser.add_argument("--split", default=None, type=str, required=True,
help="dataset split: [train, dev, test]",
)
parser.add_argument("--engine", default='text-davinci-002', type=str, required=False,
help="text-davinci-002 (used in our experiments), code-davinci-002",
)
parser.add_argument("--ctxs_file", type=str, default='none', required=False)
parser.add_argument('--ctxs_key', type=str, default='none', required=False)
parser.add_argument('--ctxs_num', type=int, default=1, required=False)
parser.add_argument("--decoding", default='greedy', type=str, required=False, help='[greedy, sample], affect the temperature')
parser.add_argument("--pid", default=1, type=int)
parser.add_argument('--process_num', type=int, default=0)
# only used when using local model like llama
parser.add_argument('--batch_size', type=int, default=1)
parser.add_argument("--device_map", type=str, default='sequential')
parser.add_argument("--debug_mode", action='store_true')
args = parser.parse_args()
if args.dataset in ['nq', 'webq', 'tqa', 'twiki']:
datatype = 'question answering'
elif args.dataset in ['fever', 'fm2']:
datatype = 'fact checking'
elif args.dataset in ['wizard']:
datatype = 'dialogue system'
else:
raise NotImplementedError
# api config
api_args['engine'] = args.engine
api_args['max_tokens'] = 300
if args.decoding == 'greedy':
api_args['temperature'] = 0
else:
raise Exception("Do not support other sample method")
# output path
outputfolder = f'Generated-context-{args.decoding}-{args.engine}/{args.dataset}-{args.split}'
os.makedirs(outputfolder, exist_ok=True)
outputfile = f'{outputfolder}/{args.type}-with-{args.ctxs_num}-{args.ctxs_key}-p{args.pid}.jsonl'
metricfile = f'{outputfolder}/Recal@k.jsonl'
config_log(outputfolder, f'{args.type}-with-{args.ctxs_num}-{args.ctxs_key}-p{args.pid}')
# get prompt_template
prompt_list = load_all_jsonl('source/prompt.jsonl')
prompt_template = None
for p in prompt_list:
if p['task'] == datatype and p['type'] == args.type and p['pid'] == args.pid:
prompt_template = p['prompt_template']
break
if prompt_template==None: raise Exception("fail to find prompt template")
# qa data
inputfile = f'indatasets/{args.dataset}/{args.dataset}-{args.split}.jsonl'
qa_data = load_all_jsonl(inputfile)
# add ctxs
if args.type in ['RAGen', 'RAGen_all', 'RAGen_completion']:
ctxsfile = args.ctxs_file
assert ctxsfile != 'none'
ctxsdata = load_all_jsonl(ctxsfile)
else:
print("ctxs is none")
ctxsdata = None
data = []
for k, qa in enumerate(qa_data):
item={
"question": qa['question'],
"answer": qa['answer'],
}
if ctxsdata != None:
# assert ctxsdata[k]['reference'] == item['answer']
ctxs_item = find_with_question(qa['question'], ctxsdata)
if ctxs_item == None: raise Exception("Can not find questin {}".format(qa['question']))
item['ctxs'] = ctxs_item[args.ctxs_key][:args.ctxs_num]
data.append(item)
logging.info ("Total data to process {}".format(len(data)))
logging.info("\n Data[0]:{}".format(data[0]))
# debug model
if args.debug_mode:
data = data[:100]
logging.error("Start debug mode, only process 100 examples")
# debug
debug_info = {
"dataset": args.dataset,
"args": vars(args),
"outputfile": outputfile,
"prompt_template": prompt_template
}
logging.info(debug_info)
# prepare llama model
if args.engine in llama_path:
logging.info('Using llama model in {}'.format(args.engine))
if args.type == 'Gen':
llm = Gen_llama(prompt_template, outputfile, api_args, batch_size=args.batch_size, device_map=args.device_map)
else:
raise Exception("Unexpected type {}".format(args.type))
# llama batch request
result = llm.forward(data)
# API request
else:
# create llm
if args.type == 'Gen':
llm = Gen(prompt_template, outputfile, api_args, process_num=args.process_num)
else:
raise Exception("Unexpected type {}".format(args.type))
# multi process request
result = llm.forward_multi_thread(data)
# read the whole result
if len(result) != len(data):
result = load_all_jsonl(outputfile)
# evaluation
recall_dict = evaluate_recall(result)
metric_file = f'{outputfolder}/metric.jsonl'
metric = {
"recall": recall_dict,
"dataset": args.dataset,
"args": vars(args),
"outputfile": outputfile,
"prompt_template": prompt_template
}
logging.info(metric)
dump_jsonl(metric, metric_file)