Home / File/ message.py — langchain Source File

message.py — langchain Source File

Architecture documentation for message.py, a python file in the langchain codebase. 6 imports, 0 dependents.

File python PromptManagement ExampleSelection 6 imports 1 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  593c0a14_f867_7cd3_9fea_9cdefbda6270["message.py"]
  cccbe73e_4644_7211_4d55_e8fb133a8014["abc"]
  593c0a14_f867_7cd3_9fea_9cdefbda6270 --> cccbe73e_4644_7211_4d55_e8fb133a8014
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  593c0a14_f867_7cd3_9fea_9cdefbda6270 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  36cce5da_d805_04c3_7e86_e1b4dd49b497["langchain_core.load"]
  593c0a14_f867_7cd3_9fea_9cdefbda6270 --> 36cce5da_d805_04c3_7e86_e1b4dd49b497
  c98d597d_2ec1_88dc_2aec_af377c69a987["langchain_core.utils.interactive_env"]
  593c0a14_f867_7cd3_9fea_9cdefbda6270 --> c98d597d_2ec1_88dc_2aec_af377c69a987
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  593c0a14_f867_7cd3_9fea_9cdefbda6270 --> d758344f_537f_649e_f467_b9d7442e86df
  e45722a2_0136_a972_1f58_7b5987500404["langchain_core.prompts.chat"]
  593c0a14_f867_7cd3_9fea_9cdefbda6270 --> e45722a2_0136_a972_1f58_7b5987500404
  style 593c0a14_f867_7cd3_9fea_9cdefbda6270 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Message prompt templates."""

from __future__ import annotations

from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Any

from langchain_core.load import Serializable
from langchain_core.utils.interactive_env import is_interactive_env

if TYPE_CHECKING:
    from langchain_core.messages import BaseMessage
    from langchain_core.prompts.chat import ChatPromptTemplate


class BaseMessagePromptTemplate(Serializable, ABC):
    """Base class for message prompt templates."""

    @classmethod
    def is_lc_serializable(cls) -> bool:
        """Return `True` as this class is serializable."""
        return True

    @classmethod
    def get_lc_namespace(cls) -> list[str]:
        """Get the namespace of the LangChain object.

        Returns:
            `["langchain", "prompts", "chat"]`
        """
        return ["langchain", "prompts", "chat"]

    @abstractmethod
    def format_messages(self, **kwargs: Any) -> list[BaseMessage]:
        """Format messages from kwargs.

        Should return a list of `BaseMessage` objects.

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

        Returns:
            List of `BaseMessage` objects.
        """

    async def aformat_messages(self, **kwargs: Any) -> list[BaseMessage]:
        """Async format messages from kwargs.

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

        Returns:
            List of `BaseMessage` objects.
        """
        return self.format_messages(**kwargs)

    @property
    @abstractmethod
    def input_variables(self) -> list[str]:
        """Input variables for this prompt template.

        Returns:
            List of input variables.
        """

    def pretty_repr(
        self,
        html: bool = False,  # noqa: FBT001,FBT002
    ) -> str:
        """Human-readable representation.

        Args:
            html: Whether to format as HTML.

        Returns:
            Human-readable representation.
        """
        raise NotImplementedError

    def pretty_print(self) -> None:
        """Print a human-readable representation."""
        print(self.pretty_repr(html=is_interactive_env()))  # noqa: T201

    def __add__(self, other: Any) -> ChatPromptTemplate:
        """Combine two prompt templates.

        Args:
            other: Another prompt template.

        Returns:
            Combined prompt template.
        """
        # Import locally to avoid circular import.
        from langchain_core.prompts.chat import ChatPromptTemplate  # noqa: PLC0415

        prompt = ChatPromptTemplate(messages=[self])
        return prompt.__add__(other)

Subdomains

Functions

Dependencies

  • abc
  • langchain_core.load
  • langchain_core.messages
  • langchain_core.prompts.chat
  • langchain_core.utils.interactive_env
  • typing

Frequently Asked Questions

What does message.py do?
message.py is a source file in the langchain codebase, written in python. It belongs to the PromptManagement domain, ExampleSelection subdomain.
What functions are defined in message.py?
message.py defines 1 function(s): langchain_core.
What does message.py depend on?
message.py imports 6 module(s): abc, langchain_core.load, langchain_core.messages, langchain_core.prompts.chat, langchain_core.utils.interactive_env, typing.
Where is message.py in the architecture?
message.py is located at libs/core/langchain_core/prompts/message.py (domain: PromptManagement, subdomain: ExampleSelection, directory: libs/core/langchain_core/prompts).

Analyze Your Own Codebase

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

Try Supermodel Free