Home / Function/ from_messages() — langchain Function Reference

from_messages() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  868943e8_3763_c08b_6ee7_cd2801948c3a["from_messages()"]
  6be4a9a5_5fe4_e64f_c374_e63767576bf6["ChatPromptTemplate"]
  868943e8_3763_c08b_6ee7_cd2801948c3a -->|defined in| 6be4a9a5_5fe4_e64f_c374_e63767576bf6
  05fbd76f_5319_5d90_fd32_8ae595f01924["__add__()"]
  05fbd76f_5319_5d90_fd32_8ae595f01924 -->|calls| 868943e8_3763_c08b_6ee7_cd2801948c3a
  fb7525a5_49cc_cee5_9a12_dffafec8f35b["from_template()"]
  fb7525a5_49cc_cee5_9a12_dffafec8f35b -->|calls| 868943e8_3763_c08b_6ee7_cd2801948c3a
  490adae9_6c8b_f224_cfeb_133ca6ea29c8["partial()"]
  490adae9_6c8b_f224_cfeb_133ca6ea29c8 -->|calls| 868943e8_3763_c08b_6ee7_cd2801948c3a
  559cafe7_23f5_323e_933a_c91d4152905d["__getitem__()"]
  559cafe7_23f5_323e_933a_c91d4152905d -->|calls| 868943e8_3763_c08b_6ee7_cd2801948c3a
  style 868943e8_3763_c08b_6ee7_cd2801948c3a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/prompts/chat.py lines 1119–1167

    def from_messages(
        cls,
        messages: Sequence[MessageLikeRepresentation],
        template_format: PromptTemplateFormat = "f-string",
    ) -> ChatPromptTemplate:
        """Create a chat prompt template from a variety of message formats.

        Examples:
            Instantiation from a list of message templates:

            ```python
            template = ChatPromptTemplate.from_messages(
                [
                    ("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.from_messages(
                [
                    SystemMessage(content="hello"),
                    ("human", "Hello, how are you?"),
                ]
            )
            ```
        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.

        Returns:
            A chat prompt template.

        """
        return cls(messages, template_format=template_format)

Subdomains

Frequently Asked Questions

What does from_messages() do?
from_messages() is a function in the langchain codebase, defined in libs/core/langchain_core/prompts/chat.py.
Where is from_messages() defined?
from_messages() is defined in libs/core/langchain_core/prompts/chat.py at line 1119.
What calls from_messages()?
from_messages() is called by 4 function(s): __add__, __getitem__, from_template, partial.

Analyze Your Own Codebase

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

Try Supermodel Free