forked from explodinggradients/ragas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_faithfulness.py
403 lines (336 loc) · 15.4 KB
/
_faithfulness.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
from __future__ import annotations
import json
import logging
import typing as t
from dataclasses import dataclass, field
import numpy as np
from langchain_core.pydantic_v1 import BaseModel, Field
from ragas.dataset_schema import SingleTurnSample
from ragas.llms.output_parser import RagasoutputParser, get_json_format_instructions
from ragas.llms.prompt import Prompt
from ragas.metrics.base import (
MetricType,
MetricWithLLM,
SingleTurnMetric,
ensembler,
get_segmenter,
)
if t.TYPE_CHECKING:
from langchain_core.callbacks import Callbacks
from ragas.llms.prompt import PromptValue
class HasSegmentMethod(t.Protocol):
def segment(self, text) -> t.Any: ...
logger = logging.getLogger(__name__)
class Statements(BaseModel):
sentence_index: int = Field(
..., description="Index of the sentence from the statement list"
)
simpler_statements: t.List[str] = Field(..., description="the simpler statements")
class StatementsAnswers(BaseModel):
__root__: t.List[Statements]
def dicts(self) -> t.List[t.Dict]:
return self.dict()["__root__"]
_statements_output_instructions = get_json_format_instructions(StatementsAnswers)
_statements_output_parser = RagasoutputParser(pydantic_object=StatementsAnswers)
LONG_FORM_ANSWER_PROMPT = Prompt(
name="long_form_answer",
output_format_instruction=_statements_output_instructions,
instruction="Given a question, an answer, and sentences from the answer analyze the complexity of each sentence given under 'sentences' and break down each sentence into one or more fully understandable statements while also ensuring no pronouns are used in each statement. Format the outputs in JSON.",
examples=[
{
"question": "Who was Albert Einstein and what is he best known for?",
"answer": "He was a German-born theoretical physicist, widely acknowledged to be one of the greatest and most influential physicists of all time. He was best known for developing the theory of relativity, he also made important contributions to the development of the theory of quantum mechanics.",
"sentences": """
0:He was a German-born theoretical physicist, widely acknowledged to be one of the greatest and most influential physicists of all time.
1:He was best known for developing the theory of relativity, he also made important contributions to the development of the theory of quantum mechanics.
""",
"analysis": StatementsAnswers.parse_obj(
[
{
"sentence_index": 0,
"simpler_statements": [
"Albert Einstein was a German-born theoretical physicist.",
"Albert Einstein is recognized as one of the greatest and most influential physicists of all time.",
],
},
{
"sentence_index": 1,
"simpler_statements": [
"Albert Einstein was best known for developing the theory of relativity.",
"Albert Einstein also made important contributions to the development of the theory of quantum mechanics.",
],
},
]
).dicts(),
}
],
input_keys=["question", "answer", "sentences"],
output_key="analysis",
language="english",
)
class StatementFaithfulnessAnswer(BaseModel):
statement: str = Field(..., description="the original statement, word-by-word")
reason: str = Field(..., description="the reason of the verdict")
verdict: int = Field(..., description="the verdict(0/1) of the faithfulness.")
class StatementFaithfulnessAnswers(BaseModel):
__root__: t.List[StatementFaithfulnessAnswer]
def dicts(self) -> t.List[t.Dict]:
return self.dict()["__root__"]
_faithfulness_output_instructions = get_json_format_instructions(
StatementFaithfulnessAnswers
)
_faithfulness_output_parser = RagasoutputParser(
pydantic_object=StatementFaithfulnessAnswers
)
NLI_STATEMENTS_MESSAGE = Prompt(
name="nli_statements",
instruction="Your task is to judge the faithfulness of a series of statements based on a given context. For each statement you must return verdict as 1 if the statement can be directly inferred based on the context or 0 if the statement can not be directly inferred based on the context.",
output_format_instruction=_faithfulness_output_instructions,
examples=[
{
"context": """John is a student at XYZ University. He is pursuing a degree in Computer Science. He is enrolled in several courses this semester, including Data Structures, Algorithms, and Database Management. John is a diligent student and spends a significant amount of time studying and completing assignments. He often stays late in the library to work on his projects.""",
"statements": [
"John is majoring in Biology.",
"John is taking a course on Artificial Intelligence.",
"John is a dedicated student.",
"John has a part-time job.",
],
"answer": StatementFaithfulnessAnswers.parse_obj(
[
{
"statement": "John is majoring in Biology.",
"reason": "John's major is explicitly mentioned as Computer Science. There is no information suggesting he is majoring in Biology.",
"verdict": 0,
},
{
"statement": "John is taking a course on Artificial Intelligence.",
"reason": "The context mentions the courses John is currently enrolled in, and Artificial Intelligence is not mentioned. Therefore, it cannot be deduced that John is taking a course on AI.",
"verdict": 0,
},
{
"statement": "John is a dedicated student.",
"reason": "The context states that he spends a significant amount of time studying and completing assignments. Additionally, it mentions that he often stays late in the library to work on his projects, which implies dedication.",
"verdict": 1,
},
{
"statement": "John has a part-time job.",
"reason": "There is no information given in the context about John having a part-time job.",
"verdict": 0,
},
]
).dicts(),
},
{
"context": """Photosynthesis is a process used by plants, algae, and certain bacteria to convert light energy into chemical energy.""",
"statements": ["Albert Einstein was a genius."],
"answer": StatementFaithfulnessAnswers.parse_obj(
[
{
"statement": "Albert Einstein was a genius.",
"reason": "The context and statement are unrelated",
"verdict": 0,
}
]
).dicts(),
},
],
input_keys=["context", "statements"],
output_key="answer",
output_type="json",
language="english",
) # noqa: E501
@dataclass
class Faithfulness(MetricWithLLM, SingleTurnMetric):
name: str = "faithfulness" # type: ignore
_required_columns: t.Dict[MetricType, t.Set[str]] = field(
default_factory=lambda: {
MetricType.SINGLE_TURN: {
"user_input",
"response",
"retrieved_contexts",
}
}
)
nli_statements_message: Prompt = field(
default_factory=lambda: NLI_STATEMENTS_MESSAGE
)
statement_prompt: Prompt = field(default_factory=lambda: LONG_FORM_ANSWER_PROMPT)
sentence_segmenter: t.Optional[HasSegmentMethod] = None
max_retries: int = 1
_reproducibility: int = 1
@property
def reproducibility(self):
return self._reproducibility
@reproducibility.setter
def reproducibility(self, value):
if value < 1:
logger.warning("reproducibility cannot be less than 1, setting to 1")
value = 1
elif value % 2 == 0:
logger.warning(
"reproducibility level cannot be set to even number, setting to odd"
)
value += 1
self._reproducibility = value
def __post_init__(self):
if self.sentence_segmenter is None:
language = self.nli_statements_message.language
self.sentence_segmenter = get_segmenter(language=language, clean=False)
def _create_nli_prompt(self, row: t.Dict, statements: t.List[str]) -> PromptValue:
assert self.llm is not None, "llm must be set to compute score"
contexts = row["retrieved_contexts"]
# check if the statements are support in the contexts
contexts_str: str = "\n".join(contexts)
statements_str: str = json.dumps(statements, ensure_ascii=False)
prompt_value = self.nli_statements_message.format(
context=contexts_str, statements=statements_str
)
return prompt_value
def _create_statements_prompt(self, row: t.Dict) -> PromptValue:
assert self.sentence_segmenter is not None, "sentence_segmenter is not set"
text, question = row["response"], row["user_input"]
sentences = self.sentence_segmenter.segment(text)
sentences = [
sentence for sentence in sentences if sentence.strip().endswith(".")
]
sentences = "\n".join([f"{i}:{x}" for i, x in enumerate(sentences)])
prompt_value = self.statement_prompt.format(
question=question, answer=text, sentences=sentences
)
return prompt_value
def _compute_score(self, answers: StatementFaithfulnessAnswers):
# check the verdicts and compute the score
faithful_statements = sum(
1 if answer.verdict else 0 for answer in answers.__root__
)
num_statements = len(answers.__root__)
if num_statements:
score = faithful_statements / num_statements
else:
logger.warning("No statements were generated from the answer.")
score = np.nan
return score
async def _single_turn_ascore(
self, sample: SingleTurnSample, callbacks: Callbacks
) -> float:
row = sample.dict()
return await self._ascore(row, callbacks)
async def _ascore(self: t.Self, row: t.Dict, callbacks: Callbacks) -> float:
"""
returns the NLI score for each (q, c, a) pair
"""
assert self.llm is not None, "LLM is not set"
p_value = self._create_statements_prompt(row)
statements = await self.llm.generate(
p_value,
callbacks=callbacks,
)
statements = await _statements_output_parser.aparse(
statements.generations[0][0].text, p_value, self.llm, self.max_retries
)
if statements is None:
return np.nan
statements = [item["simpler_statements"] for item in statements.dicts()]
statements = [item for sublist in statements for item in sublist]
assert isinstance(statements, t.List), "statements must be a list"
p_value = self._create_nli_prompt(row, statements)
nli_result = await self.llm.generate(
p_value,
callbacks=callbacks,
n=self._reproducibility,
)
nli_result_text = [
nli_result.generations[0][i].text for i in range(self._reproducibility)
]
faithfulness_list = [
await _faithfulness_output_parser.aparse(
text, p_value, self.llm, self.max_retries
)
for text in nli_result_text
]
faithfulness_list = [
faith.dicts() for faith in faithfulness_list if faith is not None
]
if faithfulness_list:
faithfulness_list = ensembler.from_discrete(
faithfulness_list,
"verdict",
)
faithfulness_list = StatementFaithfulnessAnswers.parse_obj(
faithfulness_list
)
else:
return np.nan
return self._compute_score(faithfulness_list)
def adapt(self, language: str, cache_dir: t.Optional[str] = None) -> None:
assert self.llm is not None, "LLM is not set"
logger.info(f"Adapting Faithfulness metric to {language}")
self.nli_statements_message = self.nli_statements_message.adapt(
language, self.llm, cache_dir
)
self.statement_prompt = self.statement_prompt.adapt(
language, self.llm, cache_dir
)
self.sentence_segmenter = get_segmenter(language=language, clean=False)
def save(self, cache_dir: t.Optional[str] = None) -> None:
self.nli_statements_message.save(cache_dir)
self.statement_prompt.save(cache_dir)
@dataclass
class FaithfulnesswithHHEM(Faithfulness):
name: str = "faithfulness_with_hhem" # type: ignore
device: str = "cpu"
batch_size: int = 10
def __post_init__(self):
try:
from transformers import AutoModelForSequenceClassification
except ImportError:
raise ImportError(
"Huggingface transformers must be installed to use this feature, try `pip install transformers`"
)
self.nli_classifier = AutoModelForSequenceClassification.from_pretrained(
"vectara/hallucination_evaluation_model", trust_remote_code=True
)
self.nli_classifier.to(self.device)
super().__post_init__()
def _create_pairs(
self, row: t.Dict, statements: t.List[str]
) -> t.List[t.Tuple[str, str]]:
"""
create pairs of (question, answer) from the row
"""
premise = "\n".join(row["contexts"])
pairs = [(premise, statement) for statement in statements]
return pairs
def _create_batch(
self, pairs: t.List[t.Tuple[str, str]]
) -> t.Generator[t.List[t.Tuple[str, str]], None, None]:
length_of_pairs = len(pairs)
for ndx in range(0, length_of_pairs, self.batch_size):
yield pairs[ndx : min(ndx + self.batch_size, length_of_pairs)]
async def _ascore(self: t.Self, row: t.Dict, callbacks: Callbacks) -> float:
"""
returns the NLI score for each (q, c, a) pair
"""
assert self.llm is not None, "LLM is not set"
p_value = self._create_statements_prompt(row)
statements = await self.llm.generate(
p_value,
callbacks=callbacks,
)
statements = await _statements_output_parser.aparse(
statements.generations[0][0].text, p_value, self.llm, self.max_retries
)
if statements is None:
return np.nan
statements = [item["simpler_statements"] for item in statements.dicts()]
statements = [item for sublist in statements for item in sublist]
assert isinstance(statements, t.List), "statements must be a list"
scores = []
pairs = self._create_pairs(row, statements)
for input_pairs in self._create_batch(pairs): # to avoid OOM
batch_scores = (
self.nli_classifier.predict(input_pairs).cpu().detach().round()
)
scores += batch_scores
return sum(scores) / len(scores)
faithfulness = Faithfulness()