Home / Class/ BaseStringMessagePromptTemplate Class — langchain Architecture

BaseStringMessagePromptTemplate Class — langchain Architecture

Architecture documentation for the BaseStringMessagePromptTemplate class in chat.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  4766cfc7_f772_eccc_6561_2122efd2c5e8["BaseStringMessagePromptTemplate"]
  75bfc804_f68f_7b77_a169_0fd019f759a2["BaseMessagePromptTemplate"]
  4766cfc7_f772_eccc_6561_2122efd2c5e8 -->|extends| 75bfc804_f68f_7b77_a169_0fd019f759a2
  d735cdf0_4dd6_b10d_3d65_7f753809639d["chat.py"]
  4766cfc7_f772_eccc_6561_2122efd2c5e8 -->|defined in| d735cdf0_4dd6_b10d_3d65_7f753809639d
  6966af16_ade2_de3e_4f06_f97eca43d8e0["from_template()"]
  4766cfc7_f772_eccc_6561_2122efd2c5e8 -->|method| 6966af16_ade2_de3e_4f06_f97eca43d8e0
  99bde657_97b6_c8a5_899c_2b894c683f03["from_template_file()"]
  4766cfc7_f772_eccc_6561_2122efd2c5e8 -->|method| 99bde657_97b6_c8a5_899c_2b894c683f03
  c5487c6c_d88c_b7f9_8f1f_8abaf332a76d["format()"]
  4766cfc7_f772_eccc_6561_2122efd2c5e8 -->|method| c5487c6c_d88c_b7f9_8f1f_8abaf332a76d
  3916fd95_3f4c_3139_05ee_a34850487826["aformat()"]
  4766cfc7_f772_eccc_6561_2122efd2c5e8 -->|method| 3916fd95_3f4c_3139_05ee_a34850487826
  3614db71_8c6d_b963_aa95_00d88d57c918["format_messages()"]
  4766cfc7_f772_eccc_6561_2122efd2c5e8 -->|method| 3614db71_8c6d_b963_aa95_00d88d57c918
  ce3d7cb2_bdf3_ef85_e7ac_243cf6168a0f["aformat_messages()"]
  4766cfc7_f772_eccc_6561_2122efd2c5e8 -->|method| ce3d7cb2_bdf3_ef85_e7ac_243cf6168a0f
  45230c2b_7a6d_5f8e_c2d6_80b39a0d02c4["input_variables()"]
  4766cfc7_f772_eccc_6561_2122efd2c5e8 -->|method| 45230c2b_7a6d_5f8e_c2d6_80b39a0d02c4
  a7370f78_d6a3_5fb0_404d_3cd84faa406a["pretty_repr()"]
  4766cfc7_f772_eccc_6561_2122efd2c5e8 -->|method| a7370f78_d6a3_5fb0_404d_3cd84faa406a

Relationship Graph

Source Code

libs/core/langchain_core/prompts/chat.py lines 225–350

class BaseStringMessagePromptTemplate(BaseMessagePromptTemplate, ABC):
    """Base class for message prompt templates that use a string prompt template."""

    prompt: StringPromptTemplate
    """String prompt template."""

    additional_kwargs: dict = Field(default_factory=dict)
    """Additional keyword arguments to pass to the prompt template."""

    @classmethod
    def from_template(
        cls,
        template: str,
        template_format: PromptTemplateFormat = "f-string",
        partial_variables: dict[str, Any] | None = None,
        **kwargs: Any,
    ) -> Self:
        """Create a class from a string template.

        Args:
            template: a template.
            template_format: format of the template.
            partial_variables: A dictionary of variables that can be used to partially
                fill in the template.

                For example, if the template is `"{variable1} {variable2}"`, and
                `partial_variables` is `{"variable1": "foo"}`, then the final prompt
                will be `"foo {variable2}"`.

            **kwargs: Keyword arguments to pass to the constructor.

        Returns:
            A new instance of this class.
        """
        prompt = PromptTemplate.from_template(
            template,
            template_format=template_format,
            partial_variables=partial_variables,
        )
        return cls(prompt=prompt, **kwargs)

    @classmethod
    def from_template_file(
        cls,
        template_file: str | Path,
        **kwargs: Any,
    ) -> Self:
        """Create a class from a template file.

        Args:
            template_file: path to a template file.
            **kwargs: Keyword arguments to pass to the constructor.

        Returns:
            A new instance of this class.
        """
        prompt = PromptTemplate.from_file(template_file)
        return cls(prompt=prompt, **kwargs)

    @abstractmethod
    def format(self, **kwargs: Any) -> BaseMessage:
        """Format the prompt template.

        Args:
            **kwargs: Keyword arguments to use for formatting.

        Returns:
            Formatted message.
        """

    async def aformat(self, **kwargs: Any) -> BaseMessage:
        """Async format the prompt template.

        Args:
            **kwargs: Keyword arguments to use for formatting.

        Returns:
            Formatted message.
        """
        return self.format(**kwargs)

Frequently Asked Questions

What is the BaseStringMessagePromptTemplate class?
BaseStringMessagePromptTemplate is a class in the langchain codebase, defined in libs/core/langchain_core/prompts/chat.py.
Where is BaseStringMessagePromptTemplate defined?
BaseStringMessagePromptTemplate is defined in libs/core/langchain_core/prompts/chat.py at line 225.
What does BaseStringMessagePromptTemplate extend?
BaseStringMessagePromptTemplate extends BaseMessagePromptTemplate.

Analyze Your Own Codebase

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

Try Supermodel Free