Retrieval and similarity#

Corpus supports its existing lexical/vector/hybrid retrieval stack and can also consume user scorers through CustomRetrievalIndex.

Hybrid fusion#

Every hybrid path fuses ranks by one rule, FusionPolicy (weighted reciprocal rank fusion): RetrievalIndex.search with match_mode="hybrid", fuse_by_rank, and the MCP hybrid retriever, which carries a byte-identical copy of the contract. A document scores the sum of weight / (k + rank) over the legs that list it, ranks counted from 1.

  • A document counts once per leg, at its best rank.

  • A weight-0 leg contributes nothing and adds no document, so hybrid_alpha=0 returns the lexical leg alone and 1 the dense leg alone, exactly; the other leg is not run and is reported SKIPPED. hybrid_alpha is shorthand for the weights {lexical: 1 - alpha, dense: alpha}.

  • Scores are compared exactly; a tie falls to the lower doc_id.

  • Each corpus hybrid hit carries its per-leg contributions; MCP results carry only the fused score.

from scikitplot.corpus import FusionPolicy

fused = FusionPolicy(k=60).fuse(
    [("lexical", ["A", "B", "C"]), ("dense", ["C", "A", "D"])]
)
[item.key for item in fused]                       # ['A', 'C', 'B', 'D']
[(c.leg, c.rank) for c in fused[0].contributions]  # [('lexical', 1), ('dense', 2)]
dict(FusionPolicy.from_alpha(1.0).weights)         # {'lexical': 0.0, 'dense': 1.0}

Probability fusion#

When every leg has a fitted calibrator (for example scikitplot.rank_bm25.ScoreCalibrator for BM25 scores and another for dense scores), LogOddsFusion combines the legs as evidence and returns probabilities of relevance instead of rank scores. Each leg passes its raw (doc_id, score) hits and its calibrator, so an uncalibrated score cannot enter; an unfitted calibrator is refused.

A document’s log-odds is logit(base_rate) plus, for every leg that listed it, weight * (logit(p) - logit(leg prevalence)): Bayes’ rule when the legs are independent given relevance. A leg that did not list the document adds nothing. base_rate is required; it moves every probability but never the order. Probabilities are clipped to [clip, 1 - clip] (default 1e-6) before the logit.

from scikitplot.corpus import LogOddsFusion, calibration_report

fusion = LogOddsFusion(base_rate=0.1, weights={"dense": 0.7})
result = fusion.fuse([
    ("lexical", lexical_hits, bm25_calibrator),
    ("dense", dense_hits, dense_calibrator),
])
[(item.key, item.probability) for item in result.items]
result.evidence_correlation   # {(0, 1): 0.26...}

Lexical and dense legs are rarely independent, and correlated legs make the fused probability over-confident. That cannot be measured without labels: check the fused probabilities with calibration_report on held-out labelled queries, and lower the weight of a correlated leg when they are over-confident. evidence_correlation is the marginal correlation of each pair of legs’ evidence over the documents both listed. It is positive whenever both legs are informative, so it does not test independence; a value near 1 means two legs are nearly duplicates. Rank fusion (FusionPolicy) needs no calibration and stays the default.

Hybrid search can fuse this way instead of by rank. Pass a ProbabilityFusion – the policy and a fitted calibrator per running leg – as RetrievalConfig(match_mode="hybrid", probability_fusion=...):

from scikitplot.corpus import LogOddsFusion, ProbabilityFusion, RetrievalConfig

config = RetrievalConfig(
    match_mode="hybrid",
    top_k=10,
    probability_fusion=ProbabilityFusion(
        policy=LogOddsFusion(base_rate=0.1, weights={"dense": 0.7}),
        calibrators={"lexical": bm25_calibrator, "dense": dense_calibrator},
    ),
)
hits = index.search(query, config=config, query_embedding=query_vector)
hits[0].score          # probability of relevance
hits[0].native_metric  # "logodds_probability"
  • Each leg’s native_score is calibrated: BM25 for "lexical", cosine similarity for "dense". Fit each calibrator on the scores that leg returns for the hybrid candidate pool (3 * top_k hits per leg, as in rank fusion).

  • Leg weights come from the policy; a leg runs only if its weight is above 0 and then needs a calibrator. hybrid_alpha must stay at its default, because its meaning (a rank-fusion weight) does not carry over.

  • Under a SecurityFilter scope, BM25 statistics (document count, document frequencies, average length) are computed over the documents the scope admits, so the same document scores differently than unscoped. Fit the lexical calibrator on the scoped index’s scores and pass its scope: ProbabilityFusion(policy=..., calibrators=..., scope=security_filter). Hybrid search then fuses only on an index bound to a scope with the same SecurityFilter.digest (SHA-256 of the expression’s canonical JSON; the label does not count). Any other scope, or none, is refused, and the error names both digests. A calibrator fitted unscoped (scope=None) is refused on a scoped index, and the reverse. Cosine similarity does not depend on the scope, so a dense-only fusion (lexical weight 0) runs under any scope.

Cutting a ranked list#

CutPolicy decides how many of a ranked list to keep instead of always top_k: "fixed" keeps max_k; "threshold" keeps every hit scoring at least threshold (K* = max{k : s_k >= threshold}); "max_gap" cuts after the largest drop between neighbours. min_k and max_k clamp all three, and CutResult records the rule’s evidence.

from scikitplot.corpus import CutPolicy

CutPolicy("max_gap").cut([0.9, 0.85, 0.8, 0.3, 0.25]).k      # 3
CutPolicy("threshold", threshold=0.5).apply(probability_hits)

A threshold is refused on scores outside [0, 1]: raw BM25, cosine and rank-fusion scores have no scale shared across queries, so one threshold would mean something different for each query. Use it on calibrated probabilities, such as probability-fusion hits. A large gap marks a ranking boundary; it does not show that the hits above it answer the question.

Caching searches#

CachedSearch wraps one index and returns a stored response when every input that decides it is unchanged: the index’s content generation (a rebuild changes it), its security filter by canonical expression, the query, every RetrievalConfig field, the caller’s filter and the query embedding’s bytes, dtype and shape.

from scikitplot.corpus import CachedSearch

cache = CachedSearch(index, max_entries=1024)
cache.search("merger plan", config=config)
cache.stats()   # hits, misses, stored, uncacheable, evicted, size

Only success and empty responses are kept, so a degraded answer is never replayed; a config with probability_fusion is answered by the index every time. Each hit returns a fresh copy of the hits list, and the cache is safe to share between threads. It is an exact cache: a similar query is a different question and is not served from it.

Levenshtein facade#

scikitplot.levenshtein is intentionally independent from Corpus and is always importable. The automatic backend preference is:

bundled scikitplot.cexternals._editdistance
    -> RapidFuzz when installed
    -> dependency-free Python fallback

The separately distributed Levenshtein/python-Levenshtein backend is supported only when explicitly requested; it is not silently selected by the automatic chain.

from scikitplot.levenshtein import distance, rank

distance("kitten", "sitting")
# 3

rank(
    "colour",
    ["color", "collar", "cloud"],
    score_cutoff=0.60,
)

Corpus compatibility is lazy:

from scikitplot.corpus import CustomRetrievalIndex, make_levenshtein_ranker

index = CustomRetrievalIndex(
    custom_scorer_fn=make_levenshtein_ranker(
        backend="auto",
        score_cutoff=0.70,
    )
)

Corpus owns the adapter: importing scikitplot.levenshtein never imports Corpus. make_pairwise_ranker does the same for any pairwise function, make_pairwise_ranker(edit_similarity) included.

score_cutoff uses the same normalized similarity scale [0, 1] for every backend, so switching between bundled, RapidFuzz and pure-Python implementations does not change the filtering contract. top_k is therefore an upper bound: a cutoff can intentionally return fewer hits when the remaining candidates are too dissimilar.

Choosing a metric#

Levenshtein distance is a character/sequence edit metric, not a semantic embedding model. It is useful for spelling variants, OCR noise, identifiers, near-duplicate labels and fuzzy lexical matching. Do not present edit-distance similarity as semantic equivalence.

Embedding cache#

EmbeddingEngine caches vectors on disk under a key that names the texts, the model and its revision, the backend, the provider and the output settings. A sentence-transformers model that is not pinned to a commit (model_revision="", or a branch or tag such as "main", which can move) names whatever the hub serves today, so by default its embeddings are computed and not cached; the call says so (EmbeddingEngine.embed_cached returns a CacheStatus). Pin model_revision to a commit hash (40 or 64 lowercase hex characters), or choose a CachePolicy:

  • CachePolicy(unpinned="resolve", revision_resolver=resolve_hub_revision) resolves the commit once per engine (a full Hub id "owner/name", not a local directory), then loads and keys exactly that commit; when resolution fails the model loads unpinned and the cache is bypassed (a branch or tag given in model_revision is not resolved);

  • CachePolicy(unpinned="allow") caches under the name given, as before: an entry can outlive an upstream change of the model.

Other backends do not load a revision: for them a non-empty model_revision is your statement of the model version, and it enables the cache. A custom provider is cached when it has a custom_fn_id. max_entries and max_bytes bound the cache directory: a hit marks its entry as used, and after a store the least recently used entries are evicted (only <64 hex>.npy files are counted or removed); one result larger than max_bytes is not kept, and evicts nothing. There is no time-to-live: whether an entry is stale is a question of which revision produced it, which the key answers.

from scikitplot.corpus import CachePolicy, EmbeddingEngine, resolve_hub_revision

name = "sentence-transformers/all-MiniLM-L6-v2"
commit = resolve_hub_revision(name)  # once; record it with your corpus
engine = EmbeddingEngine(
    model_name=name,
    model_revision=commit,
    cache_policy=CachePolicy(max_entries=10_000),
)
# engine.embed_cached(texts).status -> CacheStatus.STORED, then HIT

MultimodalEmbeddingEngine keys its cache on the content (every backend and model, the fusion, projection and normalisation, and each document’s modality, text and raw_tensor), not on the source path and its modification time. Its image and audio models have no revision, so it caches only with CachePolicy(unpinned="allow"); a custom backend or projection is never cached.

Retrieval execution plan#

RetrievalExecutionPlan runs a query through typed stages, always in this order: transform (the query to one or more queries), retrieve (index.search for each, unchanged), fuse (the queries’ rankings, by the same FusionPolicy), rerank (new scores for the first rerank_depth hits) and compress (character spans of each hit’s text, so the context stays a citation of its source). Only retrieve is required; an empty plan returns exactly what index.search returns.

Each stage reports a StageOutcome: its StageStatus (success, empty, degraded, failed, skipped or cancelled), the component that ran, items in and out, calls, seconds and budget, and an error record whenever it did not finish. Limits are checked before each call, so a spent limit stops the next call and says which limit stopped it; a running call is not interrupted. That bounds only a stage of several calls: a StageBudget limits the calls and seconds of retrieve (one call per query) and is refused for any other stage, and deadline_seconds stops any stage or query from starting after it. fuse, an in-process computation, always runs; a single call is bounded by the component’s own timeout. With on_error="degrade" (the default) a component that raises, or returns something malformed, fails its stage and the previous stage’s output is kept; on_error="raise" propagates the error. The result’s status is derived from the stages and the responses, as for a search.

from scikitplot.corpus import (
    CharBudgetCompressor,
    RetrievalConfig,
    RetrievalExecutionPlan,
    StageBudget,
    evaluate_retrieval,
)

plan = RetrievalExecutionPlan(
    retrieval=RetrievalConfig(match_mode="hybrid", top_k=20),
    transform=lambda q: [q + " definition"],  # run with the original query
    rerank=my_cross_encoder,  # (query, hits) -> one score per hit
    rerank_depth=20,
    top_k=10,
    compress=CharBudgetCompressor(4000),
    budgets={"retrieve": StageBudget(max_calls=4, max_seconds=5.0)},
    deadline_seconds=10.0,  # no stage starts after 10 s
)
result = plan.run(index, "what is a span?", embed_query=embedder.embed_query)
result.status, result.stage("rerank").seconds, result.passages
report = evaluate_retrieval(plan.bind(index, embed_query=embedder.embed_query), queries, qrels)

A plan is measured by the same harness as an index. On the pinned corpus of the chunking table above (recursive (500), keyword search, 82 queries made of the first half of a guide sentence), with a deterministic reranker that scores the longest run of query words a chunk contains. The queries are verbatim prefixes of the guide, so that reranker is close to an oracle: the table shows the stages compose and are measured, not what a learned reranker gains. The multi-query plan runs the half sentence and its two halves:

Plan

Recall@1

Recall@5

MRR@10

search

0.91

0.94

0.93

empty plan

0.91

0.94

0.93

rerank

0.94

0.94

0.94

multi-query (and halves)

0.90

0.94

0.92

With whole sentences as queries no stage changes these numbers: every miss there is a sentence that no chunk contains, which no ranking stage can recover. CharBudgetCompressor(1500) kept on average 1492 characters; the context held the sentence for 0.94 of the queries, against 0.95 whose ten hits held it: a budget drops the evidence that ranks too low to fit. No stage is a default.

Routing by query shape#

route chooses the evidence paths of a query by rule, never by a model. A plain query gets lexical and dense (and graph, when the index has one). A query whose words show a shape also gets the paths that shape needs, when the index has them; DEFAULT_ROUTING_POLICY knows five, in English:

Shape

Typical words

Adds

multi_hop

relationship between, whose

graph

aggregation

how many, total, average

metadata

negation

not, without, except

metadata

temporal

before, since, latest, a year

metadata

hierarchy

parent, section, part of

graph

from scikitplot.corpus import route

plan = route("how many sections are not about fusion", has_metadata=True)
plan.shapes        # ('aggregation', 'negation', 'hierarchy')
plan.legs          # lexical, dense, metadata
plan.unavailable   # graph: the index has no graph view

A RoutingPolicy is an ordered set of RoutingRule objects: a shape name, the legs it needs, whole-word phrases and an optional matcher(tokens) -> bool for what phrases cannot say (the default temporal rule reads four-digit years this way). Build one for another language or a domain vocabulary, extend the default with RoutingPolicy.with_rules, and pass it as route(query, policy=...); RoutingPolicy() detects nothing. Rules match lowercase whole words (query_tokens), never caller-supplied regular expressions, so routing is linear in the query’s length and a pure function of it. A quoted phrase still routes lexical only. The labelled routing set scikitplot/corpus/tests/data/routing_set.json pins the default policy’s behaviour, near misses (notebook, knot, sectional) included.

Corrective retrieval#

CorrectiveRetrieval grades what a search returned and decides what to do with it: correct evidence is accepted (corrected when a later round’s is); incorrect or ambiguous evidence is retried with a rewritten query while rounds remain (max_rounds, the call budget, and deadline_seconds, checked before each rewrite and again before each further round); when no round is accepted, the ambiguous rounds are fused by the same FusionPolicy and returned as partial evidence, or, when there are none, the retrieval abstained and returns no hit. Every round is graded against the original query, so a rewrite that drifts from it is not accepted for it. Each round is one index.search (or one RetrievalExecutionPlan run, with plan=), and the result reads like a search response, so evaluate_retrieval(corrective.bind(index), ...) measures it.

from scikitplot.corpus import CorrectiveRetrieval, TermCoverageGrader

corrective = CorrectiveRetrieval(
    grader=TermCoverageGrader(top_k=1, accept=0.9, reject=0.6),
    rewrite=my_rewriter,  # (query, rounds) -> next query, or None
    max_rounds=3,
)
result = corrective.run(index, "what keeps a chunk's span?")
result.verdict, [r.grade.grade for r in result.rounds], list(result)

TermCoverageGrader is a deterministic rule: the share of the query’s words that the top hits contain, against two thresholds. It measures overlap, not truth: a hit can hold every word and not answer the question. A model grader is any callable grader(query, hits) -> GradeResult, and its name is recorded with each grade. On the corpus of the tables above, with the 82 half-sentence queries (answerable), the same queries with three words of an unrelated page appended (noisy), and 14 half sentences of the Levenshtein guide, which is not indexed (off-corpus: the right answer is none):

Retrieval

Answerable Recall@1

Noisy R@1

Noisy answered

Off-corpus abstained

search

0.91

0.91

1.00

0.00

graded, top 3, 0.8 / 0.3

0.91

0.91

1.00

0.00

graded, top 1, 0.9 / 0.6

0.91

0.82

0.89

0.71

graded, top 1, 1.0 / 0.8

0.91

0.29

0.33

0.79

The default grader reads three hits, where some hit holds most words of any query, so it never abstains. Reading the first hit only, a strict grader abstains on most off-corpus queries and keeps every answerable one, at the price of noisy queries it no longer answers. Retrying with the halves of the query (up to three rounds) did not help: noisy recall went from 0.91 to 0.88 with the default grader, from 0.82 to 0.80 with the middle one (and off-corpus abstention from 0.71 to 0.57), and barely changed with the strictest (0.29 to 0.30), because a half query finds partial evidence that is then returned. The rewrite, not the loop, decides whether correction helps; the benchmark is maintenances/corpus/_maintenance/evidence/bench_r5b.py. The grader by default is TermCoverageGrader(), the table’s second row (graded, top 3, 0.8 / 0.3), which never abstains here; no rewrite is a default.