Home / Function/ from_examples() — langchain Function Reference

from_examples() — langchain Function Reference

Architecture documentation for the from_examples() function in semantic_similarity.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  7e87eda1_9870_51e4_5123_cecba78d6242["from_examples()"]
  3916b5eb_dc75_c546_ca45_bbc4a24287e8["SemanticSimilarityExampleSelector"]
  7e87eda1_9870_51e4_5123_cecba78d6242 -->|defined in| 3916b5eb_dc75_c546_ca45_bbc4a24287e8
  f6d64dfc_7955_63ff_b58c_f03972e0c865["from_examples()"]
  f6d64dfc_7955_63ff_b58c_f03972e0c865 -->|calls| 7e87eda1_9870_51e4_5123_cecba78d6242
  f6d64dfc_7955_63ff_b58c_f03972e0c865["from_examples()"]
  7e87eda1_9870_51e4_5123_cecba78d6242 -->|calls| f6d64dfc_7955_63ff_b58c_f03972e0c865
  5599ebbf_7d2d_cb9f_f035_4928a6716083["_example_to_text()"]
  7e87eda1_9870_51e4_5123_cecba78d6242 -->|calls| 5599ebbf_7d2d_cb9f_f035_4928a6716083
  style 7e87eda1_9870_51e4_5123_cecba78d6242 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/example_selectors/semantic_similarity.py lines 137–178

    def from_examples(
        cls,
        examples: list[dict],
        embeddings: Embeddings,
        vectorstore_cls: type[VectorStore],
        k: int = 4,
        input_keys: list[str] | None = None,
        *,
        example_keys: list[str] | None = None,
        vectorstore_kwargs: dict | None = None,
        **vectorstore_cls_kwargs: Any,
    ) -> SemanticSimilarityExampleSelector:
        """Create k-shot example selector using example list and embeddings.

        Reshuffles examples dynamically based on query similarity.

        Args:
            examples: List of examples to use in the prompt.
            embeddings: An initialized embedding API interface, e.g. OpenAIEmbeddings().
            vectorstore_cls: A vector store DB interface class, e.g. FAISS.
            k: Number of examples to select.
            input_keys: If provided, the search is based on the input variables
                instead of all variables.
            example_keys: If provided, keys to filter examples to.
            vectorstore_kwargs: Extra arguments passed to similarity_search function
                of the `VectorStore`.
            vectorstore_cls_kwargs: optional kwargs containing url for vector store

        Returns:
            The ExampleSelector instantiated, backed by a vector store.
        """
        string_examples = [cls._example_to_text(eg, input_keys) for eg in examples]
        vectorstore = vectorstore_cls.from_texts(
            string_examples, embeddings, metadatas=examples, **vectorstore_cls_kwargs
        )
        return cls(
            vectorstore=vectorstore,
            k=k,
            input_keys=input_keys,
            example_keys=example_keys,
            vectorstore_kwargs=vectorstore_kwargs,
        )

Domain

Subdomains

Called By

Frequently Asked Questions

What does from_examples() do?
from_examples() is a function in the langchain codebase, defined in libs/core/langchain_core/example_selectors/semantic_similarity.py.
Where is from_examples() defined?
from_examples() is defined in libs/core/langchain_core/example_selectors/semantic_similarity.py at line 137.
What does from_examples() call?
from_examples() calls 2 function(s): _example_to_text, from_examples.
What calls from_examples()?
from_examples() is called by 1 function(s): from_examples.

Analyze Your Own Codebase

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

Try Supermodel Free