Home / File/ semantic_similarity.py — langchain Source File

semantic_similarity.py — langchain Source File

Architecture documentation for semantic_similarity.py, a python file in the langchain codebase. 7 imports, 0 dependents.

File python PromptManagement ExampleSelection 7 imports 2 functions 3 classes

Entity Profile

Dependency Diagram

graph LR
  68b41b07_21c1_a50f_08fe_c4151de58027["semantic_similarity.py"]
  cccbe73e_4644_7211_4d55_e8fb133a8014["abc"]
  68b41b07_21c1_a50f_08fe_c4151de58027 --> cccbe73e_4644_7211_4d55_e8fb133a8014
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  68b41b07_21c1_a50f_08fe_c4151de58027 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  68b41b07_21c1_a50f_08fe_c4151de58027 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  86712768_7d49_e4ba_237c_f0dc6b157dd7["langchain_core.example_selectors.base"]
  68b41b07_21c1_a50f_08fe_c4151de58027 --> 86712768_7d49_e4ba_237c_f0dc6b157dd7
  d55af636_303c_0eb6_faee_20d89bd952d5["langchain_core.vectorstores"]
  68b41b07_21c1_a50f_08fe_c4151de58027 --> d55af636_303c_0eb6_faee_20d89bd952d5
  c554676d_b731_47b2_a98f_c1c2d537c0aa["langchain_core.documents"]
  68b41b07_21c1_a50f_08fe_c4151de58027 --> c554676d_b731_47b2_a98f_c1c2d537c0aa
  bc46b61d_cfdf_3f6b_a9dd_ac2a328d84b3["langchain_core.embeddings"]
  68b41b07_21c1_a50f_08fe_c4151de58027 --> bc46b61d_cfdf_3f6b_a9dd_ac2a328d84b3
  style 68b41b07_21c1_a50f_08fe_c4151de58027 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Example selector that selects examples based on SemanticSimilarity."""

from __future__ import annotations

from abc import ABC
from typing import TYPE_CHECKING, Any

from pydantic import BaseModel, ConfigDict

from langchain_core.example_selectors.base import BaseExampleSelector
from langchain_core.vectorstores import VectorStore

if TYPE_CHECKING:
    from langchain_core.documents import Document
    from langchain_core.embeddings import Embeddings


def sorted_values(values: dict[str, str]) -> list[Any]:
    """Return a list of values in dict sorted by key.

    Args:
        values: A dictionary with keys as input variables
            and values as their values.

    Returns:
        A list of values in dict sorted by key.
    """
    return [values[val] for val in sorted(values)]


class _VectorStoreExampleSelector(BaseExampleSelector, BaseModel, ABC):
    """Example selector that selects examples based on SemanticSimilarity."""

    vectorstore: VectorStore
    """VectorStore that contains information about examples."""
    k: int = 4
    """Number of examples to select."""
    example_keys: list[str] | None = None
    """Optional keys to filter examples to."""
    input_keys: list[str] | None = None
    """Optional keys to filter input to. If provided, the search is based on
    the input variables instead of all variables."""
    vectorstore_kwargs: dict[str, Any] | None = None
    """Extra arguments passed to similarity_search function of the `VectorStore`."""

    model_config = ConfigDict(
        arbitrary_types_allowed=True,
        extra="forbid",
    )

    @staticmethod
    def _example_to_text(example: dict[str, str], input_keys: list[str] | None) -> str:
        if input_keys:
            return " ".join(sorted_values({key: example[key] for key in input_keys}))
        return " ".join(sorted_values(example))

    def _documents_to_examples(self, documents: list[Document]) -> list[dict]:
        # Get the examples from the metadata.
        # This assumes that examples are stored in metadata.
        examples = [dict(e.metadata) for e in documents]
// ... (299 more lines)

Subdomains

Dependencies

  • abc
  • langchain_core.documents
  • langchain_core.embeddings
  • langchain_core.example_selectors.base
  • langchain_core.vectorstores
  • pydantic
  • typing

Frequently Asked Questions

What does semantic_similarity.py do?
semantic_similarity.py is a source file in the langchain codebase, written in python. It belongs to the PromptManagement domain, ExampleSelection subdomain.
What functions are defined in semantic_similarity.py?
semantic_similarity.py defines 2 function(s): langchain_core, sorted_values.
What does semantic_similarity.py depend on?
semantic_similarity.py imports 7 module(s): abc, langchain_core.documents, langchain_core.embeddings, langchain_core.example_selectors.base, langchain_core.vectorstores, pydantic, typing.
Where is semantic_similarity.py in the architecture?
semantic_similarity.py is located at libs/core/langchain_core/example_selectors/semantic_similarity.py (domain: PromptManagement, subdomain: ExampleSelection, directory: libs/core/langchain_core/example_selectors).

Analyze Your Own Codebase

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

Try Supermodel Free