mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-09 21:08:59 +00:00
docs: Add MongoDBChatMessageHistory docstrings (#24608)
- **Description:** Add MongoDBChatMessageHistory rich docstrings. - **Issue:** the issue #21983
This commit is contained in:
parent
12c3454fd9
commit
22175738ac
@ -21,17 +21,42 @@ DEFAULT_HISTORY_KEY = "History"
|
|||||||
class MongoDBChatMessageHistory(BaseChatMessageHistory):
|
class MongoDBChatMessageHistory(BaseChatMessageHistory):
|
||||||
"""Chat message history that stores history in MongoDB.
|
"""Chat message history that stores history in MongoDB.
|
||||||
|
|
||||||
Args:
|
Setup:
|
||||||
connection_string: connection string to connect to MongoDB
|
Install ``langchain-mongodb`` python package.
|
||||||
session_id: arbitrary key that is used to store the messages
|
|
||||||
of a single chat session.
|
.. code-block:: bash
|
||||||
database_name: name of the database to use
|
|
||||||
collection_name: name of the collection to use
|
pip install langchain-mongodb
|
||||||
session_id_key: name of the field that stores the session id
|
|
||||||
history_key: name of the field that stores the chat history
|
Instantiate:
|
||||||
create_index: whether to create an index on the session id field
|
.. code-block:: python
|
||||||
index_kwargs: additional keyword arguments to pass to the index creation
|
|
||||||
"""
|
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
|
||||||
|
|
||||||
|
# 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
|
||||||
|
""" # noqa: E501
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@ -45,6 +70,27 @@ class MongoDBChatMessageHistory(BaseChatMessageHistory):
|
|||||||
create_index: bool = True,
|
create_index: bool = True,
|
||||||
index_kwargs: Optional[Dict] = None,
|
index_kwargs: Optional[Dict] = None,
|
||||||
):
|
):
|
||||||
|
"""Initialize with a MongoDBChatMessageHistory instance.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
connection_string: str
|
||||||
|
connection string to connect to MongoDB.
|
||||||
|
session_id: str
|
||||||
|
arbitrary key that is used to store the messages of
|
||||||
|
a single chat session.
|
||||||
|
database_name: Optional[str]
|
||||||
|
name of the database to use.
|
||||||
|
collection_name: Optional[str]
|
||||||
|
name of the collection to use.
|
||||||
|
session_id_key: Optional[str]
|
||||||
|
name of the field that stores the session id.
|
||||||
|
history_key: Optional[str]
|
||||||
|
name of the field that stores the chat history.
|
||||||
|
create_index: Optional[bool]
|
||||||
|
whether to create an index on the session id field.
|
||||||
|
index_kwargs: Optional[Dict]
|
||||||
|
additional keyword arguments to pass to the index creation.
|
||||||
|
"""
|
||||||
self.connection_string = connection_string
|
self.connection_string = connection_string
|
||||||
self.session_id = session_id
|
self.session_id = session_id
|
||||||
self.database_name = database_name
|
self.database_name = database_name
|
||||||
|
Loading…
Reference in New Issue
Block a user