LengthBasedExampleSelector()Select examples based on length.
Example:
from langchain_core.example_selectors import LengthBasedExampleSelector
from langchain_core.prompts import PromptTemplate
# Define examples
examples = [
{"input": "happy", "output": "sad"},
{"input": "tall", "output": "short"},
{"input": "fast", "output": "slow"},
]
# Create prompt template
example_prompt = PromptTemplate(
input_variables=["input", "output"],
template="Input: {input}\nOutput: {output}",
)
# Create selector with max length constraint
selector = LengthBasedExampleSelector(
examples=examples,
example_prompt=example_prompt,
max_length=50, # Maximum prompt length
)
# Select examples for a new input
selected = selector.select_examples({"input": "large", "output": "tiny"})
# Returns examples that fit within max_length constraint