lethe-agent-memory / tests /test_rerank.py
Nagizaaz Shaik
feat: cross-encoder reranker over recalled memories
727017f
Raw History Blame Contribute Delete
1.33 kB
"""Tests for the cross-encoder reranker. Run from the Space root: python -m pytest tests.
These need the sentence-transformers model; they are skipped when it cannot be loaded (offline).
"""
import pathlib
import sys
from dataclasses import dataclass
import pytest
ROOT = pathlib.Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
import rerank # noqa: E402
@dataclass
class Cand:
text: str
@pytest.fixture(scope="module")
def _loaded():
try:
rerank._model()
except Exception as e: # noqa: BLE001 - offline or no model cache
pytest.skip(f"cross-encoder not available: {e}")
def test_shorter_than_two_is_unchanged():
assert rerank.rerank("anything", []) == []
one = [Cand("only one")]
assert rerank.rerank("anything", one) == one
def test_reranks_the_quarterly_review_weak_case(_loaded):
# The embedding ranker puts the distractor first because it shares "quarterly review"; the
# cross-encoder should lift the memory that answers who is presenting.
answer = Cand("Marcus will present the fraud dashboard at the quarterly review.")
distractor = Cand("The team agreed to ship the billing fix before the quarterly review.")
out = rerank.rerank("Who is presenting at the quarterly review?", [distractor, answer])
assert out[0] is answer