AI Engineer Problems › Evaluation & Testing

Strict Factual Citation Validator

Medium · Coding · Evaluation & Testing · Asked at Bloomberg, OpenAI

Strict Factual Citation Validator

Write a function validate_citations(answer, source_chunks) that checks if the numeric citations in an LLM's answer are supported by exact matches in the provided context documents.

The answer contains citations in the format [Doc X: Paragraph Y] or [Doc X: Clause Y] followed by the text snippet in quotes. For example: According to [Doc 1: Paragraph 3] "revenue increased by 12.5%", while [Doc 2: Clause 4] "liabilities remained flat".

The function should extract all citations, look up the document ID in the source_chunks dictionary, and verify if the quoted snippet exists exactly (case-insensitive and ignoring outer spaces) within that document's text.

Return a tuple of (is_valid: bool, invalid_citations: List[str]).

Example

answer = 'According to [Doc 1: Paragraph 3] "revenue increased by 12.5%"'
source_chunks = {"Doc 1": "Our revenue increased by 12.5% in the last quarter."}
validate_citations(answer, source_chunks)
# Returns: (True, [])

Solve it in the browser on GenAIDojo