@HamzaM to AI & LLMEnglish • 4 days ago

How do you validate that an LLM summary contains zero unsupported claims against the provided source text?

message-square
0
1
How do you validate that an LLM summary contains zero unsupported claims against the provided source text?
@HamzaM to AI & LLMEnglish • 4 days ago

Overview

An LLM summary can add facts, numbers, or names that do not appear in the source text. To keep summaries strictly grounded, split the summary into individual claims and check every claim against the source text before showing it to users.

  • Claim-by-claim checking beats whole-summary scores: A single overall score can hide one false number or name inside an otherwise accurate summary.
  • Block or revise unsupported claims: Any claim without clear support in the source text can be removed, rewritten, or flagged for review.

Steps

  1. Split the summary into atomic claims: Break summary sentences into single factual statements so each fact, date, name, or number can be checked on its own.
  2. Retrieve matching passages from the source text: Find the sentences or chunks in the source text that relate to each claim.
  3. Verify entailment and exact details: Check whether the source text directly supports the claim, paying special attention to numbers, dates, negations, and named entities.
  4. Filter or regenerate unsupported claims: Drop or flag claims that fail verification, and require regeneration if too many claims are unsupported.

Example

export interface ClaimVerificationResult {
  claim: string;
  supported: boolean;
  confidence: number;
  evidenceSnippet: string | null;
  reason: string;
}

export interface SummaryGroundingReport {
  allSupported: boolean;
  supportedCount: number;
  unsupportedCount: number;
  claims: ClaimVerificationResult[];
}

export function splitSummaryIntoClaims(summary: string): string[] {
  return summary
    .split(/(?<=[.!?])\s+|\n+/)
    .map((part) => part.trim())
    .filter((part) => part.length > 0);
}

function tokenizeContentWords(text: string): string[] {
  const stopWords = new Set([
    "the", "a", "an", "and", "or", "but", "is", "are", "was", "were",
    "to", "of", "in", "on", "for", "with", "at", "by", "from", "as",
    "it", "this", "that", "these", "those", "has", "have", "had",
  ]);

  return text
    .toLowerCase()
    .replace(/[^a-z0-9.%\$-]+/g, " ")
    .split(/\s+/)
    .filter((tok) => tok.length > 1 && !stopWords.has(tok));
}

function extractNumbersAndTokens(text: string): string[] {
  const matches = text.match(/\b\d+(?:[.,]\d+)*%?\b/g) ?? [];
  return matches.map((m) => m.replace(/,/g, "").toLowerCase());
}

function splitSourceSentences(sourceText: string): string[] {
  return sourceText
    .split(/(?<=[.!?])\s+|\n+/)
    .map((s) => s.trim())
    .filter((s) => s.length > 0);
}

/**
 * Checks a single claim against source sentences for lexical overlap and numeric grounding.
 */
export function verifyClaimAgainstSource(
  claim: string,
  sourceText: string
): ClaimVerificationResult {
  const sentences = splitSourceSentences(sourceText);
  const claimWords = tokenizeContentWords(claim);
  const claimNumbers = extractNumbersAndTokens(claim);

  if (claimWords.length === 0) {
    return {
      claim,
      supported: false,
      confidence: 0,
      evidenceSnippet: null,
      reason: "Claim contains no verifiable content terms.",
    };
  }

  let bestSentence: string | null = null;
  let bestScore = 0;

  for (const sentence of sentences) {
    const sentLower = sentence.toLowerCase();
    const sentWords = new Set(tokenizeContentWords(sentence));
    const sentNumbers = new Set(extractNumbersAndTokens(sentence));

    const matchedWords = claimWords.filter((w) => sentWords.has(w)).length;
    const wordOverlap = matchedWords / claimWords.length;

    const missingNumbers = claimNumbers.filter((num) => !sentNumbers.has(num) && !sentLower.includes(num));
    const numberPenalty = claimNumbers.length > 0 && missingNumbers.length > 0 ? 0.5 : 0;

    const score = Math.max(0, wordOverlap - numberPenalty);
    if (score > bestScore) {
      bestScore = score;
      bestSentence = sentence;
    }
  }

  const supported = bestScore >= 0.55;

  return {
    claim,
    supported,
    confidence: Number(bestScore.toFixed(3)),
    evidenceSnippet: supported ? bestSentence : null,
    reason: supported
      ? "Matched supporting source sentence with key terms and numbers."
      : "Insufficient support or ungrounded number/entity in source text.",
  };
}

/**
 * Validates every claim in a summary and returns a full grounding report.
 */
export function validateSummaryZeroUnsupportedClaims(
  summary: string,
  sourceText: string
): SummaryGroundingReport {
  const claims = splitSummaryIntoClaims(summary).map((claim) =>
    verifyClaimAgainstSource(claim, sourceText)
  );

  const supportedCount = claims.filter((c) => c.supported).length;
  const unsupportedCount = claims.length - supportedCount;

  return {
    allSupported: claims.length > 0 && unsupportedCount === 0,
    supportedCount,
    unsupportedCount,
    claims,
  };
}

Tips

  • Use an LLM-as-a-judge or NLI check for tricky paraphrases: Keyword and number checks are fast, and pairing them with a strict entailment check catches reworded claims that change meaning.
  • Watch out for numbers, dates, and negation words: Words like “not,” “no,” or “never,” or a changed percentage or year, can flip a true source sentence into a false claim.
  • Include source quotes during generation: Asking the model to cite exact short phrases from the source text for each point makes verification much easier and cleaner.