langchain.prompts.example_selector.semantic_similarity.MaxMarginalRelevanceExampleSelector¶

class langchain.prompts.example_selector.semantic_similarity.MaxMarginalRelevanceExampleSelector(*, vectorstore: VectorStore, k: int = 4, example_keys: Optional[List[str]] = None, input_keys: Optional[List[str]] = None, fetch_k: int = 20)[source]¶

Bases: SemanticSimilarityExampleSelector

ExampleSelector that selects 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: Optional[List[str]] = None¶

Optional keys to filter examples to.

param fetch_k: int = 20¶

Number of examples to fetch to rerank.

param input_keys: Optional[List[str]] = 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: langchain.vectorstores.base.VectorStore [Required]¶

VectorStore than contains information about examples.

add_example(example: Dict[str, str]) str¶

Add new example to vectorstore.

classmethod from_examples(examples: List[dict], embeddings: Embeddings, vectorstore_cls: Type[VectorStore], k: int = 4, input_keys: Optional[List[str]] = None, fetch_k: int = 20, **vectorstore_cls_kwargs: Any) MaxMarginalRelevanceExampleSelector[source]¶

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

Reshuffles examples dynamically based on query similarity.

Parameters
  • examples – List of examples to use in the prompt.

  • embeddings – An iniialized 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.

  • vectorstore_cls_kwargs – optional kwargs containing url for vector store

Returns

The ExampleSelector instantiated, backed by a vector store.

select_examples(input_variables: Dict[str, str]) List[dict][source]¶

Select which examples to use based on semantic similarity.

model Config¶

Bases: object

Configuration for this pydantic object.

arbitrary_types_allowed = True¶
extra = 'forbid'¶

Examples using MaxMarginalRelevanceExampleSelector¶