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.

exception EmptyDatabaseException(message=None)[source]
count()[source]

Return the number of entries in the database.

create(**kwargs)[source]

Creates a new statement matching the keyword arguments specified. Returns the created statement.

create_many(statements)[source]

Creates multiple statement entries.

drop()[source]

Drop the database attached to a given adapter.

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 None
  • exclude_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 None
  • search_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.

get_random()[source]

Returns a random statement from the database.

remove(statement_text)[source]

Removes the statement that matches the input text. Removes any responses from statements where the response text matches the input text.

update(statement)[source]

Modifies an entry in the database. Creates an entry if one does not exist.

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.
count()[source]

Return the number of entries in the database.

create(**kwargs)[source]

Creates a new statement matching the keyword arguments specified. Returns the created statement.

create_database()[source]

Populate the database with the tables.

create_many(statements)[source]

Creates multiple statement entries.

drop()[source]

Drop the database.

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.

get_random()[source]

Returns a random statement from the database.

get_statement_model()[source]

Return the statement model.

get_tag_model()[source]

Return the conversation model.

remove(statement_text)[source]

Removes the statement that matches the input text. Removes any responses from statements where the response text matches the input text.

update(statement)[source]

Modifies an entry in the database. Creates an entry if one does not exist.

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/'
count()[source]

Return the number of entries in the database.

create(**kwargs)[source]

Creates a new statement matching the keyword arguments specified. Returns the created statement.

create_many(statements)[source]

Creates multiple statement entries.

drop()[source]

Remove the database.

filter(**kwargs)[source]

Returns a list of statements in the database that match the parameters specified.

get_random()[source]

Returns a random statement from the database

get_statement_model()[source]

Return the class for the statement model.

mongo_to_object(statement_data)[source]

Return Statement object when given data returned from Mongo DB.

remove(statement_text)[source]

Removes the statement that matches the input text.

update(statement)[source]

Modifies an entry in the database. Creates an entry if one does not exist.

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.