SemanticSimilarityExampleSelector#

class langchain_core.example_selectors.semantic_similarity.SemanticSimilarityExampleSelector[source]#

Bases: _VectorStoreExampleSelector

Select examples based on semantic similarity.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

param example_keys: list[str] | None = None#

Optional keys to filter examples to.

param 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.

param k: int = 4#

Number of examples to select.

param vectorstore: VectorStore [Required]#

VectorStore that contains information about examples.

param vectorstore_kwargs: dict[str, Any] | None = None#

Extra arguments passed to similarity_search function of the vectorstore.

async aadd_example(example: dict[str, str]) str#

Async add new example to vectorstore.

Parameters:

example (dict[str, str]) – A dictionary with keys as input variables and values as their values.

Returns:

The ID of the added example.

Return type:

str

add_example(example: dict[str, str]) str#

Add a new example to vectorstore.

Parameters:

example (dict[str, str]) – A dictionary with keys as input variables and values as their values.

Returns:

The ID of the added example.

Return type:

str

async classmethod afrom_examples(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[source]#

Async create k-shot example selector using example list and embeddings.

Reshuffles examples dynamically based on query similarity.

Parameters:
  • examples (list[dict]) – List of examples to use in the prompt.

  • embeddings (Embeddings) – An initialized embedding API interface, e.g. OpenAIEmbeddings().

  • vectorstore_cls (type[VectorStore]) – A vector store DB interface class, e.g. FAISS.

  • k (int) – Number of examples to select. Default is 4.

  • input_keys (Optional[list[str]]) – If provided, the search is based on the input variables instead of all variables.

  • example_keys (Optional[list[str]]) – If provided, keys to filter examples to.

  • vectorstore_kwargs (Optional[dict]) – Extra arguments passed to similarity_search function of the vectorstore.

  • vectorstore_cls_kwargs (Any) – optional kwargs containing url for vector store

Returns:

The ExampleSelector instantiated, backed by a vector store.

Return type:

SemanticSimilarityExampleSelector

async aselect_examples(input_variables: dict[str, str]) list[dict][source]#

Asynchronously select examples based on semantic similarity.

Parameters:

input_variables (dict[str, str]) – The input variables to use for search.

Returns:

The selected examples.

Return type:

list[dict]

classmethod from_examples(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[source]#

Create k-shot example selector using example list and embeddings.

Reshuffles examples dynamically based on query similarity.

Parameters:
  • examples (list[dict]) – List of examples to use in the prompt.

  • embeddings (Embeddings) – An initialized embedding API interface, e.g. OpenAIEmbeddings().

  • vectorstore_cls (type[VectorStore]) – A vector store DB interface class, e.g. FAISS.

  • k (int) – Number of examples to select. Default is 4.

  • input_keys (Optional[list[str]]) – If provided, the search is based on the input variables instead of all variables.

  • example_keys (Optional[list[str]]) – If provided, keys to filter examples to.

  • vectorstore_kwargs (Optional[dict]) – Extra arguments passed to similarity_search function of the vectorstore.

  • vectorstore_cls_kwargs (Any) – optional kwargs containing url for vector store

Returns:

The ExampleSelector instantiated, backed by a vector store.

Return type:

SemanticSimilarityExampleSelector

select_examples(input_variables: dict[str, str]) list[dict][source]#

Select examples based on semantic similarity.

Parameters:

input_variables (dict[str, str]) – The input variables to use for search.

Returns:

The selected examples.

Return type:

list[dict]

Examples using SemanticSimilarityExampleSelector