Sixty-three comments said what the next statement already said: # Connect to LanceDB above connect_lancedb, # Path object above isinstance(source, Path), # Get page numbers from provenance above the prov loop, # Clear and populate results above list_view.clear(). They cost a read and carry nothing. The line is whether a comment restates one statement or labels a phase. Phase labels stay: the migrations keep # Create staging table with new schema and # Copy from staging to final table in batches, each heading ten lines of a long procedure. So do comments carrying a fact the code cannot: the merge_insert update-only note on document_meta, why the poller builds sources eagerly, why create_document_from_source returns a list for directories, that indexes need training data, the field-group markers in the config models, and the file:// URL-encoding note in create_document_from_source. capabilities/ is untouched. Its docstrings sit next to prompt surface, and changing them needs an eval to back it. The cassette-recording docs were wrong three ways. They named tests/test_qa.py::test_qa_anthropic, which no longer exists; they targeted whole modules, so a rewrite would re-record cassettes for services the recorder is not running; and they used COHERE_API_KEY where the SDK reads CO_API_KEY. docs/development.md now names exact tests with -n0, and the keyed example is test_cohere_reranker, which owns the one cassette recording api.cohere.com.
46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
from haiku.rag.utils import raise_missing_extra
|
|
|
|
try:
|
|
from zeroentropy import AsyncZeroEntropy
|
|
except ModuleNotFoundError as e: # pragma: no cover
|
|
raise_missing_extra("zeroentropy", "zeroentropy", e)
|
|
|
|
from haiku.rag.reranking.base import RerankerBase
|
|
from haiku.rag.store.models.chunk import Chunk
|
|
|
|
|
|
class ZeroEntropyReranker(RerankerBase): # pragma: no cover
|
|
"""Zero Entropy reranker implementation using the zerank-1 model."""
|
|
|
|
def __init__(self, model: str = "zerank-1"):
|
|
"""Initialize the Zero Entropy reranker.
|
|
|
|
Args:
|
|
model: The Zero Entropy model to use (default: "zerank-1")
|
|
"""
|
|
self._model = model
|
|
# Zero Entropy SDK reads ZEROENTROPY_API_KEY from environment by default
|
|
self._client = AsyncZeroEntropy()
|
|
|
|
async def _rerank(
|
|
self, query: str, chunks: list[Chunk], top_n: int = 10
|
|
) -> list[tuple[Chunk, float]]:
|
|
documents = [chunk.content for chunk in chunks]
|
|
|
|
model_name = self._model or "zerank-1"
|
|
response = await self._client.models.rerank(
|
|
model=model_name,
|
|
query=query,
|
|
documents=documents,
|
|
)
|
|
|
|
# Zero Entropy returns results sorted by relevance with scores
|
|
reranked_results = []
|
|
for result in response.results[:top_n]:
|
|
chunk_index = result.index
|
|
score = result.relevance_score
|
|
|
|
if chunk_index < len(chunks):
|
|
reranked_results.append((chunks[chunk_index], score))
|
|
|
|
return reranked_results
|