LangChain

How to Use Length-Based Example Selector in LangChain?

LangChain is used to build natural language models that can be used to have a conversation with humans in their language like English, etc. The developer uses multiple datasets or example data to train these models, and it is not possible to use all these examples all the time. So, example selectors are used to choose the dataset or example based on multiple factors and length is one of these selectors.

This post will demonstrate the process of using the select-by-length example selector in LangChain.

How to Use Length-Based Example Selector in LangChain?

Example selectors are used to select the data or examples to be used for training the models. The length-based is the process of choosing these examples using their length. The select-by-length example is mostly used when the length of the prompt exceeds the length of the context.

To learn how to use the select-by-length example selector in LangChain, simply go through the following guide:

Step 1: Install LangChain

Firstly, start the process of using the select-by-length example selector by installing the LangChain framework:

pip install langchain

Step 2: Building Example Selector

After that, simply import the libraries for configuring the example selector with multiple examples and methods like “example_prompt”, “example_selector”, and “dynamic_prompt”:

from langchain.prompts import PromptTemplate
from langchain.prompts import FewShotPromptTemplate
from langchain.prompts.example_selector import LengthBasedExampleSelector

examples = [
    {"get": "tiny", "post": "large"},
    {"get": "hate", "post": "love"},
    {"get": "ill", "post": "well"},
    {"get": "shrink", "post": "grow"},
    {"get": "soft", "post": "hard"},
]
example_prompt = PromptTemplate(
    input_variables=["get", "post"],
    template="Input: {get}\nOutput: {post}",
)
# Configure length-based example selector by providing or limiting the maximum length of the query
example_selector = LengthBasedExampleSelector(
    examples=examples,
    example_prompt=example_prompt,
    max_length=25,
)
# Configure dynamic_prompt using the FewShotPrompttemplate() method to set the template of the query
dynamic_prompt = FewShotPromptTemplate(
    example_selector=example_selector,
    example_prompt=example_prompt,
    prefix="I want to get the antonym of each object",
    suffix="Query: {style}\nResponse:",
    input_variables=["style"],
)

Step 3: Using Small Input

Now, test the example selector using a small prompt of only a single word to extract the template on the screen:

print(dynamic_prompt.format(style="big"))

Step 4: Using Long Input

After that, simply use a bigger prompt or query with multiple words and assign it to the “long_string” variable:

long_string = "big and huge and massive and large and gigantic and tall and more bigger than every other query"
print(dynamic_prompt.format(style=long_string))

Step 5: Adding Example to Example Selector

The next step is used to add the example to the example selector using the dynamic_prompt() method:

new_example = {"get": "big", "post": "small"}
dynamic_prompt.example_selector.add_example(new_example)
print(dynamic_prompt.format(style="enthusiastic"))

That is all about using the length-based example selector in LangChain.

Conclusion

To use the select-by-length example selector in LangChain, install the LangChain framework to import the libraries for building the example selector. After that, use a smaller prompt to check the output using the length-based example selector, and then use a longer prompt to get the most appropriate example. The user can also use the example selector to add another example in it using the dynamic_prompt() method. This post has illustrated the process of using the select-by-length 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.