haiku.rag/haiku_rag_slim/haiku/rag/reranking/base.py
Yiorgis Gozadinos 9ed66e3a06
Search one database the ordinary way, however it was selected
`sources=["alpha"]` on a client covering a set went through fusion, which
scores position: a result the database ranked at 0.6549 was reported as
1/(60+rank). Embedding also moved ahead of the repository, so a filter matching
no document embedded the query anyway.

A selection of one now runs the single-database search. Fusion reconciles
rankings from separate indexes, and one ranking has nothing to reconcile.

A reranker that returns chunks it built rather than the ones it was given loses
which database each came from, since ownership is by identity. That is named
now instead of surfacing as a KeyError, and stated on `RerankerBase._rerank`.
2026-08-27 14:41:10 +03:00

29 lines
1 KiB
Python

from haiku.rag.store.models.chunk import Chunk
class RerankerBase:
_model: str | None = None
async def rerank(
self, query: str, chunks: list[Chunk], top_n: int = 10
) -> list[tuple[Chunk, float]]:
if not chunks:
return []
return await self._rerank(query, chunks, top_n)
async def _rerank(
self, query: str, chunks: list[Chunk], top_n: int = 10
) -> list[tuple[Chunk, float]]:
"""Score and order `chunks`, returning the top `top_n`.
Return objects taken from `chunks`, not copies: searching several
databases maps a scored chunk back to the one holding it by identity,
because chunk ids repeat between copies of a database.
"""
raise NotImplementedError(
"Reranker is an abstract class. Please implement the _rerank method in a subclass."
)
async def aclose(self) -> None:
"""Release resources held by the reranker. No-op by default;
rerankers that own an HTTP client override this."""