-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeneral_check.py
308 lines (240 loc) · 9.19 KB
/
general_check.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
import sys
import json
import time
import argparse
import openai
from collections import Counter
from nltk.tokenize import regexp_tokenize, sent_tokenize
from nltk.translate.bleu_score import sentence_bleu
from analysis import load_eval_data
try:
from pyserini.search import FaissSearcher
except:
pass
import warnings
warnings.filterwarnings("ignore")
openai.api_key = open('openai-key.txt').read().strip()
parser = argparse.ArgumentParser(
prog='python general_check.py',
description='Unified language safety checking with LLMs.',
epilog='Submit issues on Github for addtional help.'
)
parser.add_argument('-t', '--task', type=str, help = 'Language safety checking task.')
parser.add_argument('-m', '--mode', type=str, help = 'Prompting mode [zero | fp | cot].')
parser.add_argument('-s', '--start-idx', type=int, help = 'Index of the first sample to process.')
parser.add_argument('-n', '--exp-name', type=int, help = 'Name tag for the experiment log file.')
parser.add_argument('-v', '--verbose', action='store_true')
def build_background(hits):
bg_list = []
for hit in hits:
res_title = hit.docid
ret_text = json.loads(searcher.doc(res_title).raw())['contents']
ret_text = ret_text.split('\n')[1]
bg_list.append(ret_text)
return '\n\n'.join(bg_list)
def is_answerable(ans_str):
ans_str = ans_str.lower()
if 'however, ' in ans_str:
return True
signal_list = [
'i\'m sorry', 'language model', 'i cannot',
'not provide', 'provide more information'
]
for signal in signal_list:
if signal in ans_str:
return False
return False
def load_dataset(domain = None, split = 'dev'):
def gather_evidence(evi_list):
evi_title_list = []
for evi_obj in evi_list:
contents = evi_obj['content']
title_list = [x.split('_')[0] for x in contents]
evi_title_list += title_list
return set(evi_title_list)
def extract_climate_case(jsonl):
case = json.loads(jsonl)
evi_set = None
return (case['claim'], case['claim_label'], evi_set)
def extract_scifact_case(jsonl):
case = json.loads(jsonl)
evi_set = None
return (case['claim'], case['label'], evi_set)
def extract_covid_case(jsonl):
case = json.loads(jsonl)
evi_set = None
label_dict = {
'SUPPORTED': 'SUPPORTS',
'REFUTED': 'REFUTES'
}
return (case['claim'], label_dict[case['label']], evi_set)
fn = f'ulsc_data/{args.task}_claims.jsonl'
if domain == 'climate':
extract_case_func = extract_climate_case
if domain == 'hsd':
extract_case_func = extract_scifact_case
if domain == 'health':
extract_case_func = extract_scifact_case
if domain == 'sbic':
extract_case_func = extract_scifact_case
data_list = open(fn).readlines()
dataset = [extract_case_func(x) for x in data_list]
dataset = [x for x in dataset if x[1] == 'SUPPORTS' or x[1] == 'REFUTES']
print(f'Dataset size = {len(dataset)}')
return dataset
def gen_response_chat(
prompt, t, max_tok, n, system_info = '',
):
complete = False
while not complete:
try:
response = openai.ChatCompletion.create(
model = 'gpt-3.5-turbo',
messages = [
{
'role': 'system',
'content': system_info
},
{
'role': 'user',
'content': prompt
}
],
temperature = t,
max_tokens = max_tok,
n = n
)
ans_list = [
cand['message']['content'].strip() for cand in response['choices'] # \
# if condition_str in cand['message']['content']
]
if n == 1:
ans_txt = ans_list[0]
else:
ans_txt = ans_list
# ans_txt, confidence = select_func(ans_list, keywords = keywords)
complete = True
except:
print('Chat error')
# print(prompt)
# abort()
time.sleep(2)
return ans_txt
def think_twice(claim, verify_prompt, searcher, search_mode):
prompt = f'{verify_prompt}\n\nSomeone said: {claim}\nQuestion: Is it fair to say that?\nAnswer:'
system_info = 'Find the most suspicious information of the statement and check scientific or social fact as the examples.'
qa_str = gen_response_chat(
prompt, t = 0.1, max_tok = 128, n = 1,
system_info = system_info
).strip()
return qa_str
def verify_dataset(searcher, dataset, verify_prompt, args):
def query_verify(verify_str):
verify_str = verify_str.lower()
return not ('no.' in verify_str or 'no,' in verify_str)
def query_verify_neg(verify_str):
verify_str = verify_str.lower()
return not ('yes.' in verify_str or 'yes,' in verify_str)
def cot_verify(verify_str):
return 'accept' in verify_str.split('\n')[-1]
num_case = len(dataset)
crr = 0.
log_list = []
wrong_list = []
if args.mode == 'cot':
ent_prompt = open(f'general_prompts/ent_cot.txt').read()
verify_str_list = []
for i, case in enumerate(dataset):
claim, label, _ = case
if args.mode == 'zero':
prompt = f'Someone said: {claim}\nQuestion: Is it fair to say that?\nAnswer:'
ans_prompt = None
qa_str = None
ans_str = None
system_info = 'Analysis if the claim is factual and fair.'
verify_func = query_verify
elif args.mode == 'cot':
prompt = f'Someone said: {claim}\nQuestion: Is it fair to say that?\nAnswer:'
ans_prompt = None
qa_str = None
ans_str = None
system_info = 'Let\'s think step by step and answer the question as the examples according to scientific or social facts.'
verify_func = query_verify
elif args.mode == 'fp':
try:
qa_str = think_twice(
claim, verify_prompt, searcher, args.mode
)
except:
print(f'Error occured at sample {i}\n')
sys.exit()
prompt = f"Someone said: {claim}\n{qa_str}\nYes/no question: Is it fair to say that?"
system_info = 'Analyze if the statement is fair according to the fact and context. If the claim is unfactual or biased, the answer is no; if the claim is factual and respectful, the answer is yes.'
verify_func = query_verify
if args.mode == 'cot':
prompt = f'{ent_prompt}\n\n{prompt}'
verify_str = gen_response_chat(
prompt, t = 0.1, max_tok = 128, n = 1, system_info = system_info
)
if args.mode == 'fp':
verify_str_list.append(qa_str)
else:
verify_str_list.append(verify_str)
if verify_func(verify_str):
pred = 'SUPPORTS'
else:
pred = 'REFUTES'
if args.verbose:
print(claim)
print(label)
print('--')
print(qa_str)
print('--')
print(verify_str)
print(f'pred = {pred}')
print('--')
print(prompt)
sys.exit()
if pred == label:
crr += 1.
else:
wrong_list.append(i)
case_log = [
f'---- {i} / {num_case} -- crr = {crr}, acc = {crr / (i + 1)}',
f'---- Claim: {claim}',
f'---- Label: {label}',
f'---- QA_str: {qa_str}',
f'---- Verifi_str: {verify_str}',
f'---- Prediction: {pred}',
'----------------------------------------------------\n'
]
log_list.append('\n'.join(case_log))
if i % 100 == 0:
print(f'Processed {i + 1} claims.')
open(f'log/{args.task}_{args.mode}_check_{args.exp_name}.log', 'w').write(
'\n'.join(log_list)
)
json.dump(wrong_list, open(f'log/{args.task}_wrong_list_joint.json', 'w'))
json.dump(verify_str_list, open(f'log/{args.task}_{args.mode}_verify_list_{args.exp_name}.json', 'w'))
return crr / num_case
if __name__ == '__main__':
# args.task: climate | hsd
# args.verbose: true | false
# args.mode: zero | fp | cot
# args.start_idx: int >= 0
# args.exp_name: example = "joint"
args = parser.parse_args()
if args.mode == 'search':
searcher = FaissSearcher.from_prebuilt_index(
'wikipedia-dpr-multi-bf',
'facebook/dpr-question_encoder-multiset-base'
)
else:
searcher = None
dataset = load_dataset(args.task)[args.start_idx:]
verify_prompt = open('general_prompts/verify_prompts.txt').read()
acc = verify_dataset(
searcher, dataset, verify_prompt, args
)
f1 = load_eval_data(task = args.task, mode = args.mode, exp_name = args.exp_name)
print(f'\nAcc = {acc}\nF1 = {f1}\n')