LangChain

How to Use Select by Maximal Marginal Relevance (MMR) in LangChain?

LangChain is a module that can be used to build language models to interact with humans in natural languages. The humans provide the prompt in the textual form and the model uses an example selector to extract output using the query. Example selectors are used to fetch the output based on the input by choosing the closest relevant example to the query or prompt.

This guide will illustrate the process of using the select by Maximal Marginal Relevance example selector in LangChain.

How to Use Select by Maximal Marginal Relevance (MMR) in LangChain?

The Maximal Marginal Relevance example selector is used to extract information using the cosine similarity of the prompt and example. The cosine similarity is calculated after applying the embedding methods to the data and converting text into numerical form.

To learn the process of using the MMR example selector in LangChain, simply go through the listed steps:

Step 1: Install Modules

Start the process by installing the dependencies of the LangChain using the pip command:

pip install langchain

Install the OpenAI module to use its environment for applying the OpenAIEmbedding() method:

pip install openai

Install the FAISS framework which can be used to get the output using semantic similarity:

pip install faiss-gpu

Now, install tiktoken tokenizer for splitting the text into smaller chunks using the following code:

pip install tiktoken

Step 2: Using Libraries and Examples

The next step is to import libraries for building an MMR example selector, FAISS, OpenAIEmbeddings, and PromptTemplate. After importing the libraries, simply create an example set that gives inputs and output for their respective inputs in multiple arrays:

from langchain.prompts.example_selector import (
    MaxMarginalRelevanceExampleSelector,
    SemanticSimilarityExampleSelector,
)
from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings
from langchain.prompts import FewShotPromptTemplate, PromptTemplate

example_prompt = PromptTemplate(
    input_variables=["input", "output"],
    template="Input: {input}\nOutput: {output}",
)

examples = [
    {"input": "happy", "output": "sad"},
    {"input": "tall", "output": "short"},
    {"input": "energetic", "output": "lethargic"},
    {"input": "sunny", "output": "gloomy"},
    {"input": "windy", "output": "calm"},
]

Step 3: Building Example Selector

Now, start building the MMR example selector using MaxMarginalRelevanceExampleSelector() method containing different parameters:

example_selector = MaxMarginalRelevanceExampleSelector.from_examples(
    examples,
    OpenAIEmbeddings(),
    FAISS,
    k=2,
)
mmr_prompt = FewShotPromptTemplate(
    example_selector=example_selector,
    example_prompt=example_prompt,
    prefix="Give the antonym of every input",
    suffix="Input: {adjective}\nOutput:",
    input_variables=["adjective"],
)

Step 4: Testing the MMR Example Selector

Test the Maximal Marginal Relevance MMR example selector by calling it in the print() method with the input:

print(mmr_prompt.format(adjective="worried"))

Step 5: Using SemanticSimilarity

This step uses the SemanticSimilarityExampleSelector() method and then uses the FewShotPromptTemplate() method that is supported by the LangChain:

example_selector = SemanticSimilarityExampleSelector.from_examples(
    examples,
    OpenAIEmbeddings(),
    FAISS,
    k=2,
)
similar_prompt = FewShotPromptTemplate(
    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"))

That’s all about using the select by Maximal Marginal Relevance or MMR in LangChain.

Conclusion

To use the select by Maximal Marginal Relevance or MMR example selector in LangChain, install the required modules. After that, import the libraries to build the example set using the input and output prompt template. Build the MMR example selector to test it using the MMR example selector and FewShotPromptTemplate() method to get relevant output. This guide has illustrated the process of using the select-by-MMR example selector in LangChain.

About the author

Talha Mahmood

As a technical author, I am eager to learn about writing and technology. I have a degree in computer science which gives me a deep understanding of technical concepts and the ability to communicate them to a variety of audiences effectively.