-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathprocess_hir_data.py
111 lines (97 loc) · 4.19 KB
/
process_hir_data.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
import os
import json
import random
import argparse
from tqdm import tqdm
random.seed(42)
KEC_mapping = {
'0.1': '1.0',
'0.2': '0.9',
'0.3': '0.8',
'0.4': '0.7',
'0.5': '0.6',
'0.6': '0.5',
'0.7': '0.4',
'0.8': '0.3',
'0.9': '0.2',
'1.0': '0.1',
# 'all refuse': '0.0',
}
prompt = '''Your current knowledge expression confidence level is {}, please answer the user's question:
{}
'''.format
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--root_dir', type=str, default='/sft_data/llama-2-7b-chat/')
args = parser.parse_args()
return args
def process(args):
root_dir = args.root_dir
train_data = []
valid_data = []
test_data = []
for i in tqdm(range(1, 11)):
Ik_threshold = str(i / 10)
train_file_name = os.path.join(root_dir, 'triviaqa_train_threshold_{}_sft_data.json'.format(Ik_threshold))
valid_file_name = os.path.join(root_dir, 'triviaqa_valid_threshold_{}_sft_data.json'.format(Ik_threshold))
test_file_name = os.path.join(root_dir, 'triviaqa_test_threshold_{}_sft_data.json'.format(Ik_threshold))
with open(train_file_name, 'r', encoding='utf-8') as f:
data = json.load(f)
for item in data:
item['confidence_level'] = KEC_mapping[Ik_threshold]
item['question'] = prompt(item['confidence_level'], item['question'])
train_data.append(item)
with open(valid_file_name, 'r', encoding='utf-8') as f:
data = json.load(f)
for item in data:
item['confidence_level'] = KEC_mapping[Ik_threshold]
item['question'] = prompt(item['confidence_level'], item['question'])
valid_data.append(item)
with open(test_file_name, 'r', encoding='utf-8') as f:
data = json.load(f)
for item in data:
item['confidence_level'] = KEC_mapping[Ik_threshold]
item['question'] = prompt(item['confidence_level'], item['question'])
test_data.append(item)
with open(os.path.join(root_dir, 'triviaqa_dev_tp1.0_10responses_with_em_labels.json'), 'r', encoding='utf-8') as f:
data = json.load(f)
for item in data:
new_sample_0 = {
"question_id": item['question_id'],
"confidence_level": "0.0",
"question": prompt("0.0", item['question']),
'answer': "This question is beyond the scope of my knowledge, and I am not sure what the answer is.",
}
test_data.append(new_sample_0)
with open(os.path.join(root_dir, 'triviaqa_train_threshold_0.1_sft_data.json'), 'r', encoding='utf-8') as f:
truely_train_data = json.load(f)
for item in truely_train_data:
new_sample_0 = {
"question_id": item['question_id'],
"confidence_level": "0.0",
"question": prompt("0.0", item['question']),
'answer': "This question is beyond the scope of my knowledge, and I am not sure what the answer is.",
}
train_data.append(new_sample_0)
with open(os.path.join(root_dir, 'triviaqa_valid_threshold_0.1_sft_data.json'), 'r', encoding='utf-8') as f:
truely_valid_data = json.load(f)
for item in truely_valid_data:
new_sample_0 = {
"question_id": item['question_id'],
"confidence_level": "0.0",
"question": prompt("0.0", item['question']),
'answer': "This question is beyond the scope of my knowledge, and I am not sure what the answer is.",
}
valid_data.append(new_sample_0)
random.shuffle(train_data)
random.shuffle(valid_data)
# save data
with open(os.path.join(root_dir, 'triviaqa_train_hir_data.json'), 'w', encoding='utf-8') as f:
json.dump(train_data, f, indent=2, ensure_ascii=False)
with open(os.path.join(root_dir, 'triviaqa_valid_hir_data.json'), 'w', encoding='utf-8') as f:
json.dump(valid_data, f, indent=2, ensure_ascii=False)
with open(os.path.join(root_dir, 'triviaqa_test_hir_data.json'), 'w', encoding='utf-8') as f:
json.dump(test_data, f, indent=2, ensure_ascii=False)
if __name__ == '__main__':
args = get_args()
process(args)