Spaces:
Sleeping
Sleeping
File size: 3,695 Bytes
23d418f | 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 | """System C's verify step (Step 7c): every citation of an answer is checked against
the cited section AND the passages shown (citations.Checker, Step 8), strictly:
an exact quote, found in what the model was shown (CitationCheck.feedback() empty).
Owner decisions (7c, approved):
- A failing citation on an answer triggers ONE retry: the same prompt plus
citations.retry_message (which names each failing citation and why), so the model
regenerates its answer (1a: no conversation replay, no new llm.py plumbing).
- After the retry (or without one), citations that still fail are DROPPED and the
confidence is multiplied by the share that passed (1 of 4 dropped -> x0.75; all
dropped -> 0). An answer is never turned into an abstention: the answer step
decides (owner decision 4); the threshold curve abstains at low confidence.
- An abstention's citations are context, not claims: a failing one is dropped, no
retry (Step 8 handoff).
- A retry call that fails keeps the first answer (dropped and lowered as above): the
first answer was valid, so the question is not turned into an error row.
A and B had 137/138 and 196/196 exact citations (Step 8): this step rarely fires; its
value is the guarantee that no C answer carries an unverified quote.
"""
from collections.abc import Sequence
from typing import Protocol
from coveragecheck.agent.schemas import Answer, Citation
from coveragecheck.verify.citations import CitationCheck
class CitationChecker(Protocol):
"""What verify needs from citations.Checker (a fake in tests)."""
def check(self, citation: Citation, shown: Sequence[str] | None = None) -> CitationCheck: ...
def check_answer(
answer: Answer, checker: CitationChecker, shown_ids: Sequence[str]
) -> list[CitationCheck]:
"""One CitationCheck per citation, in order, against the shown chunk ids."""
return [checker.check(c, shown_ids) for c in answer.citations]
def failing(checks: Sequence[CitationCheck]) -> list[CitationCheck]:
"""The checks that fail the strict test (exact AND in the passages shown)."""
return [c for c in checks if c.feedback()]
def needs_retry(answer: Answer, checks: Sequence[CitationCheck]) -> bool:
"""Retry only an answer (not an abstention) with at least one failing citation."""
return not answer.abstain and bool(failing(checks))
def drop_failing(answer: Answer, checks: Sequence[CitationCheck]) -> Answer:
"""The answer without its failing citations, confidence scaled by the share that
passed. Unchanged when every citation passes (or there is none). One check per
citation is required (zip strict raises ValueError otherwise)."""
kept = [c for c, chk in zip(answer.citations, checks, strict=True) if not chk.feedback()]
if len(kept) == len(answer.citations):
return answer
share = len(kept) / len(answer.citations)
return answer.model_copy(
update={"citations": kept, "confidence": round(answer.confidence * share, 4)}
)
def verify_trace(
first: Sequence[CitationCheck],
final: Sequence[CitationCheck],
retried: bool,
retry_error: str | None,
before: float,
after: float,
) -> dict:
"""What verify did, for the trace (JSON): citations checked, failing at first,
whether it retried (and why a retry failed), failing at the end (dropped), and
the confidence before and after."""
return {
"checked": len(first),
"failed_first": [f"{c.source_id}: {'; '.join(c.feedback())}" for c in failing(first)],
"retried": retried,
"retry_error": retry_error,
"dropped": len(failing(final)),
"confidence_before": before,
"confidence_after": after,
}
|