Skip to content

Commit 782b00c

Browse files
committed
fix(storage): normalize Milvus cosine scores
Signed-off-by: Cheney Zhang <chen.zhang@zilliz.com>
1 parent 0148ffc commit 782b00c

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

openviking/storage/vectordb_adapters/milvus_adapter.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ def _score_from_hit(hit: Dict[str, Any], distance_metric: str) -> float:
205205
return 0.0
206206
if not math.isfinite(score):
207207
return 0.0
208+
if distance_metric == "cosine":
209+
return 1.0 - score
208210
if distance_metric == "l2":
209211
return 1.0 / (1.0 + max(score, 0.0))
210212
return score

tests/storage/test_milvus_adapter.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ def test_scope_roots_encoding_is_token_safe():
198198
assert "\n/a/c\n" not in encoded
199199

200200

201+
def test_score_from_cosine_distance_is_higher_is_better():
202+
from openviking.storage.vectordb_adapters.milvus_adapter import _score_from_hit
203+
204+
assert _score_from_hit({"distance": 0.0}, "cosine") == pytest.approx(1.0)
205+
assert _score_from_hit({"distance": 1.0}, "cosine") == pytest.approx(0.0)
206+
207+
201208
class _FakeSchema:
202209
def __init__(self) -> None:
203210
self.fields = []
@@ -358,6 +365,7 @@ def _new_adapter() -> MilvusCollectionAdapter:
358365
output_fields=["id", "uri", "abstract", "level"],
359366
)
360367
assert [item["id"] for item in result] == ["doc-1"]
368+
assert result[0]["_score"] == pytest.approx(1.0)
361369
assert adapter.count(Eq("account_id", "missing")) == 0
362370
assert adapter.count() == 2
363371
assert adapter.delete(ids=["doc-2"]) == 1

0 commit comments

Comments
 (0)