cosine_similarity_top_k#
- langchain_community.utils.math.cosine_similarity_top_k(X: List[List[float]] | List[ndarray] | ndarray, Y: List[List[float]] | List[ndarray] | ndarray, top_k: int | None = 5, score_threshold: float | None = None) Tuple[List[Tuple[int, int]], List[float]] [source]#
Row-wise cosine similarity with optional top-k and score threshold filtering.
- Parameters:
X (List[List[float]] | List[ndarray] | ndarray) – Matrix.
Y (List[List[float]] | List[ndarray] | ndarray) – Matrix, same width as X.
top_k (int | None) – Max number of results to return.
score_threshold (float | None) – 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]]