Home / Function/ __init__() — langchain Function Reference

__init__() — langchain Function Reference

Architecture documentation for the __init__() function in chat.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  fd4d40b2_9f02_c4c9_852d_d998c70f1d1e["__init__()"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6["ChatPromptTemplate"]
  fd4d40b2_9f02_c4c9_852d_d998c70f1d1e -->|defined in| 6be4a9a5_5fe4_e64f_c374_e63767576bf6
  0a946d27_710a_f7ff_e9e4_87561079dc25["_convert_to_message_template()"]
  fd4d40b2_9f02_c4c9_852d_d998c70f1d1e -->|calls| 0a946d27_710a_f7ff_e9e4_87561079dc25
  style fd4d40b2_9f02_c4c9_852d_d998c70f1d1e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/prompts/chat.py lines 902–995

    def __init__(
        self,
        messages: Sequence[MessageLikeRepresentation],
        *,
        template_format: PromptTemplateFormat = "f-string",
        **kwargs: Any,
    ) -> None:
        """Create a chat prompt template from a variety of message formats.

        Args:
            messages: Sequence of message representations.

                A message can be represented using the following formats:

                1. `BaseMessagePromptTemplate`
                2. `BaseMessage`
                3. 2-tuple of `(message type, template)`; e.g.,
                    `('human', '{user_input}')`
                4. 2-tuple of `(message class, template)`
                5. A string which is shorthand for `('human', template)`; e.g.,
                    `'{user_input}'`
            template_format: Format of the template.
            **kwargs: Additional keyword arguments passed to `BasePromptTemplate`,
                including (but not limited to):

                - `input_variables`: A list of the names of the variables whose values
                    are required as inputs to the prompt.
                - `optional_variables`: A list of the names of the variables for
                    placeholder or `MessagePlaceholder` that are optional.

                    These variables are auto inferred from the prompt and user need not
                    provide them.

                - `partial_variables`: A dictionary of the partial variables the prompt
                    template carries.

                    Partial variables populate the template so that you don't need to
                    pass them in every time you call the prompt.

                - `validate_template`: Whether to validate the template.
                - `input_types`: A dictionary of the types of the variables the prompt
                    template expects.

                    If not provided, all variables are assumed to be strings.

        Examples:
            Instantiation from a list of message templates:

            ```python
            template = ChatPromptTemplate(
                [
                    ("human", "Hello, how are you?"),
                    ("ai", "I'm doing well, thanks!"),
                    ("human", "That's good to hear."),
                ]
            )
            ```

            Instantiation from mixed message formats:

            ```python
            template = ChatPromptTemplate(
                [
                    SystemMessage(content="hello"),
                    ("human", "Hello, how are you?"),
                ]
            )
            ```
        """
        messages_ = [
            _convert_to_message_template(message, template_format)
            for message in messages
        ]

        # Automatically infer input variables from messages
        input_vars: set[str] = set()
        optional_variables: set[str] = set()
        partial_vars: dict[str, Any] = {}
        for _message in messages_:
            if isinstance(_message, MessagesPlaceholder) and _message.optional:
                partial_vars[_message.variable_name] = []

Subdomains

Frequently Asked Questions

What does __init__() do?
__init__() is a function in the langchain codebase, defined in libs/core/langchain_core/prompts/chat.py.
Where is __init__() defined?
__init__() is defined in libs/core/langchain_core/prompts/chat.py at line 902.
What does __init__() call?
__init__() calls 1 function(s): _convert_to_message_template.

Analyze Your Own Codebase

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

Try Supermodel Free