MongoDB Storage Adapter¶
ChatterBot includes support for integration with MongoDB databases via its MongoDatabaseAdapter
class.
Before you can use this storage adapter you will need to install pymongo. An easy way to install it is to use the chatterbot[mongodb]
extra when installing ChatterBot. For example:
pip install chatterbot[mongodb]
You’ll also need to have a MongoDB server running. An easy way to run one locally is to use Docker:
services:
mongo:
# Use the latest stable version of the mongo image
image: mongo:8.0
# Expose the default MongoDB port
ports:
- "27017:27017"
# Persist the MongoDB data
volumes:
- ./.database/mongodb/db:/data/db
To start the MongoDB container, run:
docker compose up -d
Note
For more information on Docker and docker compose
, see the Docker Compose documentation.
Using MongoDB with SSL/TLS¶
For secure connections to remote MongoDB instances (such as Amazon DocumentDB, MongoDB Atlas, or production deployments), you can use SSL/TLS certificates.
Amazon DocumentDB Example¶
Amazon DocumentDB requires SSL/TLS connections with a certificate file. Here’s how to configure ChatterBot:
Download the Amazon RDS CA certificate bundle:
wget https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem
Configure ChatterBot to use the certificate:
from chatterbot import ChatBot
bot = ChatBot(
'MyBot',
storage_adapter='chatterbot.storage.MongoDatabaseAdapter',
database_uri='mongodb://USERNAME:[email protected]:27017/?ssl=true&replicaSet=rs0&readPreference=secondaryPreferred',
mongodb_client_kwargs={
'tlsCAFile': 'global-bundle.pem' # Path to your certificate file
}
)
MongoDB Atlas Example¶
For MongoDB Atlas with SSL/TLS:
from chatterbot import ChatBot
bot = ChatBot(
'MyBot',
storage_adapter='chatterbot.storage.MongoDatabaseAdapter',
database_uri='mongodb+srv://USERNAME:[email protected]/?retryWrites=true&w=majority',
mongodb_client_kwargs={
'tls': True,
'tlsAllowInvalidCertificates': False # Use True only for testing
}
)
Self-Signed Certificates¶
If you’re using self-signed certificates:
from chatterbot import ChatBot
bot = ChatBot(
'MyBot',
storage_adapter='chatterbot.storage.MongoDatabaseAdapter',
database_uri='mongodb://localhost:27017/chatterbot-database?ssl=true',
mongodb_client_kwargs={
'tlsCAFile': '/path/to/ca.pem',
'tlsCertificateKeyFile': '/path/to/client.pem',
'tlsAllowInvalidCertificates': False
}
)
Additional MongoDB Client Options¶
The mongodb_client_kwargs
parameter accepts any valid PyMongo MongoClient options, including:
tlsCAFile
: Path to CA certificate filetlsCertificateKeyFile
: Path to client certificate filetls
: Enable/disable TLStlsAllowInvalidCertificates
: Allow invalid certificates (not recommended for production)serverSelectionTimeoutMS
: Timeout for server selectionconnectTimeoutMS
: Connection timeoutsocketTimeoutMS
: Socket timeoutmaxPoolSize
: Maximum connection pool sizeminPoolSize
: Minimum connection pool size
For a complete list of options, see the PyMongo MongoClient documentation.
MongoDB Adapter Class Attributes¶
- class chatterbot.storage.MongoDatabaseAdapter(**kwargs)[source]¶
The MongoDatabaseAdapter is an interface that allows ChatterBot to store statements in a MongoDB database.
- Parameters:
database_uri (str) – The URI of a remote instance of MongoDB. This can be any valid MongoDB connection string
mongodb_client_kwargs (dict) – Additional keyword arguments to pass to the MongoClient constructor. This can include SSL/TLS settings, authentication options, and other PyMongo client configuration parameters.
# Basic connection database_uri='mongodb://example.com:8100/' # Connection with SSL/TLS (e.g., Amazon DocumentDB) database_uri='mongodb://USER:[email protected]:27017/?ssl=true&replicaSet=rs0', mongodb_client_kwargs={ 'tlsCAFile': 'path/to/rds-combined-ca-bundle.pem' }
- create(**kwargs)[source]¶
Creates a new statement matching the keyword arguments specified. Returns the created statement.
- filter(**kwargs)[source]¶
Returns a list of statements in the database that match the parameters specified.