mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-05 20:58:25 +00:00
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:
parent
5b5b37a999
commit
7a18b63dbf
@ -30,6 +30,8 @@ class MongoDBChatMessageHistory(BaseChatMessageHistory):
|
|||||||
of a single chat session.
|
of a single chat session.
|
||||||
database_name: name of the database to use
|
database_name: name of the database to use
|
||||||
collection_name: name of the collection 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__(
|
def __init__(
|
||||||
@ -38,6 +40,7 @@ class MongoDBChatMessageHistory(BaseChatMessageHistory):
|
|||||||
session_id: str,
|
session_id: str,
|
||||||
database_name: str = DEFAULT_DBNAME,
|
database_name: str = DEFAULT_DBNAME,
|
||||||
collection_name: str = DEFAULT_COLLECTION_NAME,
|
collection_name: str = DEFAULT_COLLECTION_NAME,
|
||||||
|
create_index: bool = True,
|
||||||
):
|
):
|
||||||
from pymongo import MongoClient, errors
|
from pymongo import MongoClient, errors
|
||||||
|
|
||||||
@ -53,7 +56,8 @@ class MongoDBChatMessageHistory(BaseChatMessageHistory):
|
|||||||
|
|
||||||
self.db = self.client[database_name]
|
self.db = self.client[database_name]
|
||||||
self.collection = self.db[collection_name]
|
self.collection = self.db[collection_name]
|
||||||
self.collection.create_index("SessionId")
|
if create_index:
|
||||||
|
self.collection.create_index("SessionId")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def messages(self) -> List[BaseMessage]: # type: ignore
|
def messages(self) -> List[BaseMessage]: # type: ignore
|
||||||
|
Loading…
Reference in New Issue
Block a user