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 if the input data cannot be parsed to form a valid model.

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 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, fetch_k: int = 20, example_keys: List[str] | None = None, vectorstore_kwargs: dict | None = None, **vectorstore_cls_kwargs: Any) MaxMarginalRelevanceExampleSelector[source]#

Asynchronously 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 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]

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

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