langchain.schema.chat_history.BaseChatMessageHistory¶
class langchain.schema.chat_history.BaseChatMessageHistory[source]¶
Abstract base class for storing chat message history.
See ChatMessageHistory for default implementation.
Example
class FileChatMessageHistory(BaseChatMessageHistory):
    storage_path:  str
    session_id: str
   @property
   def messages(self):
       with open(os.path.join(storage_path, session_id), 'r:utf-8') as f:
           messages = json.loads(f.read())
        return messages_from_dict(messages)
   def add_message(self, message: BaseMessage) -> None:
       messages = self.messages.append(_message_to_dict(message))
       with open(os.path.join(storage_path, session_id), 'w') as f:
           json.dump(f, messages)
   def clear(self):
       with open(os.path.join(storage_path, session_id), 'w') as f:
           f.write("[]")
Attributes
messages
A list of Messages stored in-memory.
Methods
__init__()
add_ai_message(message)
Convenience method for adding an AI message string to the store.
add_message(message)
Add a Message object to the store.
add_user_message(message)
Convenience method for adding a human message string to the store.
clear()
Remove all messages from the store
__init__()¶
add_ai_message(message: str) → None[source]¶
Convenience method for adding an AI message string to the store.
Parameters
message – The string contents of an AI message.
abstract add_message(message: BaseMessage) → None[source]¶
Add a Message object to the store.
Parameters
message – A BaseMessage object to store.
add_user_message(message: str) → None[source]¶
Convenience method for adding a human message string to the store.
Parameters