MongoDBChatMessageHistory#

class langchain_mongodb.chat_message_histories.MongoDBChatMessageHistory(connection_string: str | None, session_id: str, database_name: str = 'chat_history', collection_name: str = 'message_store', *, session_id_key: str = 'SessionId', history_key: str = 'History', create_index: bool = True, history_size: int | None = None, index_kwargs: Dict | None = None, client: MongoClient | None = None)[source]#

Chat message history that stores history in MongoDB.

Setup:

Install langchain-mongodb python package.

pip install langchain-mongodb
Instantiate:
from langchain_mongodb import MongoDBChatMessageHistory


history = MongoDBChatMessageHistory(
    connection_string="mongodb://your-host:your-port/",  # mongodb://localhost:27017/
    session_id = "your-session-id",
)
Add and retrieve messages:
# Add single message
history.add_message(message)

# Add batch messages
history.add_messages([message1, message2, message3, ...])

# Add human message
history.add_user_message(human_message)

# Add ai message
history.add_ai_message(ai_message)

# Retrieve messages
messages = history.messages

Initialize with a MongoDBChatMessageHistory instance.

Parameters:
  • connection_string (str | None) – Optional[str] connection string to connect to MongoDB. Can be None if mongo_client is provided.

  • session_id (str) –

    str arbitrary key that is used to store the messages of

    a single chat session.

  • database_name (str) – Optional[str] name of the database to use.

  • collection_name (str) – Optional[str] name of the collection to use.

  • session_id_key (str) – Optional[str] name of the field that stores the session id.

  • history_key (str) – Optional[str] name of the field that stores the chat history.

  • create_index (bool) – Optional[bool] whether to create an index on the session id field.

  • history_size (int | None) – Optional[int] count of (most recent) messages to fetch from MongoDB.

  • index_kwargs (Dict | None) – Optional[Dict] additional keyword arguments to pass to the index creation.

  • client (MongoClient | None) – Optional[MongoClient] an existing MongoClient instance. If provided, connection_string is ignored.

Attributes

messages

Retrieve the messages from MongoDB

Methods

__init__(connection_string,Β session_id[,Β ...])

Initialize with a MongoDBChatMessageHistory instance.

aadd_messages(messages)

Async add a list of messages to MongoDB

aclear()

Async clear session memory from MongoDB

add_ai_message(message)

Convenience method for adding an AI message string to the store.

add_message(message)

Append the message to the record in MongoDB

add_messages(messages)

Add a list of messages.

add_user_message(message)

Convenience method for adding a human message string to the store.

aget_messages()

Async version of getting messages from MongoDB

clear()

Clear session memory from MongoDB

__init__(connection_string: str | None, session_id: str, database_name: str = 'chat_history', collection_name: str = 'message_store', *, session_id_key: str = 'SessionId', history_key: str = 'History', create_index: bool = True, history_size: int | None = None, index_kwargs: Dict | None = None, client: MongoClient | None = None)[source]#

Initialize with a MongoDBChatMessageHistory instance.

Parameters:
  • connection_string (str | None) – Optional[str] connection string to connect to MongoDB. Can be None if mongo_client is provided.

  • session_id (str) –

    str arbitrary key that is used to store the messages of

    a single chat session.

  • database_name (str) – Optional[str] name of the database to use.

  • collection_name (str) – Optional[str] name of the collection to use.

  • session_id_key (str) – Optional[str] name of the field that stores the session id.

  • history_key (str) – Optional[str] name of the field that stores the chat history.

  • create_index (bool) – Optional[bool] whether to create an index on the session id field.

  • history_size (int | None) – Optional[int] count of (most recent) messages to fetch from MongoDB.

  • index_kwargs (Dict | None) – Optional[Dict] additional keyword arguments to pass to the index creation.

  • client (MongoClient | None) – Optional[MongoClient] an existing MongoClient instance. If provided, connection_string is ignored.

async aadd_messages(messages: Sequence[BaseMessage]) β†’ None[source]#

Async add a list of messages to MongoDB

Parameters:

messages (Sequence[BaseMessage])

Return type:

None

async aclear() β†’ None[source]#

Async clear session memory from MongoDB

Return type:

None

add_ai_message(message: AIMessage | str) β†’ None#

Convenience method for adding an AI message string to the store.

Please note that this is a convenience method. Code should favor the bulk add_messages interface instead to save on round-trips to the underlying persistence layer.

This method may be deprecated in a future release.

Parameters:

message (AIMessage | str) – The AI message to add.

Return type:

None

add_message(message: BaseMessage) β†’ None[source]#

Append the message to the record in MongoDB

Parameters:

message (BaseMessage)

Return type:

None

add_messages(messages: Sequence[BaseMessage]) β†’ None#

Add a list of messages.

Implementations should over-ride this method to handle bulk addition of messages in an efficient manner to avoid unnecessary round-trips to the underlying store.

Parameters:

messages (Sequence[BaseMessage]) – A sequence of BaseMessage objects to store.

Return type:

None

add_user_message(message: HumanMessage | str) β†’ None#

Convenience method for adding a human message string to the store.

Please note that this is a convenience method. Code should favor the bulk add_messages interface instead to save on round-trips to the underlying persistence layer.

This method may be deprecated in a future release.

Parameters:

message (HumanMessage | str) – The human message to add to the store.

Return type:

None

async aget_messages() β†’ List[BaseMessage][source]#

Async version of getting messages from MongoDB

Return type:

List[BaseMessage]

clear() β†’ None[source]#

Clear session memory from MongoDB

Return type:

None