-
Notifications
You must be signed in to change notification settings - Fork 28
/
factscorer.py
366 lines (310 loc) · 15.5 KB
/
factscorer.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import argparse
import string
import json
import numpy as np
import os
import logging
from tqdm import tqdm
from factscore.abstain_detection import is_response_abstained
from factscore.atomic_facts import AtomicFactGenerator
from factscore.clm import CLM
from factscore.npm import NPM
from factscore.openai_lm import OpenAIModel
from factscore.retrieval import DocDB, Retrieval
class FactScorer(object):
def __init__(self,
model_name="retrieval+ChatGPT",
data_dir=".cache/factscore",
model_dir=".cache/factscore",
cache_dir=".cache/factscore",
openai_key="api.key",
cost_estimate="consider_cache",
abstain_detection_type=None,
batch_size=256):
assert model_name in ["retrieval+llama", "retrieval+llama+npm", "retrieval+ChatGPT", "npm", "retrieval+ChatGPT+npm"]
self.model_name = model_name
self.db = {}
self.retrieval = {}
self.npm = {}
self.batch_size = batch_size # batch size for retrieval
self.openai_key = openai_key
self.abstain_detection_type = abstain_detection_type
self.data_dir = data_dir
self.cache_dir = cache_dir
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
self.af_generator = None
self.cost_estimate = cost_estimate
if "llama" in model_name:
self.lm = CLM("inst-llama-7B",
model_dir=os.path.join(model_dir, "inst-llama-7B"),
cache_file=os.path.join(cache_dir, "inst-llama-7B.pkl"))
elif "ChatGPT" in model_name:
self.lm = OpenAIModel("ChatGPT",
cache_file=os.path.join(cache_dir, "ChatGPT.pkl"),
key_path=openai_key)
else:
self.lm = None
def save_cache(self):
if self.lm:
self.lm.save_cache()
if "npm" in self.model_name:
for k, v in self.npm.items():
v.save_cache()
for k, v in self.retrieval.items():
v.save_cache()
def register_knowledge_source(self, name="enwiki-20230401", db_path=None, data_path=None):
assert name not in self.retrieval, f"{name} already registered"
if db_path is None:
db_path = os.path.join(self.data_dir, f"{name}.db")
if data_path is None:
data_path = os.path.join(self.data_dir, f"{name}.jsonl")
cache_path = os.path.join(self.cache_dir, f"retrieval-{name}.json")
embed_cache_path = os.path.join(self.cache_dir, f"retrieval-{name}.pkl")
self.db[name] = DocDB(db_path=db_path, data_path=data_path)
self.retrieval[name] = Retrieval(self.db[name], cache_path, embed_cache_path, batch_size=self.batch_size)
if "npm" in self.model_name:
cache_path = os.path.join(self.cache_dir, f"bm25-{name}.json")
embed_cache_path = os.path.join(self.cache_dir, f"bm25-{name}.pkl")
self.npm[name] = NPM(Retrieval(self.db[name], cache_path, embed_cache_path, "bm25"),
"npm-single",
cache_file=os.path.join(self.cache_dir, f"npm-{name}.pkl"))
def print_cost_estimates(self, total_words, task, model):
# https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them
# Number of tokens are roughly 4/3 of the number of words
total_tokens = total_words * 4.0 / 3
# https://openai.com/pricing
# if we use davinci-003, the cost is $0.02 per 1000 tokens
# if we use gpt-3.5-turbo, the cost is $0.002 per 1000 tokens
if model == "davinci-003":
rate = 0.02
elif model == "gpt-3.5-turbo":
rate = 0.002
total_cost = total_tokens * rate / 1000
# print the total words, tokens, and cost along with rate
logging.critical("Estimated OpenAI API cost for %s ($%.3f per 1000 tokens): $%.2f for %d words and %d tokens" % (task, rate, total_cost, total_words, total_tokens))
def get_score(self,
topics,
generations,
gamma=10,
atomic_facts=None,
knowledge_source=None,
verbose=False):
if knowledge_source is None:
# use the default knowledge source
knowledge_source = "enwiki-20230401"
if knowledge_source not in self.retrieval:
self.register_knowledge_source(knowledge_source)
if type(topics)==type(generations)==str:
topics = [topics]
generations = [generations]
else:
assert type(topics)==type(generations)==list, "`topics` and `generations` should be lists."
assert len(topics)==len(generations), "`topics` and `generations` should have the same length"
if atomic_facts is not None:
assert len(topics)==len(atomic_facts), "`topics` and `atomic_facts` should have the same length"
else:
if self.af_generator is None:
self.af_generator = AtomicFactGenerator(key_path=self.openai_key,
demon_dir=os.path.join(self.data_dir, "demos"),
gpt3_cache_file=os.path.join(self.cache_dir, "InstructGPT.pkl"))
# estimate the total cost of atomic fact generation
total_words = 0
for gen in generations:
total_words += self.af_generator.run(gen, cost_estimate=self.cost_estimate)
self.print_cost_estimates(total_words, task="atomic fact generation", model="davinci-003")
if verbose:
topics = tqdm(topics)
atomic_facts = []
for topic, gen in zip(topics, generations):
# optionally, first detect if the response is abstained
response_abstained = is_response_abstained(gen, self.abstain_detection_type)
if response_abstained:
atomic_facts.append(None)
continue
# continue only when the response is not abstained
curr_afs, _ = self.af_generator.run(gen)
curr_afs = [fact for _, facts in curr_afs for fact in facts]
if len(curr_afs)==0:
atomic_facts.append(None)
else:
atomic_facts.append(curr_afs)
if len(atomic_facts) % 10 == 0:
self.af_generator.save_cache()
assert len(atomic_facts)==len(topics)
self.af_generator.save_cache()
respond_ratio = np.mean([facts is not None for facts in atomic_facts])
if "ChatGPT" in self.model_name:
# estimate the total cost of response generation
total_words = 0
for topic, generation, facts in zip(topics, generations, atomic_facts):
if facts is not None:
total_words += self._get_score(topic, generation, facts, knowledge_source, cost_estimate=self.cost_estimate)
self.print_cost_estimates(total_words, task="factscore evaluation", model="gpt-3.5-turbo")
if verbose:
topics = tqdm(topics)
scores = []
init_scores = []
decisions = []
for topic, generation, facts in zip(topics, generations, atomic_facts):
if facts is None:
decisions.append(None)
else:
decision = self._get_score(topic, generation, facts, knowledge_source)
score = np.mean([d["is_supported"] for d in decision])
if gamma:
init_scores.append(score)
penalty = 1.0 if len(facts)>gamma else np.exp(1-gamma/len(facts))
score = penalty * score
decisions.append(decision)
scores.append(score)
if len(scores) % 10 == 0:
self.save_cache()
self.save_cache()
out = {"score": np.mean(scores),
"respond_ratio": respond_ratio,
"decisions": decisions,
"num_facts_per_response": np.mean([len(d) for d in decisions if d is not None])}
if gamma:
out["init_score"] = np.mean(init_scores)
return out
def _get_score(self, topic, generation, atomic_facts, knowledge_source, cost_estimate=None):
decisions = []
total_words = 0
for atom in atomic_facts:
atom = atom.strip()
if self.lm:
passages = self.retrieval[knowledge_source].get_passages(topic, atom, k=5)
definition = "Answer the question about {} based on the given context.\n\n".format(topic)
context = ""
for psg_idx, psg in enumerate(reversed(passages)):
context += "Title: {}\nText: {}\n\n".format(psg["title"], psg["text"].replace("<s>", "").replace("</s>", ""))
definition += context.strip()
if not definition[-1] in string.punctuation:
definition += "."
prompt = "{}\n\nInput: {} True or False?\nOutput:".format(definition.strip(), atom.strip())
if cost_estimate:
if cost_estimate == "consider_cache" and (prompt.strip() + "_0") not in self.lm.cache_dict:
total_words += len(prompt.split())
elif cost_estimate == "ignore_cache":
total_words += len(prompt.split())
continue
output = self.lm.generate(prompt)
if type(output[1])==np.ndarray:
# when logits are available
logits = np.array(output[1])
assert logits.shape[0] in [32000, 32001]
true_score = logits[5852]
false_score = logits[7700]
is_supported = true_score > false_score
else:
# when logits are unavailable
generated_answer = output[0].lower()
if "true" in generated_answer or "false" in generated_answer:
if "true" in generated_answer and "false" not in generated_answer:
is_supported = True
elif "false" in generated_answer and "true" not in generated_answer:
is_supported = False
else:
is_supported = generated_answer.index("true") > generated_answer.index("false")
else:
is_supported = all([keyword not in generated_answer.lower().translate(str.maketrans("", "", string.punctuation)).split() for keyword in ["not", "cannot", "unknown", "information"]])
else:
is_supported = True
if is_supported and "npm" in self.model_name:
npprob = self.npm[knowledge_source].get_probabilty(topic, atom)
is_supported = npprob > 0.3
decisions.append({"atom": atom, "is_supported": is_supported})
if cost_estimate:
return total_words
else:
return decisions
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--input_path',
type=str,
default="data/labeled/InstructGPT.jsonl")
parser.add_argument('--model_name',
type=str,
default="retrieval+ChatGPT")
parser.add_argument('--gamma',
type=int,
default=10,
help="hyperparameter for length penalty")
parser.add_argument('--openai_key',
type=str,
default="api.key")
parser.add_argument('--data_dir',
type=str,
default=".cache/factscore/")
parser.add_argument('--model_dir',
type=str,
default=".cache/factscore/")
parser.add_argument('--cache_dir',
type=str,
default=".cache/factscore/")
parser.add_argument('--knowledge_source',
type=str,
default=None)
parser.add_argument('--cost_estimate',
type=str,
default="consider_cache",
choices=["consider_cache", "ignore_cache"])
parser.add_argument('--abstain_detection_type',
type=str,
default=None,
choices=["perplexity_ai", "generic", "none"])
parser.add_argument('--use_atomic_facts',
action="store_true")
parser.add_argument('--verbose',
action="store_true",
help="for printing out the progress bar")
parser.add_argument('--print_rate_limit_error',
action="store_true",
help="for printing out rate limit error when using OpenAI keys")
parser.add_argument('--n_samples',
type=int,
default=None)
args = parser.parse_args()
logging.basicConfig(format='%(asctime)s - %(name)s - %(message)s',
datefmt='%m/%d/%Y %H:%M:%S',
level=logging.ERROR if args.print_rate_limit_error else logging.CRITICAL)
fs = FactScorer(model_name=args.model_name,
data_dir=args.data_dir,
model_dir=args.model_dir,
cache_dir=args.cache_dir,
openai_key=args.openai_key,
cost_estimate=args.cost_estimate,
abstain_detection_type=args.abstain_detection_type)
tot = 0
topics, generations, atomic_facts = [], [], []
with open(args.input_path) as f:
for line in f:
dp = json.loads(line)
tot += 1
if args.use_atomic_facts:
assert "annotations" in dp, "You can specify `--use_atomic_facts` only when atomic facts are available in the input data already."
if dp["annotations"] is None:
continue
topics.append(dp["topic"])
generations.append(dp["output"])
atomic_facts.append([atom["text"] for sent in dp["annotations"] for atom in sent["model-atomic-facts"]])
else:
topics.append(dp["topic"])
generations.append(dp["output"])
if args.n_samples is not None and tot==args.n_samples:
break
out = fs.get_score(topics=topics,
generations=generations,
gamma=args.gamma,
atomic_facts=atomic_facts if args.use_atomic_facts else None,
knowledge_source=args.knowledge_source,
verbose=args.verbose)
logging.critical("FActScore = %.1f%%" % (100*out["score"]))
if "init_score" in out:
logging.critical("FActScore w/o length penalty = %.1f%%" % (100*out["init_score"]))
logging.critical("Respond ratio = %.1f%%" % (100*out["respond_ratio"]))
logging.critical("# Atomic facts per valid response = %.1f" % (out["num_facts_per_response"]))
# Save out as a json file
with open(args.input_path.replace(".jsonl", f"_factscore_output.json"), 'w') as f:
f.write(json.dumps(out) + "\n")