From c112d7334d6cac3296b877250d3f575fbfd46da2 Mon Sep 17 00:00:00 2001 From: Lucas Rodrigues <104941391+ldesousa-stf@users.noreply.github.com> Date: Sun, 4 Jun 2023 17:39:56 -0400 Subject: [PATCH] Update MongoDBChatMessageHistory to create an index on SessionId (#5632) All the queries to the database are done based on the SessionId property, this will optimize how Mongo retrieves all messages from a session #### Who can review? Tag maintainers/contributors who might be interested: @dev2049 --- langchain/memory/chat_message_histories/mongodb.py | 1 + 1 file changed, 1 insertion(+) diff --git a/langchain/memory/chat_message_histories/mongodb.py b/langchain/memory/chat_message_histories/mongodb.py index 3455d812..a6965af3 100644 --- a/langchain/memory/chat_message_histories/mongodb.py +++ b/langchain/memory/chat_message_histories/mongodb.py @@ -47,6 +47,7 @@ class MongoDBChatMessageHistory(BaseChatMessageHistory): self.db = self.client[database_name] self.collection = self.db[collection_name] + self.collection.create_index("SessionId") @property def messages(self) -> List[BaseMessage]: # type: ignore