Spaces:
Sleeping
Sleeping
File size: 3,383 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 | """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()
|