Home / File/ chat.py — langchain Source File

chat.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  c5f8bcad_6839_d8ea_4196_ccf4767a509d["chat.py"]
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  c5f8bcad_6839_d8ea_4196_ccf4767a509d --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  c5f8bcad_6839_d8ea_4196_ccf4767a509d --> 91721f45_4909_e489_8c1f_084f8bd87145
  a1369c93_b21f_2edb_d15c_ec3e09ac1e42["langchain_core.messages.base"]
  c5f8bcad_6839_d8ea_4196_ccf4767a509d --> a1369c93_b21f_2edb_d15c_ec3e09ac1e42
  053c6d65_7a74_9819_7c2a_c7357c95d2b8["langchain_core.utils._merge"]
  c5f8bcad_6839_d8ea_4196_ccf4767a509d --> 053c6d65_7a74_9819_7c2a_c7357c95d2b8
  style c5f8bcad_6839_d8ea_4196_ccf4767a509d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Chat Message."""

from typing import Any, Literal

from typing_extensions import override

from langchain_core.messages.base import (
    BaseMessage,
    BaseMessageChunk,
    merge_content,
)
from langchain_core.utils._merge import merge_dicts


class ChatMessage(BaseMessage):
    """Message that can be assigned an arbitrary speaker (i.e. role)."""

    role: str
    """The speaker / role of the Message."""

    type: Literal["chat"] = "chat"
    """The type of the message (used during serialization)."""


class ChatMessageChunk(ChatMessage, BaseMessageChunk):
    """Chat Message chunk."""

    # Ignoring mypy re-assignment here since we're overriding the value
    # to make sure that the chunk variant can be discriminated from the
    # non-chunk variant.
    type: Literal["ChatMessageChunk"] = "ChatMessageChunk"  # type: ignore[assignment]
    """The type of the message (used during serialization)."""

    @override
    def __add__(self, other: Any) -> BaseMessageChunk:  # type: ignore[override]
        if isinstance(other, ChatMessageChunk):
            if self.role != other.role:
                msg = "Cannot concatenate ChatMessageChunks with different roles."
                raise ValueError(msg)

            return self.__class__(
                role=self.role,
                content=merge_content(self.content, other.content),
                additional_kwargs=merge_dicts(
                    self.additional_kwargs, other.additional_kwargs
                ),
                response_metadata=merge_dicts(
                    self.response_metadata, other.response_metadata
                ),
                id=self.id,
            )
        if isinstance(other, BaseMessageChunk):
            return self.__class__(
                role=self.role,
                content=merge_content(self.content, other.content),
                additional_kwargs=merge_dicts(
                    self.additional_kwargs, other.additional_kwargs
                ),
                response_metadata=merge_dicts(
                    self.response_metadata, other.response_metadata
                ),
                id=self.id,
            )
        return super().__add__(other)

Subdomains

Dependencies

  • langchain_core.messages.base
  • langchain_core.utils._merge
  • typing
  • typing_extensions

Frequently Asked Questions

What does chat.py do?
chat.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What does chat.py depend on?
chat.py imports 4 module(s): langchain_core.messages.base, langchain_core.utils._merge, typing, typing_extensions.
Where is chat.py in the architecture?
chat.py is located at libs/core/langchain_core/messages/chat.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/core/langchain_core/messages).

Analyze Your Own Codebase

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

Try Supermodel Free