| Name | Type | Description |
|---|---|---|
connection_string* | Optional[str] | 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 | Default: DEFAULT_DBNAMEOptional[str] name of the database to use. |
collection_name | str | Default: DEFAULT_COLLECTION_NAME |
session_id_key | str | Default: DEFAULT_SESSION_ID_KEY |
history_key | str | Default: DEFAULT_HISTORY_KEY |
create_index | bool | Default: True |
history_size | Optional[int] | Default: None |
index_kwargs | Optional[Dict] | Default: None |
client | Optional[MongoClient] | Default: None |
Chat message history that stores history in MongoDB.
Setup:
Install langchain-mongodb python package.
.. code-block:: bash
pip install langchain-mongodb
Instantiate:
.. code-block:: python
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:
.. code-block:: python
history.add_message(message)
history.add_messages([message1, message2, message3, ...])
history.add_user_message(human_message)
history.add_ai_message(ai_message)
messages = history.messages
Optional[str] name of the collection to use.
Optional[str] name of the field that stores the session id.
Optional[str] name of the field that stores the chat history.
Optional[bool] whether to create an index on the session id field.
Optional[int] count of (most recent) messages to fetch from MongoDB.
Optional[Dict] additional keyword arguments to pass to the index creation.
Optional[MongoClient] an existing MongoClient instance. If provided, connection_string is ignored.