Home / Class/ LengthBasedExampleSelector Class — langchain Architecture

LengthBasedExampleSelector Class — langchain Architecture

Architecture documentation for the LengthBasedExampleSelector class in length_based.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  0eacfd0f_9944_7bad_618f_81f1c3dafb2a["LengthBasedExampleSelector"]
  24f4d101_08ec_c1e7_bd03_b1308f5aa1aa["BaseExampleSelector"]
  0eacfd0f_9944_7bad_618f_81f1c3dafb2a -->|extends| 24f4d101_08ec_c1e7_bd03_b1308f5aa1aa
  948beb61_0a59_b5a6_60e4_d57473e17404["length_based.py"]
  0eacfd0f_9944_7bad_618f_81f1c3dafb2a -->|defined in| 948beb61_0a59_b5a6_60e4_d57473e17404
  60d0098a_fe66_aae7_9d5b_79c421b2c80c["add_example()"]
  0eacfd0f_9944_7bad_618f_81f1c3dafb2a -->|method| 60d0098a_fe66_aae7_9d5b_79c421b2c80c
  b24d41b3_2fff_0d6c_cbf6_bd3e3fe3c69b["aadd_example()"]
  0eacfd0f_9944_7bad_618f_81f1c3dafb2a -->|method| b24d41b3_2fff_0d6c_cbf6_bd3e3fe3c69b
  247d47d5_dd69_b76f_2f94_3dadf251744e["post_init()"]
  0eacfd0f_9944_7bad_618f_81f1c3dafb2a -->|method| 247d47d5_dd69_b76f_2f94_3dadf251744e
  064f78c2_7411_e02b_7566_cc055cccbf8f["select_examples()"]
  0eacfd0f_9944_7bad_618f_81f1c3dafb2a -->|method| 064f78c2_7411_e02b_7566_cc055cccbf8f
  becfa088_b785_9211_0e88_b427170ccd8a["aselect_examples()"]
  0eacfd0f_9944_7bad_618f_81f1c3dafb2a -->|method| becfa088_b785_9211_0e88_b427170ccd8a

Relationship Graph

Source Code

libs/core/langchain_core/example_selectors/length_based.py lines 17–128

class LengthBasedExampleSelector(BaseExampleSelector, BaseModel):
    r"""Select examples based on length.

    Example:
        ```python
        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
        ```
    """

    examples: list[dict]
    """A list of the examples that the prompt template expects."""

    example_prompt: PromptTemplate
    """Prompt template used to format the examples."""

    get_text_length: Callable[[str], int] = _get_length_based
    """Function to measure prompt length. Defaults to word count."""

    max_length: int = 2048
    """Max length for the prompt, beyond which examples are cut."""

    example_text_lengths: list[int] = Field(default_factory=list)
    """Length of each example."""

    def add_example(self, example: dict[str, str]) -> None:
        """Add new example to list.

        Args:
            example: A dictionary with keys as input variables
                and values as their values.
        """
        self.examples.append(example)
        string_example = self.example_prompt.format(**example)
        self.example_text_lengths.append(self.get_text_length(string_example))

    async def aadd_example(self, example: dict[str, str]) -> None:
        """Async add new example to list.

        Args:
            example: A dictionary with keys as input variables
                and values as their values.
        """
        self.add_example(example)

    @model_validator(mode="after")
    def post_init(self) -> Self:
        """Validate that the examples are formatted correctly."""
        if self.example_text_lengths:
            return self
        string_examples = [self.example_prompt.format(**eg) for eg in self.examples]
        self.example_text_lengths = [self.get_text_length(eg) for eg in string_examples]
        return self

    def select_examples(self, input_variables: dict[str, str]) -> list[dict]:
        """Select which examples to use based on the input lengths.

Frequently Asked Questions

What is the LengthBasedExampleSelector class?
LengthBasedExampleSelector is a class in the langchain codebase, defined in libs/core/langchain_core/example_selectors/length_based.py.
Where is LengthBasedExampleSelector defined?
LengthBasedExampleSelector is defined in libs/core/langchain_core/example_selectors/length_based.py at line 17.
What does LengthBasedExampleSelector extend?
LengthBasedExampleSelector extends BaseExampleSelector.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free