community[patch]: Mongo index creation (#17748)

- [ ] Title: Mongodb: MongoDB connection performance improvement. 
- [ ] Message: 
- **Description:** I made collection index_creation as optional. Index
Creation is one time process.
- **Issue:** MongoDBChatMessageHistory class object is attempting to
create an index during connection, causing each request to take longer
than usual. This should be optional with a parameter.
    - **Dependencies:** N/A
    - **Branch to be checked:** origin/mongo_index_creation

---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
Venkatesan 2024-03-09 06:13:17 +05:30 committed by GitHub
parent 5b5b37a999
commit 7a18b63dbf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -30,6 +30,8 @@ class MongoDBChatMessageHistory(BaseChatMessageHistory):
of a single chat session.
database_name: name of the database to use
collection_name: name of the collection to use
create_index: whether to create an index with name SessionId. Set to False if
such an index already exists.
"""
def __init__(
@ -38,6 +40,7 @@ class MongoDBChatMessageHistory(BaseChatMessageHistory):
session_id: str,
database_name: str = DEFAULT_DBNAME,
collection_name: str = DEFAULT_COLLECTION_NAME,
create_index: bool = True,
):
from pymongo import MongoClient, errors
@ -53,7 +56,8 @@ class MongoDBChatMessageHistory(BaseChatMessageHistory):
self.db = self.client[database_name]
self.collection = self.db[collection_name]
self.collection.create_index("SessionId")
if create_index:
self.collection.create_index("SessionId")
@property
def messages(self) -> List[BaseMessage]: # type: ignore