maximal_marginal_relevance#

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

Calculate maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to the query AND diversity among selected documents.

Parameters:
  • query_embedding (ndarray) – Embedding of the query text.

  • embedding_list (List[ndarray]) – List of embeddings to select from.

  • lambda_mult (float) – Number between 0 and 1 that determines the degree of diversity among the results, where 0 corresponds to maximum diversity and 1 to minimum diversity. Defaults to 0.5.

  • k (int) – Number of results to return. Defaults to 4.

Returns:

List of indices of selected embeddings.

Return type:

List[int]

Example

from langchain_redis import RedisVectorStore
from langchain_openai import OpenAIEmbeddings
import numpy as np

embeddings = OpenAIEmbeddings()
vector_store = RedisVectorStore(
    index_name="langchain-demo",
    embedding=embeddings,
    redis_url="redis://localhost:6379",
)

query = "What is the capital of France?"
query_embedding = embeddings.embed_query(query)

# Assuming you have a list of document embeddings
doc_embeddings = [embeddings.embed_query(doc) for doc in documents]

selected_indices = vector_store.maximal_marginal_relevance(
    query_embedding=np.array(query_embedding),
    embedding_list=[np.array(emb) for emb in doc_embeddings],
    lambda_mult=0.5,
    k=2
)

for idx in selected_indices:
    print(f"Selected document: {documents[idx]}")