AI Response Groundedness Checker Project

Artificial Intelligence project 13

AI Response Groundedness Checker Project

Build a complete response groundedness checker workflow with reproducible Python code, transparent inputs, reviewable output, and responsible-use limits.

Explore AI training in VizagView all project ideas

AI objective

Flag answer sentences whose meaningful tokens have weak coverage in the source text.

Data or knowledge source

A proposed answer and one or more supplied source passages.

Requirements

  • Python 3.10 or later
  • A terminal or command prompt
  • No third-party packages required
  • About 45-60 minutes to build and review

How the system works

Split the answer into claims, remove a visible stop-word list, compute source-token coverage for each claim, and compare it with a threshold.

Validation checklist

  • Each claim receives an individual result
  • Empty content-token claims are handled safely
  • The threshold is explicit
  • The output is described as a screening heuristic

Complete Python code

Save the program as ai_response_groundedness_checker.py. The code runs locally and requires no paid API key or model download.

"""Estimate whether answer sentences are supported by supplied source text."""

from __future__ import annotations

from dataclasses import dataclass
import re


STOPWORDS = {"a", "an", "and", "are", "as", "at", "be", "by", "for", "from", "in", "is", "it", "of", "on", "or", "that", "the", "to", "was", "with"}


@dataclass(frozen=True)
class ClaimCheck:
    claim: str
    token_coverage: float
    supported: bool


def content_tokens(text: str) -> set[str]:
    return {token for token in re.findall(r"[a-z0-9]+", text.lower()) if token not in STOPWORDS and len(token) > 1}


def check_groundedness(answer: str, sources: list[str], threshold: float = 0.75) -> list[ClaimCheck]:
    source_tokens = content_tokens(" ".join(sources))
    claims = [part.strip() for part in re.split(r"(?<=[.!?])\s+", answer.strip()) if part.strip()]
    results = []
    for claim in claims:
        tokens = content_tokens(claim)
        coverage = len(tokens & source_tokens) / len(tokens) if tokens else 1.0
        results.append(ClaimCheck(claim, coverage, coverage >= threshold))
    return results


def main() -> None:
    sources = ["The workshop begins on Monday and lasts six weeks.", "Sessions are held online on weekday evenings."]
    response = "The workshop begins on Monday. It includes a guaranteed paid internship."
    for result in check_groundedness(response, sources):
        print(result)
    print("Token coverage is a screening heuristic, not proof that a claim is true or correctly entailed.")


if __name__ == "__main__":
    main()

Run the project

  1. Create and activate a virtual environment.
  2. Install dependencies with No third-party packages required when packages are required.
  3. Run python ai_response_groundedness_checker.py.
  4. Review confidence, fallbacks, sources, or error metrics rather than accepting output automatically.
  5. Test additional normal, edge, unsupported, and adversarial inputs.

Expected output

Claim text, token coverage, and a supported or unsupported flag.

Accuracy, privacy, and responsible-use limits

Token overlap cannot prove truth, entailment, correct numbers, or source quality. Use stronger natural-language inference, citation inspection, and human review for consequential claims.

Ways to extend the project

Add number and date verification, contradiction detection, quoted evidence spans, semantic entailment, claim decomposition, and evaluation on labelled examples.

Continue learning Artificial Intelligence

Try the next project, return to the Softenant project library, or explore the AI training in Vizag for guided NLP, retrieval, evaluation, automation, and responsible AI practice.