mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-07 12:06:43 +00:00
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:
parent
67b6e6c2e3
commit
820da64983
@ -68,6 +68,7 @@ class MongoDBChatMessageHistory(BaseChatMessageHistory):
|
|||||||
session_id_key: str = DEFAULT_SESSION_ID_KEY,
|
session_id_key: str = DEFAULT_SESSION_ID_KEY,
|
||||||
history_key: str = DEFAULT_HISTORY_KEY,
|
history_key: str = DEFAULT_HISTORY_KEY,
|
||||||
create_index: bool = True,
|
create_index: bool = True,
|
||||||
|
history_size: Optional[int] = None,
|
||||||
index_kwargs: Optional[Dict] = None,
|
index_kwargs: Optional[Dict] = None,
|
||||||
):
|
):
|
||||||
"""Initialize with a MongoDBChatMessageHistory instance.
|
"""Initialize with a MongoDBChatMessageHistory instance.
|
||||||
@ -88,6 +89,8 @@ class MongoDBChatMessageHistory(BaseChatMessageHistory):
|
|||||||
name of the field that stores the chat history.
|
name of the field that stores the chat history.
|
||||||
create_index: Optional[bool]
|
create_index: Optional[bool]
|
||||||
whether to create an index on the session id field.
|
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]
|
index_kwargs: Optional[Dict]
|
||||||
additional keyword arguments to pass to the index creation.
|
additional keyword arguments to pass to the index creation.
|
||||||
"""
|
"""
|
||||||
@ -97,6 +100,7 @@ class MongoDBChatMessageHistory(BaseChatMessageHistory):
|
|||||||
self.collection_name = collection_name
|
self.collection_name = collection_name
|
||||||
self.session_id_key = session_id_key
|
self.session_id_key = session_id_key
|
||||||
self.history_key = history_key
|
self.history_key = history_key
|
||||||
|
self.history_size = history_size
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.client: MongoClient = MongoClient(connection_string)
|
self.client: MongoClient = MongoClient(connection_string)
|
||||||
@ -114,7 +118,15 @@ class MongoDBChatMessageHistory(BaseChatMessageHistory):
|
|||||||
def messages(self) -> List[BaseMessage]: # type: ignore
|
def messages(self) -> List[BaseMessage]: # type: ignore
|
||||||
"""Retrieve the messages from MongoDB"""
|
"""Retrieve the messages from MongoDB"""
|
||||||
try:
|
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:
|
except errors.OperationFailure as error:
|
||||||
logger.error(error)
|
logger.error(error)
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ class PatchedMongoDBChatMessageHistory(MongoDBChatMessageHistory):
|
|||||||
self.collection = MockCollection()
|
self.collection = MockCollection()
|
||||||
self.session_id_key = "SessionId"
|
self.session_id_key = "SessionId"
|
||||||
self.history_key = "History"
|
self.history_key = "History"
|
||||||
|
self.history_size = None
|
||||||
|
|
||||||
|
|
||||||
def test_memory_with_message_store() -> None:
|
def test_memory_with_message_store() -> None:
|
||||||
|
Loading…
Reference in New Issue
Block a user