CorrectiveRetrieval#
- class scikitplot.corpus.CorrectiveRetrieval(plan=None, grader=None, rewrite=None, max_rounds=2, fusion=None, deadline_seconds=None, on_error='degrade', clock=None)[source]#
Grade, then accept, retry with a rewritten query, or abstain (CRAG).
- Parameters:
- planRetrievalExecutionPlan or None, default=None
How one round retrieves:
plan.run(index, query).Noneisindex.search(query, **search_kwargs).- gradercallable or None, default=None
grader(query, hits) -> GradeResult.NoneisTermCoverageGrader().- rewritecallable or None, default=None
rewrite(query, rounds) -> str or None: the next round’s query, given the original query and the rounds so far. The proposal is stripped of surrounding whitespace;None(or a rewrite returningNoneor a query already run) means no retry. It is called only while rounds remain and the deadline has not passed.- max_roundsint, default=2
Rounds at most, the first included: the call budget.
- fusionFusionPolicy or None, default=None
Fuses the ambiguous rounds (one or more) for a
partialresult;NoneisFusionPolicy(). The leg of roundiis named"r{i}". A fusion that keeps no hit (every leg weighed 0) makes the resultabstained. Fusing several rounds needs each hit’s identity (doc_id); without one, the first ambiguous round is returned unfused andFUSE_FAILEDis recorded. One round is cut and weighed by the policy without needing an identity.- deadline_secondsfloat or None, default=None
No rewrite is asked for and no further round starts after this many seconds (checked before the rewrite and again before the round); a stop by the deadline is recorded (
DEADLINE_EXCEEDED), so the result isdegraded. Without a rewrite there is no further round, and no deadline stop.- on_error{“degrade”, “raise”}, default=”degrade”
A search, grader or rewrite that raises, whatever the reason (a keyword argument the index does not take included): recorded, with the round graded
incorrect("degrade"), or propagated. A failed or cancelled round is never graded: it is not evidence.- clockcallable or None, default=None
Seconds;
Noneistime.perf_counter.
- Raises:
- TypeError, ValueError
If a field is of the wrong type or out of range.
- Parameters:
Examples
An index that returns only documents holding every query word finds nothing for the first query; the rewrite drops a word:
>>> from types import SimpleNamespace as NS >>> from scikitplot.corpus import CorrectiveRetrieval, TermCoverageGrader >>> docs = {"a": "spans are slices", "b": "the cache key is a digest"} >>> class Index: ... def search(self, query): ... return [ ... NS(doc=NS(doc_id=k, text=t), score=1.0) ... for k, t in docs.items() ... if set(query.split()) <= set(t.split()) ... ] >>> corrective = CorrectiveRetrieval( ... grader=TermCoverageGrader(accept=0.6), ... rewrite=lambda query, rounds: " ".join(query.split()[:-1]) or None, ... ) >>> result = corrective.run(Index(), "cache key size") >>> result.verdict.value, [r.grade.grade.value for r in result.rounds] ('corrected', ['incorrect', 'correct']) >>> [h.doc.doc_id for h in result] ['b']
- bind(index, **search_kwargs)[source]#
Return
query -> self.run(index, query, **search_kwargs).- Parameters:
- indexobject
What every round searches.
- **search_kwargs
Passed to every round.
- Returns:
- callable
For
evaluate_retrieval.
- Parameters:
- Return type:
- fusion: FusionPolicy | None = None#
- run(index, query, **search_kwargs)[source]#
Run up to
max_roundsgraded rounds forquery.- Parameters:
- indexobject
Anything with
search(or whatplan.runtakes).- querystr
A non-empty query.
- **search_kwargs
Passed to
index.search(orplan.run) in every round.
- Returns:
- CorrectionResult
- Parameters:
- Return type: