Home / Function/ __add__() — langchain Function Reference

__add__() — langchain Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

libs/core/langchain_core/prompts/chat.py lines 1006–1044

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

        Args:
            other: Another prompt template.

        Returns:
            Combined prompt template.
        """
        partials = {**self.partial_variables}

        # Need to check that other has partial variables since it may not be
        # a ChatPromptTemplate.
        if hasattr(other, "partial_variables") and other.partial_variables:
            partials.update(other.partial_variables)

        # Allow for easy combining
        if isinstance(other, ChatPromptTemplate):
            return ChatPromptTemplate(messages=self.messages + other.messages).partial(
                **partials
            )
        if isinstance(
            other, (BaseMessagePromptTemplate, BaseMessage, BaseChatPromptTemplate)
        ):
            return ChatPromptTemplate(messages=[*self.messages, other]).partial(
                **partials
            )
        if isinstance(other, (list, tuple)):
            other_ = ChatPromptTemplate.from_messages(other)
            return ChatPromptTemplate(messages=self.messages + other_.messages).partial(
                **partials
            )
        if isinstance(other, str):
            prompt = HumanMessagePromptTemplate.from_template(other)
            return ChatPromptTemplate(messages=[*self.messages, prompt]).partial(
                **partials
            )
        msg = f"Unsupported operand type for +: {type(other)}"
        raise NotImplementedError(msg)

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free