Top-k retrieval often returns five near-identical chunks, wasting the LLM's context window. Maximal Marginal Relevance (MMR) picks results that are relevant to the query and different from what's already been chosen:
Start with an empty selection . Repeatedly add the remaining document with the highest MMR score until are chosen. While is empty, the diversity term is 0. Similarity is cosine similarity. Break ties by the lower index.
Implement mmr(query, docs, k, lam=0.5) returning the indices of the chosen documents, in selection order. docs has shape (n, d). If k > n, return all indices.