fix: normalize vector store scores to similarity (higher = better)
The threshold parameter in Memory.search() was silently broken for 11 vector stores because they returned raw distance scores (lower = better) while the threshold check assumed similarity scores (higher = better). This caused threshold filtering to be inverted — good matches got dropped and bad matches passed through. Convert all affected vector stores to return similarity scores: - Cosine distance stores: score = max(0.0, 1.0 - distance) - L2 distance stores: score = 1.0 / (1.0 + distance) - Stores computing similarity then discarding it: return similarity directly Fixes #4453 Related: #3283 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
431cba20e9
commit
224fd6c7d8
@@ -129,7 +129,7 @@ class TestMilvusDB:
|
||||
# Verify results are parsed correctly
|
||||
assert len(results) == 1
|
||||
assert results[0].id == "mem1"
|
||||
assert results[0].score == 0.8
|
||||
assert results[0].score == 0.8 # COSINE: Milvus returns similarity directly
|
||||
|
||||
def test_search_different_user_ids(self, milvus_db, mock_milvus_client):
|
||||
"""Test that search works with different user_ids (reproduces reported bug)."""
|
||||
@@ -228,10 +228,10 @@ class TestMilvusDB:
|
||||
|
||||
assert len(parsed) == 2
|
||||
assert parsed[0].id == "mem1"
|
||||
assert parsed[0].score == 0.9
|
||||
assert parsed[0].score == 0.9 # COSINE: Milvus returns similarity directly
|
||||
assert parsed[0].payload == {"user_id": "alice"}
|
||||
assert parsed[1].id == "mem2"
|
||||
assert parsed[1].score == 0.85
|
||||
assert parsed[1].score == 0.85 # COSINE: Milvus returns similarity directly
|
||||
|
||||
def test_update_with_none_vector_fetches_existing(self, milvus_db, mock_milvus_client):
|
||||
"""Test that update with vector=None fetches the existing vector (fixes #3708)."""
|
||||
|
||||
Reference in New Issue
Block a user