Spaces:
Sleeping
Sleeping
Download src/coveragecheck/agent/verify.py from TBM99/CoverageCheck: direct link, hf CLI and curl.
- Browser
- Download file 3.7 kB
-
https://huggingface.co/spaces/TBM99/CoverageCheck/resolve/main/src/coveragecheck/agent/verify.py
- Command line
-
hf download hf://spaces/TBM99/CoverageCheck/src/coveragecheck/agent/verify.py
-
curl -L -o verify.py https://huggingface.co/spaces/TBM99/CoverageCheck/resolve/main/src/coveragecheck/agent/verify.py
3.7 kB
| """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, | |
| } | |