Skip to content

Commit 78bc3f0

Browse files
authored
Allow extra args to be passed to the mongodb adapter (#2423)
1 parent e956807 commit 78bc3f0

File tree

3 files changed

+129
-3
lines changed

3 files changed

+129
-3
lines changed

chatterbot/storage/mongodb.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,21 @@ class MongoDatabaseAdapter(StorageAdapter):
1313
`MongoDB connection string <https://docs.mongodb.com/manual/reference/connection-string/>`_
1414
:type database_uri: str
1515
16+
:keyword mongodb_client_kwargs: Additional keyword arguments to pass to the MongoClient constructor.
17+
This can include SSL/TLS settings, authentication options, and other
18+
PyMongo client configuration parameters.
19+
:type mongodb_client_kwargs: dict
20+
1621
.. code-block:: python
1722
23+
# Basic connection
1824
database_uri='mongodb://example.com:8100/'
25+
26+
# Connection with SSL/TLS (e.g., Amazon DocumentDB)
27+
database_uri='mongodb://USER:PASSWORD@my.cluster.us-west-x.docdb.amazonaws.com:27017/?ssl=true&replicaSet=rs0',
28+
mongodb_client_kwargs={
29+
'tlsCAFile': 'path/to/rds-combined-ca-bundle.pem'
30+
}
1931
"""
2032

2133
def __init__(self, **kwargs):
@@ -27,8 +39,11 @@ def __init__(self, **kwargs):
2739
'database_uri', 'mongodb://localhost:27017/chatterbot-database'
2840
)
2941

30-
# Use the default host and port
31-
self.client = MongoClient(self.database_uri)
42+
# Extract additional MongoClient parameters
43+
mongodb_client_kwargs = kwargs.get('mongodb_client_kwargs', {})
44+
45+
# Use the default host and port with additional client parameters
46+
self.client = MongoClient(self.database_uri, **mongodb_client_kwargs)
3247

3348
# Increase the sort buffer to 42M if possible
3449
try:

docs/storage/mongodb.rst

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,93 @@ To start the MongoDB container, run:
4242

4343
For more information on Docker and ``docker compose``, see the `Docker Compose documentation`_.
4444

45+
Using MongoDB with SSL/TLS
46+
--------------------------
47+
48+
For secure connections to remote MongoDB instances (such as Amazon DocumentDB, MongoDB Atlas, or production deployments), you can use SSL/TLS certificates.
49+
50+
Amazon DocumentDB Example
51+
~~~~~~~~~~~~~~~~~~~~~~~~~
52+
53+
Amazon DocumentDB requires SSL/TLS connections with a certificate file. Here's how to configure ChatterBot:
54+
55+
1. Download the Amazon RDS CA certificate bundle:
56+
57+
.. code-block:: bash
58+
59+
wget https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem
60+
61+
2. Configure ChatterBot to use the certificate:
62+
63+
.. code-block:: python
64+
65+
from chatterbot import ChatBot
66+
67+
bot = ChatBot(
68+
'MyBot',
69+
storage_adapter='chatterbot.storage.MongoDatabaseAdapter',
70+
database_uri='mongodb://USERNAME:PASSWORD@my-cluster.us-east-1.docdb.amazonaws.com:27017/?ssl=true&replicaSet=rs0&readPreference=secondaryPreferred',
71+
mongodb_client_kwargs={
72+
'tlsCAFile': 'global-bundle.pem' # Path to your certificate file
73+
}
74+
)
75+
76+
MongoDB Atlas Example
77+
~~~~~~~~~~~~~~~~~~~~~
78+
79+
For MongoDB Atlas with SSL/TLS:
80+
81+
.. code-block:: python
82+
83+
from chatterbot import ChatBot
84+
85+
bot = ChatBot(
86+
'MyBot',
87+
storage_adapter='chatterbot.storage.MongoDatabaseAdapter',
88+
database_uri='mongodb+srv://USERNAME:PASSWORD@cluster.mongodb.net/?retryWrites=true&w=majority',
89+
mongodb_client_kwargs={
90+
'tls': True,
91+
'tlsAllowInvalidCertificates': False # Use True only for testing
92+
}
93+
)
94+
95+
Self-Signed Certificates
96+
~~~~~~~~~~~~~~~~~~~~~~~~
97+
98+
If you're using self-signed certificates:
99+
100+
.. code-block:: python
101+
102+
from chatterbot import ChatBot
103+
104+
bot = ChatBot(
105+
'MyBot',
106+
storage_adapter='chatterbot.storage.MongoDatabaseAdapter',
107+
database_uri='mongodb://localhost:27017/chatterbot-database?ssl=true',
108+
mongodb_client_kwargs={
109+
'tlsCAFile': '/path/to/ca.pem',
110+
'tlsCertificateKeyFile': '/path/to/client.pem',
111+
'tlsAllowInvalidCertificates': False
112+
}
113+
)
114+
115+
Additional MongoDB Client Options
116+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
117+
118+
The ``mongodb_client_kwargs`` parameter accepts any valid PyMongo MongoClient options, including:
119+
120+
- ``tlsCAFile``: Path to CA certificate file
121+
- ``tlsCertificateKeyFile``: Path to client certificate file
122+
- ``tls``: Enable/disable TLS
123+
- ``tlsAllowInvalidCertificates``: Allow invalid certificates (not recommended for production)
124+
- ``serverSelectionTimeoutMS``: Timeout for server selection
125+
- ``connectTimeoutMS``: Connection timeout
126+
- ``socketTimeoutMS``: Socket timeout
127+
- ``maxPoolSize``: Maximum connection pool size
128+
- ``minPoolSize``: Minimum connection pool size
129+
130+
For a complete list of options, see the `PyMongo MongoClient documentation`_.
131+
45132
MongoDB Adapter Class Attributes
46133
--------------------------------
47134

@@ -50,3 +137,4 @@ MongoDB Adapter Class Attributes
50137

51138
.. _pymongo: https://pypi.org/project/pymongo/
52139
.. _Docker Compose documentation: https://docs.docker.com/compose/
140+
.. _PyMongo MongoClient documentation: https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html

tests/storage/test_mongo_adapter.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ def setUpClass(cls):
1515

1616
cls.has_mongo_connection = False
1717

18+
cls.database_uri = 'mongodb://localhost:27017/chatterbot_test_database'
19+
1820
try:
1921
client = MongoClient(
2022
serverSelectionTimeoutMS=0.1
2123
)
2224
client.server_info()
2325

2426
cls.adapter = MongoDatabaseAdapter(
25-
database_uri='mongodb://localhost:27017/chatterbot_test_database',
27+
database_uri=cls.database_uri,
2628
raise_on_missing_search_text=False
2729
)
2830

@@ -72,6 +74,27 @@ def test_count_returns_value(self):
7274
self.adapter.create(text="Test statement")
7375
self.assertEqual(self.adapter.count(), 1)
7476

77+
def test_mongodb_client_kwargs_parameter(self):
78+
"""
79+
Test that the adapter accepts mongodb_client_kwargs parameter.
80+
This enables SSL/TLS connections to services like Amazon DocumentDB.
81+
"""
82+
adapter = MongoDatabaseAdapter(
83+
database_uri=self.database_uri,
84+
mongodb_client_kwargs={
85+
'serverSelectionTimeoutMS': 100,
86+
'connectTimeoutMS': 100
87+
},
88+
raise_on_missing_search_text=False
89+
)
90+
91+
# Verify the adapter was created successfully
92+
self.assertIsNotNone(adapter)
93+
self.assertIsNotNone(adapter.client)
94+
95+
# Clean up
96+
adapter.close()
97+
7598
def test_filter_text_statement_not_found(self):
7699
"""
77100
Test that None is returned by the find method

0 commit comments

Comments
 (0)