LengthBasedExampleSelector#

class langchain_core.example_selectors.length_based.LengthBasedExampleSelector[source]#

Bases: BaseExampleSelector, BaseModel

Select examples based on length.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

param example_prompt: PromptTemplate [Required]#

Prompt template used to format the examples.

param example_text_lengths: list[int] [Optional]#

Length of each example.

param examples: list[dict] [Required]#

A list of the examples that the prompt template expects.

param get_text_length: Callable[[str], int] = <function _get_length_based>#

Function to measure prompt length. Defaults to word count.

param max_length: int = 2048#

Max length for the prompt, beyond which examples are cut.

async aadd_example(example: dict[str, str]) None[source]#

Async add new example to list.

Parameters:

example (dict[str, str]) – A dictionary with keys as input variables and values as their values.

Return type:

None

add_example(example: dict[str, str]) None[source]#

Add new example to list.

Parameters:

example (dict[str, str]) – A dictionary with keys as input variables and values as their values.

Return type:

None

async aselect_examples(input_variables: dict[str, str]) list[dict][source]#

Async select which examples to use based on the input lengths.

Parameters:

input_variables (dict[str, str]) – A dictionary with keys as input variables and values as their values.

Returns:

A list of examples to include in the prompt.

Return type:

list[dict]

select_examples(input_variables: dict[str, str]) list[dict][source]#

Select which examples to use based on the input lengths.

Parameters:

input_variables (dict[str, str]) – A dictionary with keys as input variables and values as their values.

Returns:

A list of examples to include in the prompt.

Return type:

list[dict]

Examples using LengthBasedExampleSelector