Select examples based on length.
LengthBasedExampleSelector()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 constraintA list of the examples that the prompt template expects.
Prompt template used to format the examples.
Function to measure prompt length. Defaults to word count.
Max length for the prompt, beyond which examples are cut.
Length of each example.
Add new example to list.
Async add new example to list.
Validate that the examples are formatted correctly.
Select which examples to use based on the input lengths.
Async select which examples to use based on the input lengths.