File size: 1,294 Bytes
4d67ec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
def compute_confidence(
    retrieval_scores: list[float],
    verified: bool,
) -> float:
    """
    Computes a single confidence score [0.0 – 1.0] from two signals:

      - retrieval_quality (60%): average cosine similarity of retrieved chunks.
        High similarity means the question closely matched NCERT content.

      - verifier_bonus (40%): whether the LLM verifier confirmed the answer
        is supported by the retrieved context. Binary boost.

    Note: The BERT classifier confidence will be incorporated once the
    classification layer (Phase 3) is built. The formula will then become:
      0.4 * bert_conf + 0.35 * avg_retrieval + 0.25 * verifier

    Args:
        retrieval_scores: list of cosine similarity floats from pgvector (0–1)
        verified: bool from verifier.py (did the answer pass the faithfulness check)

    Returns:
        confidence: float in [0.0, 1.0]
    """
    if not retrieval_scores:
        avg_retrieval = 0.0
    else:
        # avg_retrieval = sum(retrieval_scores) / len(retrieval_scores)
        avg_retrieval = max(retrieval_scores)

    verifier_score = 1.0 if verified else 0.0

    confidence = (0.6 * avg_retrieval) + (0.4 * verifier_score)

    # Clamp to [0, 1] for safety
    return round(min(max(confidence, 0.0), 1.0), 4)