limit the most recent documents to fetch from MongoDB database. (#25435)

limit the most recent documents to fetch from MongoDB database.

Thank you for contributing to LangChain!

- [ ] **limit the most recent documents to fetch from MongoDB
database.**: "langchain_mongodb: limit the most recent documents to
fetch from MongoDB database."


- [ ] **PR message**: ***Delete this entire checklist*** and replace
with
- **Description:** Added a doc_limit parameter which enables the limit
for the documents to fetch from MongoDB database
    - **Issue:** 
    - **Dependencies:** None

---------

Co-authored-by: Chester Curme <chester.curme@gmail.com>
This commit is contained in:
Krishna Kulkarni 2024-08-22 20:03:45 +05:30 committed by GitHub
parent 67b6e6c2e3
commit 820da64983
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 1 deletions

View File

@ -68,6 +68,7 @@ class MongoDBChatMessageHistory(BaseChatMessageHistory):
session_id_key: str = DEFAULT_SESSION_ID_KEY,
history_key: str = DEFAULT_HISTORY_KEY,
create_index: bool = True,
history_size: Optional[int] = None,
index_kwargs: Optional[Dict] = None,
):
"""Initialize with a MongoDBChatMessageHistory instance.
@ -88,6 +89,8 @@ class MongoDBChatMessageHistory(BaseChatMessageHistory):
name of the field that stores the chat history.
create_index: Optional[bool]
whether to create an index on the session id field.
history_size: Optional[int]
count of (most recent) messages to fetch from MongoDB.
index_kwargs: Optional[Dict]
additional keyword arguments to pass to the index creation.
"""
@ -97,6 +100,7 @@ class MongoDBChatMessageHistory(BaseChatMessageHistory):
self.collection_name = collection_name
self.session_id_key = session_id_key
self.history_key = history_key
self.history_size = history_size
try:
self.client: MongoClient = MongoClient(connection_string)
@ -114,7 +118,15 @@ class MongoDBChatMessageHistory(BaseChatMessageHistory):
def messages(self) -> List[BaseMessage]: # type: ignore
"""Retrieve the messages from MongoDB"""
try:
cursor = self.collection.find({self.session_id_key: self.session_id})
if self.history_size is None:
cursor = self.collection.find({self.session_id_key: self.session_id})
else:
skip_count = max(
0, self.collection.count_documents({}) - self.history_size
)
cursor = self.collection.find(
{self.session_id_key: self.session_id}, skip=skip_count
)
except errors.OperationFailure as error:
logger.error(error)

View File

@ -16,6 +16,7 @@ class PatchedMongoDBChatMessageHistory(MongoDBChatMessageHistory):
self.collection = MockCollection()
self.session_id_key = "SessionId"
self.history_key = "History"
self.history_size = None
def test_memory_with_message_store() -> None: