langchain_community.utils.math.cosine_similarity_top_kΒΆ

langchain_community.utils.math.cosine_similarity_top_k(X: Union[List[List[float]], List[ndarray], ndarray], Y: Union[List[List[float]], List[ndarray], ndarray], top_k: Optional[int] = 5, score_threshold: Optional[float] = None) Tuple[List[Tuple[int, int]], List[float]][source]ΒΆ

Row-wise cosine similarity with optional top-k and score threshold filtering.

Parameters
  • X – Matrix.

  • Y – Matrix, same width as X.

  • top_k – Max number of results to return.

  • score_threshold – Minimum cosine similarity of results.

Returns

Tuple of two lists. First contains two-tuples of indices (X_idx, Y_idx),

second contains corresponding cosine similarities.