File size: 14,490 Bytes
8299003
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
verifier.py
===========
Phase 7 – Answer Grounding & Hallucination Detection

Verifies that every sentence in an LLM-generated answer is supported by the
retrieved context chunks. Uses a Natural Language Inference (NLI) cross-encoder
to classify each (context, claim) pair as:

  SUPPORTED     β€” context entails the claim  (entailment score β‰₯ threshold)
  UNVERIFIED    β€” context neither supports nor contradicts (neutral)
  CONTRADICTED  β€” context explicitly contradicts the claim

NLI Model
---------
  cross-encoder/nli-deberta-v3-small
    - ~180 MB, runs on CPU
    - Label order: {0: contradiction, 1: entailment, 2: neutral}
    - Input: (premise=context_chunk, hypothesis=answer_sentence)

Two-step verification process
------------------------------
  1. Sentence splitting
       Split the LLM answer into individual claims using NLTK sentence tokenizer.

  2. Claim-context entailment
       For each sentence, pair it against every retrieved context chunk.
       The chunk with the highest entailment score is the best support.
       Classify the sentence based on that best score.

  3. Citation verification  (optional)
       Extract [1], [2], ... references from the answer.
       Check that the cited chunk actually supports the citing sentence.

Grounding score
---------------
  grounding_score = supported_sentences / total_sentences
  Range: 0.0 (fully hallucinated) – 1.0 (fully grounded)
  Typical acceptable threshold: β‰₯ 0.7

Usage
-----
    from src.verifier import AnswerVerifier

    verifier = AnswerVerifier()

    result = verifier.verify(answer=answer_text, chunks=retrieved_chunks)
    print(f"Grounding score: {result['grounding_score']:.0%}")

    for r in result["sentence_results"]:
        verdict = r["verdict"]
        sent    = r["sentence"][:80]
        print(f"  [{verdict:12s}]  {sent}")

    # Citation check
    cites = verifier.check_citations(answer=answer_text, chunks=retrieved_chunks)
    for c in cites:
        print(f"  {c['citation']}  {c['status']}  entail={c.get('entail_score','n/a')}")
"""

import logging
import re
from pathlib import Path

# ── Logging ────────────────────────────────────────────────────────────────────
logging.basicConfig(
    level  = logging.INFO,
    format = "%(asctime)s  %(levelname)-8s  %(message)s",
)
log = logging.getLogger(__name__)

# ── Constants ──────────────────────────────────────────────────────────────────
NLI_MODEL          = "cross-encoder/nli-deberta-v3-small"
ENTAIL_IDX         = 1      # confirmed: {0: contradiction, 1: entailment, 2: neutral}
CONTRA_IDX         = 0
NEUTRAL_IDX        = 2
ENTAIL_THRESHOLD   = 0.50   # sentence classified as SUPPORTED if entail β‰₯ this
CONTRA_THRESHOLD   = 0.40   # sentence classified as CONTRADICTED if contra β‰₯ this
MIN_SENTENCE_LEN   = 20     # shorter fragments are skipped (headers, bullet markers)


# ══════════════════════════════════════════════════════════════════════════════
# ANSWER VERIFIER
# ══════════════════════════════════════════════════════════════════════════════

class AnswerVerifier:
    """
    Verifies LLM answer grounding against retrieved context chunks using NLI.

    Attributes
    ----------
    entail_threshold : float
        Minimum entailment probability for a sentence to be SUPPORTED.
    contra_threshold : float
        Minimum contradiction probability to flag a sentence as CONTRADICTED.
    """

    def __init__(
        self,
        nli_model        : str   = NLI_MODEL,
        entail_threshold : float = ENTAIL_THRESHOLD,
        contra_threshold : float = CONTRA_THRESHOLD,
    ):
        from sentence_transformers import CrossEncoder

        log.info(f"Loading NLI model: {nli_model}")
        self._nli              = CrossEncoder(nli_model, max_length=512)
        self.entail_threshold  = entail_threshold
        self.contra_threshold  = contra_threshold
        log.info("NLI model ready.")

    # ── Sentence splitting ──────────────────────────────────────────────────

    def split_sentences(self, text: str) -> list[str]:
        """
        Split answer text into individual sentences.

        Uses NLTK sent_tokenize for reliable sentence boundary detection.
        Filters out very short fragments (bullet markers, standalone numbers).
        """
        try:
            import nltk
            try:
                sentences = nltk.sent_tokenize(text)
            except LookupError:
                nltk.download("punkt_tab", quiet=True)
                sentences = nltk.sent_tokenize(text)
        except ImportError:
            # Simple regex fallback if NLTK unavailable
            sentences = re.split(r"(?<=[.!?])\s+(?=[A-Z\[\(])", text.strip())

        return [s.strip() for s in sentences if len(s.strip()) >= MIN_SENTENCE_LEN]

    # ── Core verification ───────────────────────────────────────────────────

    def verify(
        self,
        answer  : str,
        chunks  : list[dict],
        verbose : bool = False,
    ) -> dict:
        """
        Verify every sentence in `answer` against the retrieved `chunks`.

        Args:
            answer  : LLM-generated answer string
            chunks  : list of chunk dicts with keys: id, text, metadata, score
            verbose : if True, log each sentence verdict

        Returns:
            {
              "grounding_score"  : float (0.0–1.0),
              "total_sentences"  : int,
              "supported"        : int,
              "unverified"       : int,
              "contradicted"     : int,
              "sentence_results" : list[dict],
            }
        """
        # ── Check for prescribed "not found" phrase ────────────────────────
        NOT_FOUND_PHRASES = [
            "not contain enough information",
            "not available in the provided",
            "cannot find",
            "no information",
        ]
        if any(p in answer.lower() for p in NOT_FOUND_PHRASES):
            log.info("Answer contains explicit 'not found' phrase β€” trivially grounded.")
            return {
                "grounding_score"  : 1.0,
                "total_sentences"  : 0,
                "supported"        : 0,
                "unverified"       : 0,
                "contradicted"     : 0,
                "sentence_results" : [],
                "note"             : "LLM correctly reported no relevant context found.",
            }

        sentences     = self.split_sentences(answer)
        context_texts = [c["text"] for c in chunks]

        if not sentences:
            return {
                "grounding_score": 0.0, "total_sentences": 0,
                "supported": 0, "unverified": 0, "contradicted": 0,
                "sentence_results": [],
            }

        sentence_results = []

        for sent in sentences:
            # Pair each sentence against ALL context chunks as premises
            pairs  = [(ctx, sent) for ctx in context_texts]
            scores = self._nli.predict(pairs, apply_softmax=True)

            # Find the chunk with the highest entailment score for this sentence
            best_idx     = max(range(len(scores)), key=lambda i: float(scores[i][ENTAIL_IDX]))
            best_scores  = scores[best_idx]

            entail_prob  = float(best_scores[ENTAIL_IDX])
            contra_prob  = float(best_scores[CONTRA_IDX])
            neutral_prob = float(best_scores[NEUTRAL_IDX])

            # Classify
            if entail_prob >= self.entail_threshold:
                verdict = "SUPPORTED"
            elif contra_prob >= self.contra_threshold:
                verdict = "CONTRADICTED"
            else:
                verdict = "UNVERIFIED"

            result = {
                "sentence"        : sent,
                "verdict"         : verdict,
                "entail_prob"     : round(entail_prob, 3),
                "contra_prob"     : round(contra_prob, 3),
                "neutral_prob"    : round(neutral_prob, 3),
                "best_chunk_id"   : chunks[best_idx]["id"],
                "best_chunk_text" : context_texts[best_idx][:120],
            }
            sentence_results.append(result)

            if verbose:
                log.info(
                    f"  [{verdict:12s}]  entail={entail_prob:.2f}  "
                    f"contra={contra_prob:.2f}  | {sent[:70]!r}"
                )

        supported    = sum(1 for r in sentence_results if r["verdict"] == "SUPPORTED")
        unverified   = sum(1 for r in sentence_results if r["verdict"] == "UNVERIFIED")
        contradicted = sum(1 for r in sentence_results if r["verdict"] == "CONTRADICTED")
        grounding    = supported / len(sentence_results)

        return {
            "grounding_score"  : round(grounding, 3),
            "total_sentences"  : len(sentence_results),
            "supported"        : supported,
            "unverified"       : unverified,
            "contradicted"     : contradicted,
            "sentence_results" : sentence_results,
        }

    # ── Citation verification ───────────────────────────────────────────────

    def check_citations(
        self,
        answer : str,
        chunks : list[dict],
    ) -> list[dict]:
        """
        Verify that [1], [2], ... citations in the answer refer to the right chunk.

        For each citation, checks whether the cited chunk actually entails the
        sentence containing the citation.

        Args:
            answer : LLM-generated answer with inline citations like [1], [2]
            chunks : list of chunk dicts (same order as context was assembled)

        Returns:
            list of dicts:
              citation     : "[1]"
              sentence     : the sentence containing the citation (first 120 chars)
              chunk_id     : ID of the cited chunk
              entail_score : how well the chunk supports the sentence
              status       : "CORRECT" | "QUESTIONABLE" | "OUT_OF_RANGE" | "NO_CITATIONS"
        """
        sentences = self.split_sentences(answer)
        results   = []

        for sent in sentences:
            cite_nums = re.findall(r"\[(\d+)\]", sent)
            for num_str in cite_nums:
                idx = int(num_str) - 1   # citations are 1-indexed

                if idx < 0 or idx >= len(chunks):
                    results.append({
                        "citation"    : f"[{num_str}]",
                        "sentence"    : sent[:120],
                        "chunk_id"    : None,
                        "entail_score": None,
                        "status"      : "OUT_OF_RANGE",
                    })
                    continue

                chunk_text = chunks[idx]["text"]
                scores     = self._nli.predict(
                    [(chunk_text, sent)], apply_softmax=True
                )
                entail_prob = round(float(scores[0][ENTAIL_IDX]), 3)

                results.append({
                    "citation"    : f"[{num_str}]",
                    "sentence"    : sent[:120],
                    "chunk_id"    : chunks[idx]["id"],
                    "entail_score": entail_prob,
                    "status"      : "CORRECT" if entail_prob >= 0.35 else "QUESTIONABLE",
                })

        if not results:
            return [{"status": "NO_CITATIONS", "note": "Answer contains no [n] citations."}]

        return results

    # ── Summary report ──────────────────────────────────────────────────────

    def report(self, verification_result: dict, citation_result: list[dict] = None) -> str:
        """
        Format a human-readable grounding report.
        """
        r     = verification_result
        score = r["grounding_score"]
        total = r["total_sentences"]

        if score >= 0.85:
            grade = "PASS   β€” well-grounded"
        elif score >= 0.60:
            grade = "REVIEW β€” partially grounded"
        else:
            grade = "FAIL   β€” high hallucination risk"

        lines = [
            "=" * 65,
            f"  Grounding Report",
            "=" * 65,
            f"  Score    : {score:.0%}  ({r['supported']}/{total} sentences supported)",
            f"  Verdict  : {grade}",
            f"  Breakdown: {r['supported']} supported  |  "
            f"{r['unverified']} unverified  |  {r['contradicted']} contradicted",
            "-" * 65,
        ]

        for i, s in enumerate(r["sentence_results"], 1):
            icon = {"SUPPORTED": "βœ“", "UNVERIFIED": "?", "CONTRADICTED": "βœ—"}.get(
                s["verdict"], " "
            )
            lines.append(
                f"  [{i}] {icon} [{s['verdict']:12s}] "
                f"e={s['entail_prob']:.2f} c={s['contra_prob']:.2f}"
            )
            lines.append(f"       {s['sentence'][:90]}")
            lines.append(f"       ↑ best match: {s['best_chunk_id']}")

        if citation_result:
            lines += ["", "-" * 65, "  Citation Check"]
            for c in citation_result:
                if c.get("status") == "NO_CITATIONS":
                    lines.append("  No inline citations found in answer.")
                else:
                    score_str = f"entail={c['entail_score']:.2f}" if c["entail_score"] else "n/a"
                    lines.append(
                        f"  {c['citation']}  [{c['status']:12s}]  {score_str}  "
                        f"chunk={c['chunk_id']}"
                    )

        lines.append("=" * 65)
        return "\n".join(lines)