Skip to content

Grounding Verification

PantheraNestah edited this page Nov 5, 2026 · 1 revision

Grounding Verification

Source: app/core/verifier.py, app/core/instrumentation.py, app/services/interaction_logger.py.

After a /query answer is returned, a background task checks whether the answer is supported by the retrieved text and stores the result. The client never sees this result, and it does not change the answer. It exists for monitoring and evaluation.

It runs only for /query, not for lessons.

Steps

  1. Extract claims. The answer is split into sentences. Sentences under 6 words, meta phrases and citation markers are removed.
  2. Score each claim against every retrieved chunk with a natural-language-inference (NLI) model, cross-encoder/nli-MiniLM2-L6-H768, which outputs probabilities for contradiction, entailment and neutral.
    • Contradiction probability above 0.60 marks the claim contradicted.
    • Entailment probability above 0.45 marks it supported.
    • Otherwise a keyword-overlap fallback applies: at least 0.35 of the claim's content words found in a chunk counts as weakly supported; anything lower is neutral (cannot verify).
  3. Aggregate. grounding_score = supported claims / checked claims.
Label Rule
partial any claim is contradicted (overrides the score)
grounded score at least 0.75
partial score at least 0.45
ungrounded otherwise

If there is no context (small talk) or no checkable claim, the score is 1.0 and the label grounded, so conversational replies always count as grounded.

The record also stores has_contradiction, claims_checked and claims_supported.

How to read it

  • It is a heuristic, not a hallucination detector or a safety filter. The keyword fallback can count a lexically similar but unsupported claim as supported, which inflates the score.
  • A small NLI model is applied to full sentences and chunks of 100-220 words, so scores are noisy. Treat them as a trend across many interactions, not a verdict on one answer.
  • Failure is non-fatal: if verification raises an error, the interaction is still saved with empty grounding fields.
  • Verification takes seconds to tens of seconds on a CPU. It runs in a worker thread, one at a time, so it does not block other requests.

Citation instrumentation

Separately and instantly, instrument_response() records citations_present and citation_count from [n] markers. These are stored in the log, and citations_present is not returned by the API (API Reference).

See Data and Logging for the stored fields and queries, and Evaluation for how grounding is aggregated.

Clone this wiki locally