Storage Adapters¶
Storage adapters provide an interface that allows ChatterBot to connect to different storage technologies.
The storage adapter that your bot uses can be specified by setting
the storage_adapter
parameter to the import path of the
storage adapter you want to use.
chatbot = ChatBot(
"My ChatterBot",
storage_adapter="chatterbot.storage.SQLStorageAdapter"
)
Common storage adapter attributes¶
Each storage adapter inherits the following attributes and methods.
- class chatterbot.storage.StorageAdapter(*args, **kwargs)[source]¶
This is an abstract class that represents the interface that all storage adapters should implement.
- exception AdapterMethodNotImplementedError[source]¶
An exception to be raised when a storage adapter method has not been implemented. Typically this indicates that the method should be implement in a subclass.
- create(**kwargs)[source]¶
Creates a new statement matching the keyword arguments specified. Returns the created statement.
- filter(**kwargs)[source]¶
Returns a list of objects from the database. The kwargs parameter can contain any number of attributes. Only objects which contain all listed attributes and in which all values match for all listed attributes will be returned.
- Parameters:
page_size – The maximum number of records to load into memory at once when returning results. Defaults to 1000
order_by – The field name that should be used to determine the order that results are returned in. Defaults to None
tags – A list of tags. When specified, the results will only include statements that have a tag in the provided list. Defaults to [] (empty list)
exclude_text – If the
text
of a statement is an exact match for the value of this parameter the statement will not be included in the result set. Defaults to Noneexclude_text_words – If the
text
of a statement contains a word from this list then the statement will not be included in the result set. Defaults to [] (empty list)persona_not_startswith – If the
persona
field of a statement starts with the value specified by this parameter, then the statement will not be returned in the result set. Defaults to Nonesearch_text_contains – If the
search_text
field of a statement contains a word that is in the string provided to this parameter, then the statement will be included in the result set. Defaults to None
- get_model(model_name)[source]¶
Return the model class for a given model name.
model_name is case insensitive.
- get_object(object_name)[source]¶
Return the class for a given object name.
object_name is case insensitive.
SQL Storage Adapter¶
- class chatterbot.storage.SQLStorageAdapter(**kwargs)[source]¶
The SQLStorageAdapter allows ChatterBot to store conversation data in any database supported by the SQL Alchemy ORM.
All parameters are optional, by default a sqlite database is used.
It will check if tables are present, if they are not, it will attempt to create the required tables.
- Parameters:
database_uri (str) – eg: sqlite:///database_test.sqlite3’, The database_uri can be specified to choose database driver.
- create(**kwargs)[source]¶
Creates a new statement matching the keyword arguments specified. Returns the created statement.
- filter(**kwargs)[source]¶
Returns a list of objects from the database. The kwargs parameter can contain any number of attributes. Only objects which contain all listed attributes and in which all values match for all listed attributes will be returned.
MongoDB Storage Adapter¶
Note
Before you can use this storage adapter you will need to install
pymongo. Consider adding pymongo
to your project’s
requirements.txt
file so you can keep track of your dependencies
and their versions.
- 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
database_uri='mongodb://example.com:8100/'
- 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.
Database Migrations¶
Various frameworks such as Django and SQL Alchemy support functionality that allows revisions to be made to databases programmatically. This makes it possible for updates and revisions to structures in the database to be be applied in consecutive version releases.
The following explains the included migration process for each of the databases that ChatterBot comes with support for.
Django: Full schema migrations and data migrations will be included with each release.
SQL Alchemy: No migrations are currently provided in releases. If you require migrations between versions Alembic is the recommended solution for generating them.
MongoDB: No migrations are provided.