MaxMarginalRelevanceExampleSelector#

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

Bases: _VectorStoreExampleSelector

Select examples based on Max Marginal Relevance.

This was shown to improve performance in this paper: https://arxiv.org/pdf/2211.13892.pdf

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 fetch_k: int = 20#

Number of examples to fetch to rerank.

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 classmethod afrom_examples(
examples: list[dict],
embeddings: Embeddings,
vectorstore_cls: type[VectorStore],
*,
k: int = 4,
input_keys: list[str] | None = None,
fetch_k: int = 20,
example_keys: list[str] | None = None,
vectorstore_kwargs: dict | None = None,
**vectorstore_cls_kwargs: Any,
) MaxMarginalRelevanceExampleSelector[source]#

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

Reshuffles examples dynamically based on Max Marginal Relevance.

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.

  • fetch_k (int) – Number of Documents to fetch to pass to MMR algorithm. Default is 20.

  • 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:

MaxMarginalRelevanceExampleSelector

classmethod from_examples(
examples: list[dict],
embeddings: Embeddings,
vectorstore_cls: type[VectorStore],
k: int = 4,
input_keys: list[str] | None = None,
fetch_k: int = 20,
example_keys: list[str] | None = None,
vectorstore_kwargs: dict | None = None,
**vectorstore_cls_kwargs: Any,
) MaxMarginalRelevanceExampleSelector[source]#

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

Reshuffles examples dynamically based on Max Marginal Relevance.

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.

  • fetch_k (int) – Number of Documents to fetch to pass to MMR algorithm. Default is 20.

  • 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:

MaxMarginalRelevanceExampleSelector

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 aselect_examples(
input_variables: dict[str, str],
) list[dict][source]#

Asynchronously select examples based on Max Marginal Relevance.

Parameters:

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

Returns:

The selected examples.

Return type:

list[dict]

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

Select examples based on Max Marginal Relevance.

Parameters:

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

Returns:

The selected examples.

Return type:

list[dict]

Examples using MaxMarginalRelevanceExampleSelector