2024-03-05 18:53:02 +00:00
|
|
|
import json
|
|
|
|
import logging
|
2024-06-24 19:42:56 +00:00
|
|
|
from typing import Dict, List, Optional
|
2024-03-05 18:53:02 +00:00
|
|
|
|
|
|
|
from langchain_core.chat_history import BaseChatMessageHistory
|
|
|
|
from langchain_core.messages import (
|
|
|
|
BaseMessage,
|
|
|
|
message_to_dict,
|
|
|
|
messages_from_dict,
|
|
|
|
)
|
|
|
|
from pymongo import MongoClient, errors
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEFAULT_DBNAME = "chat_history"
|
|
|
|
DEFAULT_COLLECTION_NAME = "message_store"
|
2024-06-24 19:42:56 +00:00
|
|
|
DEFAULT_SESSION_ID_KEY = "SessionId"
|
|
|
|
DEFAULT_HISTORY_KEY = "History"
|
2024-03-05 18:53:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MongoDBChatMessageHistory(BaseChatMessageHistory):
|
|
|
|
"""Chat message history that stores history in MongoDB.
|
|
|
|
|
2024-07-24 14:12:44 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
# 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
|
2024-03-05 18:53:02 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
connection_string: str,
|
|
|
|
session_id: str,
|
|
|
|
database_name: str = DEFAULT_DBNAME,
|
|
|
|
collection_name: str = DEFAULT_COLLECTION_NAME,
|
2024-06-24 19:42:56 +00:00
|
|
|
*,
|
|
|
|
session_id_key: str = DEFAULT_SESSION_ID_KEY,
|
|
|
|
history_key: str = DEFAULT_HISTORY_KEY,
|
|
|
|
create_index: bool = True,
|
|
|
|
index_kwargs: Optional[Dict] = None,
|
2024-03-05 18:53:02 +00:00
|
|
|
):
|
2024-07-24 14:12:44 +00:00
|
|
|
"""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.
|
|
|
|
"""
|
2024-03-05 18:53:02 +00:00
|
|
|
self.connection_string = connection_string
|
|
|
|
self.session_id = session_id
|
|
|
|
self.database_name = database_name
|
|
|
|
self.collection_name = collection_name
|
2024-06-24 19:42:56 +00:00
|
|
|
self.session_id_key = session_id_key
|
|
|
|
self.history_key = history_key
|
2024-03-05 18:53:02 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.client: MongoClient = MongoClient(connection_string)
|
|
|
|
except errors.ConnectionFailure as error:
|
|
|
|
logger.error(error)
|
|
|
|
|
|
|
|
self.db = self.client[database_name]
|
|
|
|
self.collection = self.db[collection_name]
|
2024-06-24 19:42:56 +00:00
|
|
|
|
|
|
|
if create_index:
|
|
|
|
index_kwargs = index_kwargs or {}
|
|
|
|
self.collection.create_index(self.session_id_key, **index_kwargs)
|
2024-03-05 18:53:02 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def messages(self) -> List[BaseMessage]: # type: ignore
|
|
|
|
"""Retrieve the messages from MongoDB"""
|
|
|
|
try:
|
2024-06-24 19:42:56 +00:00
|
|
|
cursor = self.collection.find({self.session_id_key: self.session_id})
|
2024-03-05 18:53:02 +00:00
|
|
|
except errors.OperationFailure as error:
|
|
|
|
logger.error(error)
|
|
|
|
|
|
|
|
if cursor:
|
2024-06-24 19:42:56 +00:00
|
|
|
items = [json.loads(document[self.history_key]) for document in cursor]
|
2024-03-05 18:53:02 +00:00
|
|
|
else:
|
|
|
|
items = []
|
|
|
|
|
|
|
|
messages = messages_from_dict(items)
|
|
|
|
return messages
|
|
|
|
|
|
|
|
def add_message(self, message: BaseMessage) -> None:
|
|
|
|
"""Append the message to the record in MongoDB"""
|
|
|
|
try:
|
|
|
|
self.collection.insert_one(
|
|
|
|
{
|
2024-06-24 19:42:56 +00:00
|
|
|
self.session_id_key: self.session_id,
|
|
|
|
self.history_key: json.dumps(message_to_dict(message)),
|
2024-03-05 18:53:02 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
except errors.WriteError as err:
|
|
|
|
logger.error(err)
|
|
|
|
|
|
|
|
def clear(self) -> None:
|
|
|
|
"""Clear session memory from MongoDB"""
|
|
|
|
try:
|
2024-06-24 19:42:56 +00:00
|
|
|
self.collection.delete_many({self.session_id_key: self.session_id})
|
2024-03-05 18:53:02 +00:00
|
|
|
except errors.WriteError as err:
|
|
|
|
logger.error(err)
|