*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to select examples by maximal marginal relevance (MMR)

How to select examples by maximal marginal relevance (MMR)
==========================================================

The `MaxMarginalRelevanceExampleSelector` selects examples based on a combination of which examples are most similar to the inputs, while also optimizing for diversity. It does this by finding the examples with the embeddings that have the greatest cosine similarity with the inputs, and then iteratively adding them while penalizing them for closeness to already selected examples.

    from langchain_community.vectorstores import FAISSfrom langchain_core.example_selectors import (    MaxMarginalRelevanceExampleSelector,    SemanticSimilarityExampleSelector,)from langchain_core.prompts import FewShotPromptTemplate, PromptTemplatefrom langchain_openai import OpenAIEmbeddingsexample_prompt = PromptTemplate(    input_variables=["input", "output"],    template="Input: {input}\nOutput: {output}",)# Examples of a pretend task of creating antonyms.examples = [    {"input": "happy", "output": "sad"},    {"input": "tall", "output": "short"},    {"input": "energetic", "output": "lethargic"},    {"input": "sunny", "output": "gloomy"},    {"input": "windy", "output": "calm"},]

**API Reference:**[FAISS](https://api.python.langchain.com/en/latest/vectorstores/langchain_community.vectorstores.faiss.FAISS.html) | [MaxMarginalRelevanceExampleSelector](https://api.python.langchain.com/en/latest/example_selectors/langchain_core.example_selectors.semantic_similarity.MaxMarginalRelevanceExampleSelector.html) | [SemanticSimilarityExampleSelector](https://api.python.langchain.com/en/latest/example_selectors/langchain_core.example_selectors.semantic_similarity.SemanticSimilarityExampleSelector.html) | [FewShotPromptTemplate](https://api.python.langchain.com/en/latest/prompts/langchain_core.prompts.few_shot.FewShotPromptTemplate.html) | [PromptTemplate](https://api.python.langchain.com/en/latest/prompts/langchain_core.prompts.prompt.PromptTemplate.html) | [OpenAIEmbeddings](https://api.python.langchain.com/en/latest/embeddings/langchain_openai.embeddings.base.OpenAIEmbeddings.html)

    example_selector = MaxMarginalRelevanceExampleSelector.from_examples(    # The list of examples available to select from.    examples,    # The embedding class used to produce embeddings which are used to measure semantic similarity.    OpenAIEmbeddings(),    # The VectorStore class that is used to store the embeddings and do a similarity search over.    FAISS,    # The number of examples to produce.    k=2,)mmr_prompt = FewShotPromptTemplate(    # We provide an ExampleSelector instead of examples.    example_selector=example_selector,    example_prompt=example_prompt,    prefix="Give the antonym of every input",    suffix="Input: {adjective}\nOutput:",    input_variables=["adjective"],)

    # Input is a feeling, so should select the happy/sad example as the first oneprint(mmr_prompt.format(adjective="worried"))

    Give the antonym of every inputInput: happyOutput: sadInput: windyOutput: calmInput: worriedOutput:

    # Let's compare this to what we would just get if we went solely off of similarity,# by using SemanticSimilarityExampleSelector instead of MaxMarginalRelevanceExampleSelector.example_selector = SemanticSimilarityExampleSelector.from_examples(    # The list of examples available to select from.    examples,    # The embedding class used to produce embeddings which are used to measure semantic similarity.    OpenAIEmbeddings(),    # The VectorStore class that is used to store the embeddings and do a similarity search over.    FAISS,    # The number of examples to produce.    k=2,)similar_prompt = FewShotPromptTemplate(    # We provide an ExampleSelector instead of examples.    example_selector=example_selector,    example_prompt=example_prompt,    prefix="Give the antonym of every input",    suffix="Input: {adjective}\nOutput:",    input_variables=["adjective"],)print(similar_prompt.format(adjective="worried"))

    Give the antonym of every inputInput: happyOutput: sadInput: sunnyOutput: gloomyInput: worriedOutput:

[Edit this page](https://github.com/langchain-ai/langchain/edit/master/docs/docs/how_to/example_selectors_mmr.ipynb)

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchain/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to select examples by length

](/v0.2/docs/how_to/example_selectors_length_based/)[

Next

How to select examples by n-gram overlap

](/v0.2/docs/how_to/example_selectors_ngram/)