Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/fuser/knowledge_base/faiss/faiss_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def search(self, query_embedding: np.ndarray, top_k: int = 5) -> list[Document]:
if idx < 0 or idx >= len(self.documents):
continue
doc = self.documents[idx]
score = float(dist)
score = float(1.0 / (1.0 + dist))
results.append(
Document(text=doc.text, metadata=doc.metadata.copy(), score=score)
)
Expand Down
10 changes: 5 additions & 5 deletions tests/fuser/knowledge_base/faiss/test_faiss_retriever.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,11 @@ def test_index_is_loaded_on_init(self, mock_faiss_index):
mock_load.assert_called_once()

def test_score_calculation(self, mock_faiss_index):
"""Test that score is calculated correctly from distance."""
"""Test that score is calculated consistently with batch_search."""
index_path, metadata_path, dim, _ = mock_faiss_index
retriever = FAISSRetriever(index_path, metadata_path)

with patch.object(retriever.index, "search") as mock_search:
# For IndexFlatIP, dist is already cosine similarity
mock_search.return_value = (
np.array([[0.95, 0.85, 0.70]], dtype="float32"),
np.array([[0, 1, 2]], dtype="int64"),
Expand All @@ -274,9 +273,10 @@ def test_score_calculation(self, mock_faiss_index):
query_embedding = np.random.randn(dim).astype("float32")
results = retriever.search(query_embedding, top_k=3)

assert results[0].score == pytest.approx(0.95, rel=1e-5)
assert results[1].score == pytest.approx(0.85, rel=1e-5)
assert results[2].score == pytest.approx(0.70, rel=1e-5)
# Score uses 1/(1+dist) normalization, consistent with batch_search
assert results[0].score == pytest.approx(1.0 / (1.0 + 0.95), rel=1e-5)
assert results[1].score == pytest.approx(1.0 / (1.0 + 0.85), rel=1e-5)
assert results[2].score == pytest.approx(1.0 / (1.0 + 0.70), rel=1e-5)

def test_batch_search_returns_independent_results(self, mock_faiss_index):
"""Test that batch search returns independent result sets."""
Expand Down
Loading