Skip to content

mongodb ¤

This module contains the MongoDBMessageHistory class, which is used to store chat message history in a MongoDB database.

Classes:

Name Description
MongoDBMessageHistory

Chat message history that stores history in MongoDB.

Attributes:

Name Type Description
DEFAULT_COLLECTION_NAME

A collection name for the MongoDB database.

DEFAULT_CONNECTION_STRING

A connection string for a MongoDB database.

DEFAULT_DBNAME

A database name for the MongoDB database.

DEFAULT_COLLECTION_NAME module-attribute ¤

DEFAULT_COLLECTION_NAME = 'message_store'

A collection name for the MongoDB database.

DEFAULT_CONNECTION_STRING module-attribute ¤

DEFAULT_CONNECTION_STRING = 'mongodb://localhost:27017'

A connection string for a MongoDB database.

DEFAULT_DBNAME module-attribute ¤

DEFAULT_DBNAME = 'chat_history'

A database name for the MongoDB database.

MongoDBMessageHistory ¤

MongoDBMessageHistory(
    session_id: str,
    connection_string: str = DEFAULT_CONNECTION_STRING,
    database_name: str = DEFAULT_DBNAME,
    collection_name: str = DEFAULT_COLLECTION_NAME,
)

Bases: BaseChatMessageHistory

Chat message history that stores history in MongoDB.

Parameters:

Name Type Description Default
connection_string str

connection string to connect to MongoDB

DEFAULT_CONNECTION_STRING
session_id str

Arbitrary key that is used to store the messages for a single chat session.

required
database_name str

name of the database to use

DEFAULT_DBNAME
collection_name str

name of the collection to use

DEFAULT_COLLECTION_NAME

Methods:

Name Description
add_message

Append the message to the record in MongoDB

clear

Clear session memory from MongoDB

Attributes:

Name Type Description
history List[Message]

Retrieve the messages from MongoDB

Source code in src/declarai/memory/mongodb.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def __init__(
    self,
    session_id: str,
    connection_string: str = DEFAULT_CONNECTION_STRING,
    database_name: str = DEFAULT_DBNAME,
    collection_name: str = DEFAULT_COLLECTION_NAME,
):
    try:
        from pymongo import MongoClient
    except ImportError:
        raise ImportError(
            "Could not import pymongo python package. "
            "Please install it with `pip install pymongo`."
        )

    self.connection_string = connection_string
    self.session_id = session_id
    self.database_name = database_name
    self.collection_name = collection_name

    self.client: MongoClient = MongoClient(connection_string)
    self.db = self.client[database_name]
    self.collection = self.db[collection_name]
    self.collection.create_index("SessionId")

history property ¤

history: List[Message]

Retrieve the messages from MongoDB

add_message ¤

add_message(message: Message) -> None

Append the message to the record in MongoDB

Source code in src/declarai/memory/mongodb.py
71
72
73
74
75
76
77
78
def add_message(self, message: Message) -> None:
    """Append the message to the record in MongoDB"""
    self.collection.insert_one(
        {
            "SessionId": self.session_id,
            "History": json.dumps(message.dict()),
        }
    )

clear ¤

clear() -> None

Clear session memory from MongoDB

Source code in src/declarai/memory/mongodb.py
80
81
82
def clear(self) -> None:
    """Clear session memory from MongoDB"""
    self.collection.delete_many({"SessionId": self.session_id})