Spaces:
Sleeping
Sleeping
Download src/coveragecheck/evals/dummy.py from TBM99/CoverageCheck: direct link, hf CLI and curl.
- Browser
- Download file 5.06 kB
-
https://huggingface.co/spaces/TBM99/CoverageCheck/resolve/main/src/coveragecheck/evals/dummy.py
- Command line
-
hf download hf://spaces/TBM99/CoverageCheck/src/coveragecheck/evals/dummy.py
-
curl -L -o dummy.py https://huggingface.co/spaces/TBM99/CoverageCheck/resolve/main/src/coveragecheck/evals/dummy.py
5.06 kB
| """Step 4b: the harness dummy, a retrieval-only System B with no LLM. | |
| It proves run -> grade -> report works end to end before any LLM exists, and its | |
| retrieval is real (System B's hybrid search), so recall@k from its runs is a real | |
| number. Its "answer" is the top passage, its citation a verbatim quote from it (so | |
| the Step 8 checker passes it), and its confidence the top RRF score over the best | |
| possible one. Measured on the draft set (4b survey): that confidence is 0.85-1.0 for | |
| answerable and 0.83-1.0 for unanswerable questions, so it cannot tell them apart; | |
| the dummy's threshold curve is expected to be flat. That is fine for plumbing. | |
| """ | |
| from datetime import date | |
| from typing import Protocol | |
| import pandas as pd | |
| from coveragecheck import config | |
| from coveragecheck.agent.schemas import Answer, Citation, Response | |
| from coveragecheck.index.hybrid import Result | |
| ABSTAIN_TEXT = "I can't determine this from the documents." | |
| def cut_quote(text: str, limit: int) -> str: | |
| """The start of text, at most `limit` characters, ending at a word boundary (a | |
| plain cut ended mid-word in most of the 63 draft-set quotes; 4b audit). Always a verbatim | |
| prefix of text, so the Step 8 citation check finds it. A first word longer than | |
| the limit is cut as is.""" | |
| text = text.strip() | |
| if len(text) <= limit: | |
| return text | |
| head = text[:limit] | |
| # the cut already sits at a boundary (before or just after a space), or there is | |
| # no boundary to go back to; rsplit on "alpha beta " would drop "beta" (a test | |
| # caught that) | |
| if text[limit].isspace() or head[-1].isspace() or not any(c.isspace() for c in head): | |
| return head.rstrip() | |
| return head.rsplit(None, 1)[0] | |
| class Searcher(Protocol): | |
| """The part of hybrid.Retriever the dummy uses (tests pass a fake).""" | |
| chunks: pd.DataFrame | |
| def search(self, query: str, day: date | None = None) -> Result: ... | |
| class DummySystem: | |
| name = "dummy" | |
| def __init__( | |
| self, | |
| retriever: Searcher, | |
| abstain_below: float = config.DUMMY_ABSTAIN_BELOW, | |
| quote_chars: int = config.DUMMY_QUOTE_CHARS, | |
| index_hash: str | None = None, | |
| ): | |
| self.retriever = retriever | |
| self.texts = list(retriever.chunks.text) # by row: a list index, not .iloc | |
| self.abstain_below, self.quote_chars = abstain_below, quote_chars | |
| # RRF's best possible score: rank 1 in both the lexical and the dense list | |
| self.best_score = 2 / (config.RRF_K + 1) | |
| self.settings = { | |
| "retriever": "hybrid (System B, no rerank)", | |
| "candidate_k": config.CANDIDATE_K, | |
| "rrf_k": config.RRF_K, | |
| "abstain_below": abstain_below, | |
| "quote_chars": quote_chars, | |
| "embed_model": config.EMBED_MODEL, | |
| "index_hash": index_hash, # proves which chunk texts the run searched | |
| } | |
| def warm_up(self) -> None: | |
| """Load the embedding model now, so the first timed question doesn't pay ~1 s.""" | |
| self.retriever.search("warm up") | |
| def confidence(self, score: float) -> float: | |
| """Top RRF score as a share of the best possible, capped at 1.""" | |
| return min(1.0, score / self.best_score) | |
| def answer(self, question: str, day: date) -> Response: | |
| """Search, then answer with the top chunk; abstain when nothing is found or | |
| the confidence is under the cut-off (an abstention cites nothing).""" | |
| result = self.retriever.search(question, day) | |
| chunk_ids = [c.chunk_id for c in result.candidates] | |
| source_ids = [c.source_id for c in result.candidates] | |
| if not result.candidates: | |
| answer = Answer( | |
| answer=ABSTAIN_TEXT, | |
| abstain=True, | |
| confidence=0.0, | |
| missing_info=["no passage matched the question"], | |
| ) | |
| return Response( | |
| answer=answer, retrieved_chunk_ids=chunk_ids, retrieved_source_ids=source_ids | |
| ) | |
| top = result.candidates[0] | |
| confidence = self.confidence(top.score) | |
| if confidence < self.abstain_below: | |
| answer = Answer( | |
| answer=ABSTAIN_TEXT, | |
| abstain=True, | |
| confidence=confidence, | |
| missing_info=["the best passage matched weakly"], | |
| ) | |
| else: | |
| text = self.texts[top.row] | |
| citation = Citation(source_id=top.source_id, quote=cut_quote(text, self.quote_chars)) | |
| answer = Answer(answer=text, citations=[citation], abstain=False, confidence=confidence) | |
| return Response( | |
| answer=answer, retrieved_chunk_ids=chunk_ids, retrieved_source_ids=source_ids | |
| ) | |
| def build() -> DummySystem: | |
| """The registry factory: load System B's retriever once (~0.1 s plus imports).""" | |
| from coveragecheck.index import dense | |
| from coveragecheck.index.hybrid import Retriever | |
| retriever = Retriever.load(dense.fastembed_embedder()) | |
| return DummySystem(retriever, index_hash=retriever.lexical.input_hash) | |