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:
Install the OpenAI module to use its environment for applying the OpenAIEmbedding() method:
Install the FAISS framework which can be used to get the output using semantic similarity:
Now, install tiktoken tokenizer for splitting the text into smaller chunks using the following code:
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:
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:
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:
Step 5: Using SemanticSimilarity
This step uses the SemanticSimilarityExampleSelector() method and then uses the FewShotPromptTemplate() method that is supported by the LangChain:
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.