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 (Union[List[List[float]], List[ndarray], ndarray]) – Matrix.

  • Y (Union[List[List[float]], List[ndarray], ndarray]) – Matrix, same width as X.

  • top_k (Optional[int]) – Max number of results to return.

  • score_threshold (Optional[float]) – 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.

Return type

Tuple[List[Tuple[int, int]], List[float]]