Skip to content

fix(chroma): add missing Euclidean relevance-score function #31643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions libs/partners/chroma/langchain_chroma/vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,16 @@ def similarity_search_with_vectors(

return _results_to_docs_and_vectors(results)

@staticmethod
def _euclidean_relevance_score_fn(distance: float) -> float:
"""Normalize Euclidean (L2) distance to a [0, 1] relevance score.

Uses the transformation ``1 / (1 + distance)`` so that:
* distance == 0 -> score == 1 (most relevant)
* distance -> ∞ -> score -> 0 (least relevant)
"""
return 1.0 / (1.0 + float(distance))

def _select_relevance_score_fn(self) -> Callable[[float], float]:
"""Select the relevance score function based on collections distance metric.

Expand Down
Loading