Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This commit implements the F-beta score metric #1543

Merged
Merged
7 changes: 6 additions & 1 deletion src/ragas/metrics/_answer_correctness.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class AnswerCorrectness(MetricWithLLM, MetricWithEmbeddings, SingleTurnMetric):
default_factory=LongFormAnswerPrompt
)
weights: list[float] = field(default_factory=lambda: [0.75, 0.25])
beta: float = field(default_factory= lambda: 1.0)
answer_similarity: t.Optional[AnswerSimilarity] = None
sentence_segmenter: t.Optional[HasSegmentMethod] = None
max_retries: int = 1
Expand All @@ -185,6 +186,9 @@ def __post_init__(self: t.Self):
language = self.long_form_answer_prompt.language
self.sentence_segmenter = get_segmenter(language=language, clean=False)

if type(self.beta) is not float:
raise ValueError("Beta must be a float. A beta > 1 gives more weight to recall, while beta < 1 favors precision.")

def init(self, run_config: RunConfig):
super().init(run_config)
if self.answer_similarity is None and self.weights[1] != 0:
Expand All @@ -198,7 +202,8 @@ def _compute_statement_presence(
tp = len(prediction.TP)
fp = len(prediction.FP)
fn = len(prediction.FN)
score = tp / (tp + 0.5 * (fp + fn)) if tp > 0 else 0
beta = self.beta
score = ((1 + beta * beta) * tp) / ((1 + beta * beta) * tp + fp + (beta * beta) * fn) if tp > 0 else 0
return score

async def _create_simplified_statements(
Expand Down
Loading