Source code for langchain_community.chat_message_histories.in_memory

from typing import List, Sequence

from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.messages import BaseMessage
from langchain_core.pydantic_v1 import BaseModel, Field

[docs]class ChatMessageHistory(BaseChatMessageHistory, BaseModel): """In memory implementation of chat message history. Stores messages in an in memory list. """ messages: List[BaseMessage] = Field(default_factory=list)
[docs] async def aget_messages(self) -> List[BaseMessage]: return self.messages
[docs] def add_message(self, message: BaseMessage) -> None: """Add a self-created message to the store""" self.messages.append(message)
[docs] async def aadd_messages(self, messages: Sequence[BaseMessage]) -> None: """Add messages to the store""" self.add_messages(messages)
[docs] def clear(self) -> None: self.messages = []
[docs] async def aclear(self) -> None: self.clear()