# maximal_marginal_relevance

> **Function** in `langchain_mongodb`

📖 [View in docs](https://reference.langchain.com/python/langchain-mongodb/utils/maximal_marginal_relevance)

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.

## Signature

```python
maximal_marginal_relevance(
    query_embedding: np.ndarray,
    embedding_list: list,
    lambda_mult: float = 0.5,
    k: int = 4,
) -> List[int]
```

## Description

**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.

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `query_embedding` | `np.ndarray` | Yes | The embedding vector of the query. |
| `embedding_list` | `list of np.ndarray` | Yes | A list containing the embedding vectors of the candidate documents. |
| `lambda_mult` | `float` | No | The trade-off parameter between relevance and diversity. Defaults to 0.5. (default: `0.5`) |
| `k` | `int` | No | The number of embeddings to select. Defaults to 4. (default: `4`) |

## Returns

`List[int]`

list of int: The indices of the embeddings that maximize the marginal relevance.

---

[View source on GitHub](https://github.com/langchain-ai/langchain-mongodb/blob/ad9050c28e092b335dcb846f77c0ec2245553f79/libs/langchain-mongodb/langchain_mongodb/utils.py#L77)