Spaces:
Sleeping
Sleeping
Download src/coveragecheck/index/query.py from TBM99/CoverageCheck: direct link, hf CLI and curl.
- Browser
- Download file 3.38 kB
-
https://huggingface.co/spaces/TBM99/CoverageCheck/resolve/main/src/coveragecheck/index/query.py
- Command line
-
hf download hf://spaces/TBM99/CoverageCheck/src/coveragecheck/index/query.py
-
curl -L -o query.py https://huggingface.co/spaces/TBM99/CoverageCheck/resolve/main/src/coveragecheck/index/query.py
3.38 kB
| """Step 3d: search System B from the command line (Step 3's Done-when check). | |
| uv run python -m coveragecheck.index.query "is a rented car covered in the US?" | |
| uv run python -m coveragecheck.index.query "non-earner benefit" --date 2026-01-01 | |
| uv run python -m coveragecheck.index.query "priority of payment" --form "OPCF 47" | |
| Prints the top chunks with their heading paths, both retrievers' ranks, any | |
| "possibly the wrong version" flags, and the timings (load once, then the search). | |
| Search results, not coverage advice: read the cited text. | |
| """ | |
| import argparse | |
| import sys | |
| import time | |
| from collections.abc import Callable | |
| from datetime import date | |
| from coveragecheck import config | |
| from coveragecheck.index import dense | |
| from coveragecheck.index.hybrid import Result, Retriever | |
| from coveragecheck.index.store import StaleIndexError | |
| def show(result: Result, retriever: Retriever, top: int) -> list[str]: | |
| """The lines to print for a result: one header line per chunk + its heading path.""" | |
| lines = [f"policy date {result.day}"] | |
| if result.flagged: | |
| lines.append( | |
| f"possibly the wrong version (not in force on that date): {sorted(result.flagged)}" | |
| ) | |
| if not result.candidates: | |
| lines.append("no results (the question has no searchable word)") | |
| for n, c in enumerate(result.candidates[:top], start=1): | |
| ranks = f"BM25 {c.lexical_rank or '-'}, dense {c.dense_rank or '-'}" | |
| lines.append(f"{n}. {c.chunk_id} (RRF {c.score:.4f}; {ranks})") | |
| lines.append(f" {retriever.chunks.heading_path.iat[c.row]}") | |
| return lines | |
| NOT_ADVICE = "Search results from public documents, not coverage advice: read the cited text." | |
| def default_retriever() -> Retriever: | |
| return Retriever.load(dense.fastembed_embedder()) | |
| def main(argv: list[str] | None = None, load: Callable[[], Retriever] = default_retriever) -> None: | |
| """Parse the arguments, load once (+ one warm-up search, so the timing below is the | |
| search alone), search, print. Known failures exit with one readable line instead of | |
| a traceback: a stale index (rebuild), an unknown form, a missing file or model.""" | |
| parser = argparse.ArgumentParser(description="Search System B's indexes.") | |
| parser.add_argument("question") | |
| parser.add_argument("--date", type=date.fromisoformat, help="policy date YYYY-MM-DD") | |
| parser.add_argument("--form", action="append", help="restrict to a form (repeatable)") | |
| parser.add_argument("--top", type=int, default=5) | |
| args = parser.parse_args(argv) | |
| try: | |
| start = time.perf_counter() | |
| retriever = load() | |
| retriever.search("warm up") | |
| loaded = time.perf_counter() - start | |
| start = time.perf_counter() | |
| result = retriever.search(args.question, args.date, args.form, config.CANDIDATE_K) | |
| searched = time.perf_counter() - start | |
| except StaleIndexError as error: | |
| sys.exit(f"{error}\nrebuild: uv run python -m coveragecheck.index.build") | |
| except (OSError, ValueError, RuntimeError) as error: | |
| sys.exit(f"search failed: {error}") | |
| print("\n".join(show(result, retriever, args.top))) | |
| print(f"\n{NOT_ADVICE}") | |
| print(f"loaded in {loaded:.2f} s (once per process); search took {1000 * searched:.0f} ms") | |
| if __name__ == "__main__": | |
| main() | |