Home / Function/ similarity_search_with_relevance_scores() — langchain Function Reference

similarity_search_with_relevance_scores() — langchain Function Reference

Architecture documentation for the similarity_search_with_relevance_scores() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  42fc6116_575b_3b3d_aef3_7429e8fbe07e["similarity_search_with_relevance_scores()"]
  6c336ac6_f55c_1ad7_6db3_73dbd71fb625["VectorStore"]
  42fc6116_575b_3b3d_aef3_7429e8fbe07e -->|defined in| 6c336ac6_f55c_1ad7_6db3_73dbd71fb625
  bacd4785_4aaa_2a48_7d2a_aaff3f343f40["search()"]
  bacd4785_4aaa_2a48_7d2a_aaff3f343f40 -->|calls| 42fc6116_575b_3b3d_aef3_7429e8fbe07e
  c524ddff_060d_d388_e89e_f62f4a167558["_get_relevant_documents()"]
  c524ddff_060d_d388_e89e_f62f4a167558 -->|calls| 42fc6116_575b_3b3d_aef3_7429e8fbe07e
  af56f371_686f_e656_d90d_d283b46d217f["_similarity_search_with_relevance_scores()"]
  42fc6116_575b_3b3d_aef3_7429e8fbe07e -->|calls| af56f371_686f_e656_d90d_d283b46d217f
  style 42fc6116_575b_3b3d_aef3_7429e8fbe07e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/vectorstores/base.py lines 506–554

    def similarity_search_with_relevance_scores(
        self,
        query: str,
        k: int = 4,
        **kwargs: Any,
    ) -> list[tuple[Document, float]]:
        """Return docs and relevance scores in the range `[0, 1]`.

        `0` is dissimilar, `1` is most similar.

        Args:
            query: Input text.
            k: Number of `Document` objects to return.
            **kwargs: Kwargs to be passed to similarity search.

                Should include `score_threshold`, an optional floating point value
                between `0` to `1` to filter the resulting set of retrieved docs.

        Returns:
            List of tuples of `(doc, similarity_score)`.
        """
        score_threshold = kwargs.pop("score_threshold", None)

        docs_and_similarities = self._similarity_search_with_relevance_scores(
            query, k=k, **kwargs
        )
        if any(
            similarity < 0.0 or similarity > 1.0
            for _, similarity in docs_and_similarities
        ):
            warnings.warn(
                "Relevance scores must be between"
                f" 0 and 1, got {docs_and_similarities}",
                stacklevel=2,
            )

        if score_threshold is not None:
            docs_and_similarities = [
                (doc, similarity)
                for doc, similarity in docs_and_similarities
                if similarity >= score_threshold
            ]
            if len(docs_and_similarities) == 0:
                logger.warning(
                    "No relevant docs were retrieved using the "
                    "relevance score threshold %s",
                    score_threshold,
                )
        return docs_and_similarities

Subdomains

Frequently Asked Questions

What does similarity_search_with_relevance_scores() do?
similarity_search_with_relevance_scores() is a function in the langchain codebase, defined in libs/core/langchain_core/vectorstores/base.py.
Where is similarity_search_with_relevance_scores() defined?
similarity_search_with_relevance_scores() is defined in libs/core/langchain_core/vectorstores/base.py at line 506.
What does similarity_search_with_relevance_scores() call?
similarity_search_with_relevance_scores() calls 1 function(s): _similarity_search_with_relevance_scores.
What calls similarity_search_with_relevance_scores()?
similarity_search_with_relevance_scores() is called by 2 function(s): _get_relevant_documents, search.

Analyze Your Own Codebase

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

Try Supermodel Free