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:
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 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:
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:
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:
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.