Creating a new logic adapter

You can write your own logic adapters by creating a new class that inherits from LogicAdapter and overrides the necessary methods established in the LogicAdapter base class.

Logic adapter methods

class chatterbot.logic.LogicAdapter(**kwargs)[source]

This is an abstract class that represents the interface that all logic adapters should implement.

  • statement_comparison_function – The dot-notated import path to a statement comparison function. Defaults to levenshtein_distance.
  • response_selection_method – The a response selection method. Defaults to get_first_response.
exception EmptyDatasetException(value='An empty set was received when at least one statement was expected.')[source]

A preliminary check that is called to determine if a logic adapter can process a given statement. By default, this method returns true but it can be overridden in child classes as needed.

Return type:bool

Return the name of the current logic adapter class. This is typically used for logging and debugging.


Return a dictionary of functions to be run once when the chat bot is instantiated.


Override this method and implement your logic for selecting a response to an input statement.

A confidence value and the selected response statement should be returned. The confidence value represents a rating of how accurate the logic adapter expects the selected response to be. Confidence scores are used to select the best response from multiple logic adapters.

The confidence value should be a number between 0 and 1 where 0 is the lowest confidence level and 1 is the highest.

Parameters:statement (Statement) – An input statement to be processed by the logic adapter.
Return type:Statement

Example logic adapter

from chatterbot.logic import LogicAdapter

class MyLogicAdapter(LogicAdapter):
    def __init__(self, **kwargs):
        super(MyLogicAdapter, self).__init__(**kwargs)

    def can_process(self, statement):
        return True

    def process(self, statement):
        import random

        # Randomly select a confidence between 0 and 1
        confidence = random.uniform(0, 1)

        # For this example, we will just return the input as output
        selected_statement = statement
        selected_statement.confidence = confidence

        return selected_statement

Directory structure

If you create your own logic adapter you will need to have it in a separate file from your chat bot. Your directory setup should look something like the following:


Then assuming that you have a class named MyLogicAdapter in your file, you should be able to specify the following when you initialize your chat bot.

    # ...
            'import_path': 'cool_adapter.MyLogicAdapter'

Responding to specific input

If you want a particular logic adapter to only respond to a unique type of input, the best way to do this is by overriding the can_process method on your own logic adapter.

Here is a simple example. Let’s say that we only want this logic adapter to generate a response when the input statement starts with “Hey Mike”. This way, statements such as “Hey Mike, what time is it?” will be processed, but statements such as “Do you know what time it is?” will not be processed.

def can_process(self, statement):
    if statement.text.startswith('Hey Mike')
        return True
        return False

Interacting with services

In some cases, it is useful to have a logic adapter that can interact with an external service or api in order to complete its task. Here is an example that demonstrates how this could be done. For this example we will use a fictitious API endpoint that returns the current temperature.

def can_process(self, statement):
    Return true if the input statement contains
    'what' and 'is' and 'temperature'.
    words = ['what', 'is', 'temperature']
    if all(x in statement.text.split() for x in words)
        return True
        return False

def process(self, statement):
    from chatterbot.conversation import Statement
    import requests

    # Make a request to the temperature API
    response = requests.get('')
    data = response.json()

    # Let's base the confidence value on if the request was successful
    if response.status_code == 200:
        confidence = 1
        confidence = 0

    temperature = data.get('temperature', 'unavailable')

    response_statement = Statement('The current temperature is {}'.format(temperature))

    return confidence, response_statement

Providing extra arguments

All key word arguments that have been set in your ChatBot class’s constructor will also be passed to the __init__ method of each logic adapter. This allows you to access these variables if you need to use them in your logic adapter. (An API key might be an example of a parameter you would want to access here.)

You can override the __init__ method on your logic adapter to store additional information passed to it by the ChatBot class.

class MyLogicAdapter(LogicAdapter):
    def __init__(self, **kwargs):
        super(MyLogicAdapter, self).__init__(**kwargs)

        self.api_key = kwargs.get('secret_key')

The secret_key variable would then be passed to the ChatBot class as shown below.

chatbot = ChatBot(
    # ...