Skip to content

Commit

Permalink
community[patch]: Mongo index creation (#17748)
Browse files Browse the repository at this point in the history
- [ ] 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 <[email protected]>
  • Loading branch information
2 people authored and hinthornw committed Apr 26, 2024
1 parent 09abdcc commit 00e999f
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand All @@ -38,6 +40,7 @@ def __init__(
session_id: str,
database_name: str = DEFAULT_DBNAME,
collection_name: str = DEFAULT_COLLECTION_NAME,
create_index: bool = True,
):
from pymongo import MongoClient, errors

Expand All @@ -53,7 +56,8 @@ def __init__(

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
Expand Down

0 comments on commit 00e999f

Please sign in to comment.