Spaces:
Sleeping
Sleeping
Download src/coveragecheck/agent/schemas.py from TBM99/CoverageCheck: direct link, hf CLI and curl.
- Browser
- Download file 4.59 kB
-
https://huggingface.co/spaces/TBM99/CoverageCheck/resolve/main/src/coveragecheck/agent/schemas.py
- Command line
-
hf download hf://spaces/TBM99/CoverageCheck/src/coveragecheck/agent/schemas.py
-
curl -L -o schemas.py https://huggingface.co/spaces/TBM99/CoverageCheck/resolve/main/src/coveragecheck/agent/schemas.py
4.59 kB
| """What every system returns, and the interface the eval harness calls (SPEC Step 5; | |
| A, B and C share it). | |
| Plain Pydantic and dataclasses, no LangGraph or eval import, so Systems A and B (plain | |
| Python) and the deployed app can use it without pulling in pandas-heavy eval code | |
| (4b audit: Response and System lived in evals/run.py at first). Steps 5-7 validate | |
| each LLM output against Answer and re-ask on failure. | |
| """ | |
| from dataclasses import dataclass, field, fields | |
| from datetime import date | |
| from typing import Annotated, Protocol | |
| from pydantic import BaseModel, ConfigDict, Field | |
| class Citation(BaseModel): | |
| """A quoted passage and the section it comes from (checked in Step 8).""" | |
| model_config = ConfigDict(extra="forbid", frozen=True) | |
| source_id: str | |
| quote: str | |
| class Answer(BaseModel): | |
| """abstain=True means "I can't determine": missing_info then says what a broker | |
| would need to find out. confidence is the system's 0-1 estimate that its best | |
| answer is right, given even when it abstains: the eval's threshold curve abstains | |
| below a cut-off on it, so it must not mean "confidence in abstaining".""" | |
| model_config = ConfigDict(extra="forbid", frozen=True) | |
| answer: str | |
| citations: list[Citation] = [] | |
| abstain: bool | |
| missing_info: list[str] = [] | |
| confidence: Annotated[float, Field(ge=0.0, le=1.0)] | |
| class Work: | |
| """What a system did and spent on one question, apart from its answer. Kept separate | |
| so a question that FAILS still reports it (AnswerFailed.work): an error row must not | |
| look free or retrieve nothing (4b design audit). | |
| Contract (5b audit, binding for B and C): retrieved_* are EXACTLY the passages shown | |
| in the answer prompt, in prompt order (A: the 5 windows; B: after rerank and | |
| expansion; C: after llm_rank). Recall@k, the app's "passages shown" and Step 8's | |
| shown-text check all read them, so a system must not record a wider candidate set | |
| here (put that in trace). System A's windows have no section, so its | |
| retrieved_source_ids are doc_ids (4d scores A through covers_sources). | |
| tokens/llm_calls/reasks/api_retries/cache_hits/replayed_ms come from llm.Usage, | |
| SUMMED over every LLM call the question made (System C must add up route, rank, | |
| sufficiency and answer, also on failure). replayed_ms: model time of the calls | |
| answered from the cache (their time when first made); metrics adds it to | |
| latency_ms, which only saw a ~0 ms cache read. trace: free-form detail (e.g. C's | |
| graph steps), stored as JSON; never used for scoring.""" | |
| retrieved_chunk_ids: list[str] = field(default_factory=list) | |
| retrieved_source_ids: list[str] = field(default_factory=list) | |
| tokens_in: int = 0 | |
| tokens_out: int = 0 | |
| llm_calls: int = 0 | |
| reasks: int = 0 | |
| api_retries: int = 0 | |
| cache_hits: int = 0 | |
| replayed_ms: float = 0.0 | |
| trace: dict = field(default_factory=dict) | |
| class Response(Work): | |
| """What a system returns for one question: its Answer plus the Work behind it. | |
| answer is keyword-only (a dataclass rule: a field without a default can't follow | |
| Work's defaulted fields otherwise): Response(answer=..., retrieved_chunk_ids=...).""" | |
| answer: Answer = field(kw_only=True) | |
| def of(cls, answer: Answer, work: Work) -> "Response": | |
| """The Answer joined to the Work behind it, field by field (explicit, so it keeps | |
| working if Work ever gains properties or slots, unlike splatting vars()).""" | |
| return cls(answer=answer, **{f.name: getattr(work, f.name) for f in fields(Work)}) | |
| class AnswerFailed(Exception): | |
| """A system could not produce a valid Answer. kind says why (llm.FAILURE_KINDS: | |
| api, refusal, stopped, truncated, invalid); work is what it retrieved and spent | |
| before failing, recorded on the error row. A model refusal is an error, never | |
| turned into an abstention (owner, 5b).""" | |
| def __init__(self, message: str, kind: str, work: Work): | |
| super().__init__(message) | |
| self.kind, self.work = kind, work | |
| class System(Protocol): | |
| """Anything that answers a question for a policy date. It only ever gets the | |
| question text and the date, never the gold. settings go into the run's metadata | |
| (model, k, thresholds, index hash); warm_up loads lazy models so the first timed | |
| question doesn't pay for them (a no-op for systems with nothing to load).""" | |
| name: str | |
| settings: dict | |
| def warm_up(self) -> None: ... | |
| def answer(self, question: str, day: date) -> Response: ... | |