maximal_marginal_relevance#

langchain_mongodb.utils.maximal_marginal_relevance(query_embedding: ndarray, embedding_list: list, lambda_mult: float = 0.5, k: int = 4) List[int][source]#

Compute Maximal Marginal Relevance (MMR).

MMR is a technique used to select documents that are both relevant to the query and diverse among themselves. This function returns the indices of the top-k embeddings that maximize the marginal relevance.

Parameters:
  • query_embedding (np.ndarray) – The embedding vector of the query.

  • embedding_list (list of np.ndarray) – A list containing the embedding vectors of the candidate documents.

  • lambda_mult (float, optional) – The trade-off parameter between relevance and diversity. Defaults to 0.5.

  • k (int, optional) – The number of embeddings to select. Defaults to 4.

Returns:

The indices of the embeddings that maximize the marginal relevance.

Return type:

list of int

Notes

The Maximal Marginal Relevance (MMR) is computed using the following formula:

MMR = argmax_{D_i ∈ R S} [λ * Sim(D_i, Q) - (1 - λ) * max_{D_j ∈ S} Sim(D_i, D_j)]

where: - R is the set of candidate documents, - S is the set of selected documents, - Q is the query embedding, - Sim(D_i, Q) is the similarity between document D_i and the query, - Sim(D_i, D_j) is the similarity between documents D_i and D_j, - λ is the trade-off parameter.