Storage Adapters

Storage adapters provide an interface that allows ChatterBot to connect to different storage backends.

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"
)

SQL Storage Adapter

class chatterbot.storage.SQLStorageAdapter(**kwargs)[source]

SQLStorageAdapter allows ChatterBot to store conversation data semi-structured T-SQL database, virtually, any database that SQL Alchemy supports.

Notes:
Tables may change (and will), so, save your training data. There is no data migration (yet). Performance test not done yet. Tests using other databases not finished.

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 (str) – Used for sqlite database. Ignored if database_uri is specified.
  • database_uri (str) – eg: sqlite:///database_test.db”, use database_uri or database, database_uri can be specified to choose database driver (database parameter will be ignored).
  • read_only (bool) – False by default, makes all operations read only, has priority over all DB operations so, create, update, delete will NOT be executed
add_to_conversation(conversation_id, statement, response)[source]

Add the statement and response to the conversation.

count()[source]

Return the number of entries in the database.

create()[source]

Populate the database with the tables.

create_conversation()[source]

Create a new conversation.

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.

find(statement_text)[source]

Returns a statement if it exists otherwise None

get_conversation_model()[source]

Return the conversation model.

get_latest_response(conversation_id)[source]

Returns the latest response in a conversation if it exists. Returns None if a matching conversation cannot be found.

get_random()[source]

Returns a random statement from the database

get_response_model()[source]

Return the response model.

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

class chatterbot.storage.MongoDatabaseAdapter(**kwargs)[source]

The MongoDatabaseAdapter is an interface that allows ChatterBot to store statements in a MongoDB database.

Parameters:database (str) – The name of the database you wish to connect to.
database='chatterbot-database'
Parameters:database_uri (str) – The URI of a remote instance of MongoDB.
database_uri='mongodb://example.com:8100/'
add_to_conversation(conversation_id, statement, response)[source]

Add the statement and response to the conversation.

create_conversation()[source]

Create a new conversation.

deserialize_responses(response_list)[source]

Takes the list of response items and returns the list converted to Response objects.

drop()[source]

Remove the database.

filter(**kwargs)[source]

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

get_latest_response(conversation_id)[source]

Returns the latest response in a conversation if it exists. Returns None if a matching conversation cannot be found.

get_random()[source]

Returns a random statement from the database

get_response_model()[source]

Return the class for the response model.

get_response_statements()[source]

Return only statements that are in response to another statement. A statement must exist which lists the closest matching statement in the in_response_to field. Otherwise, the logic adapter may find a closest matching statement that does not have a known response.

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. Removes any responses from statements if the response text matches the input text.