-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrewrite_kqa.py
More file actions
315 lines (254 loc) · 9.92 KB
/
rewrite_kqa.py
File metadata and controls
315 lines (254 loc) · 9.92 KB
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
import torch
import argparse
import json
import pickle
import numpy as np
from collections import Counter
from tqdm import tqdm
from tqdm.contrib import tzip
from transformers import AutoModelForCausalLM, AutoTokenizer
def get_args():
parser = argparse.ArgumentParser(description="Process model configurations for LLM evaluation.")
parser.add_argument("--device_respond", default='cuda:3', help="Device for responding")
parser.add_argument("--test", default=0, help="Test")
return parser.parse_args()
def ask(prompt):
messages = [
{"role": "user", "content": prompt}
]
input_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt"
).to(model.device)
terminators = [
tokenizer.eos_token_id,
tokenizer.convert_tokens_to_ids("<|eot_id|>")
]
outputs = model.generate(
input_ids,
max_new_tokens=512,
eos_token_id=terminators,
do_sample = False
)
response = outputs[0][input_ids.shape[-1]:]
return tokenizer.decode(response, skip_special_tokens=True)
def ask_sample_1(prompt):
messages = [
{"role": "user", "content": prompt}
]
input_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt"
).to(model.device)
terminators = [
tokenizer.eos_token_id,
tokenizer.convert_tokens_to_ids("<|eot_id|>")
]
outputs = model.generate(
input_ids,
max_new_tokens=512,
eos_token_id=terminators,
do_sample = True,
temperature=1,
top_p=0.999,
)
response = outputs[0][input_ids.shape[-1]:]
return tokenizer.decode(response, skip_special_tokens=True)
def ask_sample(prompt):
messages = [
{"role": "user", "content": prompt}
]
input_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt"
).to(model.device)
terminators = [
tokenizer.eos_token_id,
tokenizer.convert_tokens_to_ids("<|eot_id|>")
]
outputs = model.generate(
input_ids,
max_new_tokens=512,
eos_token_id=terminators,
do_sample = True,
temperature=0.6,
top_p=0.9,
)
response = outputs[0][input_ids.shape[-1]:]
return tokenizer.decode(response, skip_special_tokens=True)
def read_questions_from_jsonl(file_path):
questions = []
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
data = json.loads(line)
questions.append(data["Question"])
return questions
def most_frequent(entail_result_temp):
if len(entail_result_temp) == 0:
return 0
count = Counter(entail_result_temp)
max_count = max(count.values())
frequent_elements = [key for key, value in count.items() if value == max_count]
if len(frequent_elements) > 1:
return 1
return frequent_elements[0]
def is_entails(question, llm_answer, answer):
with open(f"datasets/K-QA/prompts/is_entails.txt", "r") as f:
prompt_template_entails = f.read()
prompt = prompt_template_entails.replace('{question}', question).replace('{llm_answer}', llm_answer).replace('{answer}', answer)
answer = ask(prompt)
if 'answer is False' in answer or 'answer is false' in answer or 'the answer as False' in answer:
return 0
elif 'answer is True' in answer or 'answer is true' in answer:
return 1
else:
entail_result_temp = []
for i in range(11):
answer = ask_sample(prompt)
if 'answer is False' in answer or 'answer is false' in answer or 'the answer as False' in answer:
entail_result_temp.append(0)
elif 'answer is True' in answer or 'answer is true' in answer:
entail_result_temp.append(1)
if len(entail_result_temp) == 3:
break
return most_frequent(entail_result_temp)
def is_contradict(question, llm_answer, answer):
with open(f"datasets/K-QA/prompts/is_contradict.txt", "r") as f:
prompt_template_contradict = f.read()
prompt = prompt_template_contradict.replace('{question}', question).replace('{llm_answer}', llm_answer).replace('{answer}', answer)
answer = ask(prompt)
if 'answer is False' in answer or 'answer is false' in answer or 'the answer as False' in answer:
return 0
elif 'answer is True' in answer or 'answer is true' in answer:
return 1
else:
entail_result_temp = []
for i in range(11):
answer = ask_sample(prompt)
if 'answer is False' in answer or 'answer is false' in answer or 'the answer as False' in answer:
entail_result_temp.append(0)
elif 'answer is True' in answer or 'answer is true' in answer:
entail_result_temp.append(1)
if len(entail_result_temp) == 3:
break
return most_frequent(entail_result_temp)
def get_score(question_list, answer_list, mh_list):
evaluate_entails = []
for question, llm_answer, mh in tzip(question_list, answer_list, mh_list):
result_single = []
for answer in mh:
result_single.append(is_entails(question, llm_answer, answer))
evaluate_entails.append(np.array(result_single).sum() / len(mh))
evaluate_contradict = []
for question, llm_answer, mh in tzip(question_list, answer_list, mh_list):
result_single = []
for answer in mh:
result_single.append(is_contradict(question, llm_answer, answer))
evaluate_contradict.append(np.array(result_single).sum())
return evaluate_entails, evaluate_contradict
def rewrite_questions(question):
rewrite_ask_list = []
if int(test):
times = 2
else:
times = 10000
for i in range(times):
try:
a = ask_sample_1('Rewriting question to make it more understandable, just give me the rewritten question without any other word: ' + question)
if a not in rewrite_ask_list:
rewrite_ask_list.append(a)
print(a)
if len(rewrite_ask_list) == 100:
break
except:
continue
return rewrite_ask_list
def generate_rewrite(questions_list, mh_list, name):
try:
with open('data/rewrite/rewrite_kqa_100_{}.pkl'.format(name), 'rb') as f:
rewrite_q = pickle.load(f)
except:
rewrite_q = []
for q in tqdm(questions_list[len(rewrite_q):]):
rewrite_q.append(rewrite_questions(q))
with open('rewrite_kqa_100_{}.pkl'.format(name), 'wb') as f:
pickle.dump(rewrite_q, f)
with open('data/rewrite/rewrite_kqa_100_{}.pkl'.format(name), 'wb') as f:
pickle.dump(rewrite_q, f)
try:
with open('data/rewrite/rewrite_kqa_100_answer_{}.pkl'.format(name), 'rb') as f:
rewrite_a = pickle.load(f)
except:
rewrite_a = []
for q_list in tqdm(rewrite_q[len(rewrite_a):]):
temp_rewrite_a = []
for q in q_list:
answer = ask(q)
temp_rewrite_a.append(answer)
rewrite_a.append(temp_rewrite_a)
with open('data/rewrite/rewrite_kqa_100_answer_{}.pkl'.format(name), 'wb') as f:
pickle.dump(rewrite_a, f)
with open('data/rewrite/rewrite_kqa_100_answer_{}.pkl'.format(name), 'wb') as f:
pickle.dump(rewrite_a, f)
try:
with open('data/rewrite/rewrite_kqa_100_score_{}.pkl'.format(name), 'rb') as f:
rewrite_s = pickle.load(f)
except:
rewrite_s = []
for q_list, a_list in tzip(rewrite_q[len(rewrite_s):], rewrite_a[len(rewrite_s):]):
rewrite_s.append(get_score(q_list, a_list, mh_list))
with open('data/rewrite/rewrite_kqa_100_score_{}.pkl'.format(name), 'wb') as f:
pickle.dump(rewrite_s, f)
with open('data/rewrite/rewrite_kqa_100_score_{}.pkl'.format(name), 'wb') as f:
pickle.dump(rewrite_s, f)
def main():
global test
global tokenizer
global model
args = get_args()
device_respond = args.device_respond
test = args.test
model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.bfloat16
)
model.to(device_respond)
np.random.seed(1024)
indices = np.arange(201)
test_indices = np.random.choice(indices, size=50, replace=False)
train_indices = np.setdiff1d(indices, test_indices)
val_indices = np.random.choice(train_indices, size=50, replace=False)
train_indices = np.setdiff1d(train_indices, val_indices)
file_path = 'datasets/K-QA/dataset/questions_w_answers.jsonl'
questions_list = read_questions_from_jsonl(file_path)
mh_list = []
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
data = json.loads(line)
mh_list.append(data['Must_have'])
questions_list = [questions_list[i] for i in train_indices]
mh_list = [mh_list[i] for i in train_indices]
if int(test):
questions_list = [questions_list[i] for i in [0,1]]
mh_list = [mh_list[i] for i in [0,1]]
generate_rewrite(questions_list, mh_list, 'train')
file_path = 'datasets/K-QA/dataset/questions_w_answers.jsonl'
questions_list = read_questions_from_jsonl(file_path)
mh_list = []
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
data = json.loads(line)
mh_list.append(data['Must_have'])
questions_list = [questions_list[i] for i in val_indices]
mh_list = [mh_list[i] for i in val_indices]
if int(test):
questions_list = [questions_list[i] for i in [0,1]]
mh_list = [mh_list[i] for i in [0,1]]
generate_rewrite(questions_list, mh_list, 'val')
if __name__ == "__main__":
main()