-
Notifications
You must be signed in to change notification settings - Fork 1
/
close_semi_gre.py
155 lines (124 loc) · 5.39 KB
/
close_semi_gre.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
import os
from transformers import AutoTokenizer, AutoModelForCausalLM
import argparse
import json
from tqdm import tqdm
from collections import defaultdict
from run_models.llama import model_name_wrapper, llama_model_init, llama_model_inference, vicuna_model_inference, \
wizardlm_model_inference, mpt_model_inferece, openchat_model_inference, zephyr_model_inferece, chatglm3_model_inference
from run_models.galactica import galactica_model_init, galactica_model_inference
try:
from run_models.gpt import gpt_instruct, gpt_chat
# from run_models.claude import claude_init, claude_chat
except:
print('gpt or claude not installed')
def get_rel_ent_set(dataset):
if dataset == "cdr":
relation_set = ["induced by", "not induced by"]
entity_type_set = ["chemical", "disease"]
elif dataset == "nyt10m":
relation_set =['administrative_divisions',
'advisors',
'capital',
'children',
'company',
'contains',
'country',
'county_seat',
'ethnicity',
'featured_film_locations',
'founders',
'geographic_distribution',
'location',
'locations',
'majorshareholders',
'nationality',
'neighborhood_of',
'place_founded',
'place_lived',
'place_of_birth',
'place_of_burial',
'place_of_death',
'religion'
]
entity_type_set = ['administrative_division',
'business',
'company',
'country',
'deceasedperson',
'ethnicity',
'event',
'film',
'location',
'neighborhood',
'people',
'person',
'region',
'time',
'us_county'
]
return relation_set, entity_type_set
def gpt_run_model(args, dataset_file, prompt_file, output_file):
results = defaultdict(list)
dataset_name = args.dataset.split('_')[0]
relation_set, entity_type_set = get_rel_ent_set(dataset_name)
with open(prompt_file, 'r') as f:
prompt_ = f.read()
with open(dataset_file, 'r') as f:
dataset = json.load(f)
gpt_func = gpt_instruct
if args.type == 'closed':
source_texts = list(dataset.keys())
for i in tqdm(range(len(source_texts))):
source_text = source_texts[i]
triples = dataset[source_text]
for triple in triples:
subject_, object_ = triple[0], triple[2]
prompt = prompt_.replace('$TEXT$', source_text)
prompt = prompt.replace('$RELATION_SET$', str(relation_set))
prompt = prompt.replace('$SUBJECT$', subject_)
prompt = prompt.replace('$OBJECT$', object_)
generation = gpt_func(args.model_name, prompt)
results[source_text].append([subject_, generation, object_])
if i % 20 == 0:
with open(output_file, 'w') as f:
json.dump(results, f, indent=6)
else:
source_texts = list(dataset.keys())
for i in tqdm(range(len(source_texts))):
# try:
source_text = source_texts[i]
prompt = prompt_.replace('$TEXT$', source_text)
prompt = prompt.replace('$RELATION_SET$', str(relation_set))
prompt = prompt.replace('$ENTITY_TYPE_SET$', str(entity_type_set))
generation = gpt_func(args.model_name, prompt)
# relation_str = post_processing(args.model_name, generation)
results[source_text] = generation
if i % 20 == 0:
with open(output_file, 'w') as f:
json.dump(results, f, indent=6)
# except:
# print(f'error occured at {i}')
# continue
return results
def construct_args():
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, default='nyt10m')
parser.add_argument('--type', type=str, default='open')
parser.add_argument('--model_name', type=str, default='gpt-3.5-turbo-instruct')
parser.add_argument('--exp_id', type=str, default='1')
args = parser.parse_args()
return args
def main():
args = construct_args()
if args.type == 'closed':
prompt_file = './prompts/close_gre.txt'
elif args.type == 'semi':
prompt_file = './prompts/semi_open_gre.txt'
dataset_file = f'./datasets/processed/{args.dataset}_processed.json'
output_file = f'./results/{args.dataset}_gpt-3.5_{args.type}_{args.exp_id}.json'
results = gpt_run_model(args, dataset_file, prompt_file, output_file)
with open(output_file, 'w') as f:
json.dump(results, f, indent=6)
if __name__ == '__main__':
main()