Skip to content

F1 at k

get_assert(output, context)

Calculates F1@k.

Source code in src/evaluation/metrics/order_unaware/f1_at_k.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def get_assert(output: str, context) -> GradingResult:
    """Calculates F1@k."""
    precision = precision_at_k.get_assert(context=context, output=output)["score"]
    recall = recall_at_k.get_assert(context=context, output=output)["score"]

    if precision + recall == 0:
        score = 0.0
    else:
        score = round(float(2 * (precision * recall) / (precision + recall)), 2)

    # threshold = context["test"]["metadata"]["threshold_ragas_as"]
    threshold = 0

    if math.isnan(score):
        score = 0.0

    return {
        "pass": score > threshold,
        "score": score,
        "reason": f"{score} > {threshold} = {score > threshold}",
    }