Home / Class/ _FewShotPromptTemplateMixin Class — langchain Architecture

_FewShotPromptTemplateMixin Class — langchain Architecture

Architecture documentation for the _FewShotPromptTemplateMixin class in few_shot.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  d2f3c5fa_0eaf_bafd_40aa_1d4884ff518c["_FewShotPromptTemplateMixin"]
  aa1f155e_f1a2_3609_0e63_d06bf3004d3f["few_shot.py"]
  d2f3c5fa_0eaf_bafd_40aa_1d4884ff518c -->|defined in| aa1f155e_f1a2_3609_0e63_d06bf3004d3f
  9ff2f0d5_406d_8c60_078a_7124546e026c["check_examples_and_selector()"]
  d2f3c5fa_0eaf_bafd_40aa_1d4884ff518c -->|method| 9ff2f0d5_406d_8c60_078a_7124546e026c
  9d58b55a_97b5_f494_fa04_e3243a64de25["_get_examples()"]
  d2f3c5fa_0eaf_bafd_40aa_1d4884ff518c -->|method| 9d58b55a_97b5_f494_fa04_e3243a64de25
  451110ca_cd2a_6b46_2728_65bad5d8be19["_aget_examples()"]
  d2f3c5fa_0eaf_bafd_40aa_1d4884ff518c -->|method| 451110ca_cd2a_6b46_2728_65bad5d8be19

Relationship Graph

Source Code

libs/core/langchain_core/prompts/few_shot.py lines 33–117

class _FewShotPromptTemplateMixin(BaseModel):
    """Prompt template that contains few shot examples."""

    examples: list[dict] | None = None
    """Examples to format into the prompt.

    Either this or `example_selector` should be provided.
    """

    example_selector: BaseExampleSelector | None = None
    """`ExampleSelector` to choose the examples to format into the prompt.

    Either this or `examples` should be provided.
    """

    model_config = ConfigDict(
        arbitrary_types_allowed=True,
        extra="forbid",
    )

    @model_validator(mode="before")
    @classmethod
    def check_examples_and_selector(cls, values: dict) -> Any:
        """Check that one and only one of `examples`/`example_selector` are provided.

        Args:
            values: The values to check.

        Returns:
            The values if they are valid.

        Raises:
            ValueError: If neither or both `examples` and `example_selector` are
                provided.
            ValueError: If both `examples` and `example_selector` are provided.
        """
        examples = values.get("examples")
        example_selector = values.get("example_selector")
        if examples and example_selector:
            msg = "Only one of 'examples' and 'example_selector' should be provided"
            raise ValueError(msg)

        if examples is None and example_selector is None:
            msg = "One of 'examples' and 'example_selector' should be provided"
            raise ValueError(msg)

        return values

    def _get_examples(self, **kwargs: Any) -> list[dict]:
        """Get the examples to use for formatting the prompt.

        Args:
            **kwargs: Keyword arguments to be passed to the example selector.

        Returns:
            List of examples.

        Raises:
            ValueError: If neither `examples` nor `example_selector` are provided.
        """
        if self.examples is not None:
            return self.examples
        if self.example_selector is not None:
            return self.example_selector.select_examples(kwargs)
        msg = "One of 'examples' and 'example_selector' should be provided"
        raise ValueError(msg)

    async def _aget_examples(self, **kwargs: Any) -> list[dict]:
        """Async get the examples to use for formatting the prompt.

        Args:
            **kwargs: Keyword arguments to be passed to the example selector.

        Returns:
            List of examples.

        Raises:
            ValueError: If neither `examples` nor `example_selector` are provided.
        """
        if self.examples is not None:
            return self.examples

Frequently Asked Questions

What is the _FewShotPromptTemplateMixin class?
_FewShotPromptTemplateMixin is a class in the langchain codebase, defined in libs/core/langchain_core/prompts/few_shot.py.
Where is _FewShotPromptTemplateMixin defined?
_FewShotPromptTemplateMixin is defined in libs/core/langchain_core/prompts/few_shot.py at line 33.

Analyze Your Own Codebase

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

Try Supermodel Free