Home / Function/ max_marginal_relevance_search() — langchain Function Reference

max_marginal_relevance_search() — langchain Function Reference

Architecture documentation for the max_marginal_relevance_search() function in vectorstores.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  c17a2c48_ebf6_0c5a_fc95_55ab8d53b4a8["max_marginal_relevance_search()"]
  bf62db79_4217_463c_798f_6f8528ed0d6e["Qdrant"]
  c17a2c48_ebf6_0c5a_fc95_55ab8d53b4a8 -->|defined in| bf62db79_4217_463c_798f_6f8528ed0d6e
  ce92a056_502f_c4a0_9d88_124ae4b56bb5["_embed_query()"]
  c17a2c48_ebf6_0c5a_fc95_55ab8d53b4a8 -->|calls| ce92a056_502f_c4a0_9d88_124ae4b56bb5
  356b00a0_41fb_37f6_c2e0_41baee3bf61a["max_marginal_relevance_search_by_vector()"]
  c17a2c48_ebf6_0c5a_fc95_55ab8d53b4a8 -->|calls| 356b00a0_41fb_37f6_c2e0_41baee3bf61a
  style c17a2c48_ebf6_0c5a_fc95_55ab8d53b4a8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/qdrant/langchain_qdrant/vectorstores.py lines 724–785

    def max_marginal_relevance_search(
        self,
        query: str,
        k: int = 4,
        fetch_k: int = 20,
        lambda_mult: float = 0.5,
        filter: MetadataFilter | None = None,  # noqa: A002
        search_params: models.SearchParams | None = None,
        score_threshold: float | None = None,
        consistency: models.ReadConsistency | None = None,
        **kwargs: Any,
    ) -> list[Document]:
        """Return docs selected using the maximal marginal relevance.

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

        Args:
            query: Text to look up documents similar to.
            k: Number of Documents to return.
            fetch_k: Number of Documents to fetch to pass to MMR algorithm.
            lambda_mult: Number between `0` and `1` that determines the degree
                of diversity among the results with `0` corresponding to maximum
                diversity and `1` to minimum diversity.
            filter: Filter by metadata.
            search_params: Additional search params
            score_threshold:
                Define a minimal score threshold for the result.
                If defined, less similar results will not be returned.
                Score of the returned result might be higher or smaller than the
                threshold depending on the Distance function used.
                E.g. for cosine similarity only higher scores will be returned.
            consistency:
                Read consistency of the search. Defines how many replicas should be
                queried before returning the result.
                Values:
                - int - number of replicas to query, values should present in all
                        queried replicas
                - 'majority' - query all replicas, but return values present in the
                    majority of replicas
                - 'quorum' - query the majority of replicas, return values present in
                    all of them
                - 'all' - query all replicas, and return values present in all replicas
            **kwargs:
                Any other named arguments to pass through to QdrantClient.search()

        Returns:
            List of `Document` objects selected by maximal marginal relevance.

        """
        query_embedding = self._embed_query(query)
        return self.max_marginal_relevance_search_by_vector(
            query_embedding,
            k=k,
            fetch_k=fetch_k,
            lambda_mult=lambda_mult,
            filter=filter,
            search_params=search_params,
            score_threshold=score_threshold,
            consistency=consistency,
            **kwargs,
        )

Subdomains

Frequently Asked Questions

What does max_marginal_relevance_search() do?
max_marginal_relevance_search() is a function in the langchain codebase, defined in libs/partners/qdrant/langchain_qdrant/vectorstores.py.
Where is max_marginal_relevance_search() defined?
max_marginal_relevance_search() is defined in libs/partners/qdrant/langchain_qdrant/vectorstores.py at line 724.
What does max_marginal_relevance_search() call?
max_marginal_relevance_search() calls 2 function(s): _embed_query, max_marginal_relevance_search_by_vector.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free