Skip to content

Commit 1ee02dd

Browse files
committed
CrateDB: Vector Store -- make _euclidean_relevance_score_fn identity f.
We don't need anything on top of it, ie we don't need this function and instead should use value from CrateDB as is. Similarity is already in the (0,1] interval and dividing by math.sqrt(2) won't normalize it but return wrong result, for example 1 will become 0.714.
1 parent 0561dcc commit 1ee02dd

File tree

2 files changed

+5
-6
lines changed
  • libs/community

2 files changed

+5
-6
lines changed

libs/community/langchain_community/vectorstores/cratedb/base.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import enum
4-
import math
54
from typing import (
65
Any,
76
Callable,
@@ -466,10 +465,10 @@ def _euclidean_relevance_score_fn(similarity: float) -> float:
466465
# others are not!)
467466
# - embedding dimensionality
468467
# - etc.
469-
# This function converts the euclidean norm of normalized embeddings
468+
# This function converts the Euclidean norm of normalized embeddings
470469
# (0 is most similar, sqrt(2) most dissimilar)
471470
# to a similarity function (0 to 1)
472471

473472
# Original:
474473
# return 1.0 - distance / math.sqrt(2)
475-
return similarity / math.sqrt(2)
474+
return similarity

libs/community/tests/integration_tests/vectorstores/test_cratedb.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,9 @@ def test_cratedb_relevance_score() -> None:
470470
output = docsearch.similarity_search_with_relevance_scores("foo", k=3)
471471
# Original score values: 1.0, 0.9996744261675065, 0.9986996093328621
472472
assert output == [
473-
(Document(page_content="foo", metadata={"page": "0"}), 0.7071067811865475),
474-
(Document(page_content="bar", metadata={"page": "1"}), 0.35355339059327373),
475-
(Document(page_content="baz", metadata={"page": "2"}), 0.1414213562373095),
473+
(Document(page_content="foo", metadata={"page": "0"}), 1.0),
474+
(Document(page_content="bar", metadata={"page": "1"}), 0.5),
475+
(Document(page_content="baz", metadata={"page": "2"}), 0.2),
476476
]
477477

478478

0 commit comments

Comments
 (0)