-
Notifications
You must be signed in to change notification settings - Fork 2
/
gpt_rank.py
130 lines (117 loc) · 4.8 KB
/
gpt_rank.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
from gpt import gpt_critic_rank
import json
import random
import re
import argparse
def gpt_compare(src_dir, cand_dir_1, cand_dir_2, tgt_json_dir, tgt_txt_dir, model="gpt-3.5-turbo-0301"):
with open(src_dir) as f:
articles = [x.strip() for x in f.readlines()]
with open(cand_dir_1) as f:
summaries_1 = [x.strip() for x in f.readlines()]
with open(cand_dir_2) as f:
summaries_2 = [x.strip() for x in f.readlines()]
with open("./prompts/gptcompare.txt") as f:
prompt = f.read().strip()
with open(tgt_json_dir, "w") as f1, open(tgt_txt_dir, "w") as f2:
for i in range(len(articles)):
print(i)
article = articles[i]
summary_1 = summaries_1[i]
summary_2 = summaries_2[i]
idxs = list(range(2))
random.shuffle(idxs)
summary_1, summary_2 = [summary_1, summary_2][idxs[0]], [summary_1, summary_2][idxs[1]]
response, _ = gpt_critic_rank(article, [summary_1, summary_2], prompt, model)
print(response["message"]["content"], file=f2)
print(json.dumps({
"article": article,
"summary_1": summary_1,
"summary_2": summary_2,
"response": response,
"idxs": idxs,
}), file=f1)
def compute_scores(files, systems, output_dir=None):
scores = {s: 0 for s in systems}
data = []
for fdir in files:
with open(fdir) as f:
data += [json.loads(line) for line in f.readlines()]
output = dict()
for (i, d) in enumerate(data):
response = d["response"]["message"]["content"]
# extract the decision with format: Decision: ...
decision = re.findall(r"Decision: (.*)", response)[0]
if "tie" not in decision and "Tie" not in decision:
try:
int(decision.strip())
if int(decision.strip()) == 1:
scores[systems[d["idxs"][0]]] += 1
# print(systems[d["idxs"][0]])
output[i] = systems[d["idxs"][0]]
elif int(decision.strip()) == 2:
scores[systems[d["idxs"][1]]] += 1
# print(systems[d["idxs"][1]])
output[i] = systems[d["idxs"][1]]
else:
# print(decision)
pass
except:
if "Summary 1" in decision:
scores[systems[d["idxs"][0]]] += 1
# print(systems[d["idxs"][0]])
output[i] = systems[d["idxs"][0]]
elif "Summary 2" in decision:
scores[systems[d["idxs"][1]]] += 1
# print(systems[d["idxs"][1]])
output[i] = systems[d["idxs"][1]]
else:
# print(decision)
raise
else:
output[i] = "tie"
print(scores)
if output_dir is not None:
with open(output_dir, "w") as f:
json.dump(output, f, indent=4)
return scores
def gpt_rank(article, summaries, model, tgt_dir):
"""
article: str
summaries: list of str
model: str, OpenAI model name
tgt_dir: str, target directory
"""
with open("./prompts/gptrank.txt") as f:
prompt = f.read().strip()
response = gpt_critic_rank(article, summaries, prompt, model)
with open(tgt_dir, "w") as f:
print(json.dumps({
"article": article,
"summaries": summaries,
"response": response,
}), file=f)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Parameters')
parser.add_argument("-r", "--raw", action="store_true", help="get raw gpt score")
parser.add_argument("-s", "--score", action="store_true", help="compute gpt score")
parser.add_argument("--src_dir", type=str, help="source file")
parser.add_argument("--cand_dir_1", type=str, help="candidate file 1")
parser.add_argument("--cand_dir_2", type=str, help="candidate file 2")
parser.add_argument("--tgt_json_dir", type=str, help="target json file")
parser.add_argument("--tgt_txt_dir", type=str, help="target txt file")
parser.add_argument("--model", type=str, help="model", default="gpt-3.5-turbo-0301")
parser.add_argument("--system_1", type=str, help="system 1")
parser.add_argument("--system_2", type=str, help="system 2")
parser.add_argument("--output_dir", type=str, help="output dir")
args = parser.parse_args()
if args.raw:
gpt_compare(
args.src_dir,
args.cand_dir_1,
args.cand_dir_2,
args.tgt_json_dir,
args.tgt_txt_dir,
args.model
)
elif args.score:
compute_scores([args.tgt_json_dir], [args.system_1, args.system_2], args.output_dir)