Home / File/ chat_history.py — langchain Source File

chat_history.py — langchain Source File

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

File python CoreAbstractions MessageSchema 6 imports 1 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  9b5035cb_14dd_a873_4d62_c9f628c714f0["chat_history.py"]
  cccbe73e_4644_7211_4d55_e8fb133a8014["abc"]
  9b5035cb_14dd_a873_4d62_c9f628c714f0 --> cccbe73e_4644_7211_4d55_e8fb133a8014
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  9b5035cb_14dd_a873_4d62_c9f628c714f0 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  9b5035cb_14dd_a873_4d62_c9f628c714f0 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  9b5035cb_14dd_a873_4d62_c9f628c714f0 --> d758344f_537f_649e_f467_b9d7442e86df
  2971f9da_6393_a3e3_610e_ace3d35ee978["langchain_core.runnables.config"]
  9b5035cb_14dd_a873_4d62_c9f628c714f0 --> 2971f9da_6393_a3e3_610e_ace3d35ee978
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  9b5035cb_14dd_a873_4d62_c9f628c714f0 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  style 9b5035cb_14dd_a873_4d62_c9f628c714f0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Chat message history stores a history of the message interactions in a chat."""

from __future__ import annotations

from abc import ABC, abstractmethod
from typing import TYPE_CHECKING

from pydantic import BaseModel, Field

from langchain_core.messages import (
    AIMessage,
    BaseMessage,
    HumanMessage,
    get_buffer_string,
)
from langchain_core.runnables.config import run_in_executor

if TYPE_CHECKING:
    from collections.abc import Sequence


class BaseChatMessageHistory(ABC):
    """Abstract base class for storing chat message history.

    Implementations guidelines:

    Implementations are expected to over-ride all or some of the following methods:

    * `add_messages`: sync variant for bulk addition of messages
    * `aadd_messages`: async variant for bulk addition of messages
    * `messages`: sync variant for getting messages
    * `aget_messages`: async variant for getting messages
    * `clear`: sync variant for clearing messages
    * `aclear`: async variant for clearing messages

    `add_messages` contains a default implementation that calls `add_message`
    for each message in the sequence. This is provided for backwards compatibility
    with existing implementations which only had `add_message`.

    Async variants all have default implementations that call the sync variants.
    Implementers can choose to override the async implementations to provide
    truly async implementations.

    Usage guidelines:

    When used for updating history, users should favor usage of `add_messages`
    over `add_message` or other variants like `add_user_message` and `add_ai_message`
    to avoid unnecessary round-trips to the underlying persistence layer.

    Example:
        ```python
        import json
        import os
        from langchain_core.messages import messages_from_dict, message_to_dict


        class FileChatMessageHistory(BaseChatMessageHistory):
            storage_path: str
            session_id: str

// ... (187 more lines)

Subdomains

Functions

Dependencies

  • abc
  • collections.abc
  • langchain_core.messages
  • langchain_core.runnables.config
  • pydantic
  • typing

Frequently Asked Questions

What does chat_history.py do?
chat_history.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What functions are defined in chat_history.py?
chat_history.py defines 1 function(s): collections.
What does chat_history.py depend on?
chat_history.py imports 6 module(s): abc, collections.abc, langchain_core.messages, langchain_core.runnables.config, pydantic, typing.
Where is chat_history.py in the architecture?
chat_history.py is located at libs/core/langchain_core/chat_history.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/core/langchain_core).

Analyze Your Own Codebase

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

Try Supermodel Free