gunthercox / ChatterBot

ChatterBot is a machine learning, conversational dialog engine for creating chat bots
https://chatterbot.readthedocs.io
BSD 3-Clause "New" or "Revised" License
14.06k stars 4.44k forks source link

How to pass extra parameters to Chatterbot's Statement object and get those parameters with get_response() method or get_response.serialize() #1997

Open MurphyAdam opened 4 years ago

MurphyAdam commented 4 years ago

I am using Chatterbot's logical adapters to generate different responses including calls to external APIs such as weather and news. My problem is simple, I would like to pass an extra parameter to the Statement object when I am returning a response from a logical adapter. This extra parameter would be something like 'message_type' explaining the data returned as in message_type='weather_data'. To illustrate that, here is some code:

what I am doing now:

class WeatherAPIAdapter(LogicAdapter):

    def __init__(self, chatbot, **kwargs):
        super().__init__(chatbot, **kwargs)
        # varibales 
    def can_process(self, statement):
        """
        Return true if the input statement contains
        'what' and 'is' and 'temperature'.
        """
        # verification code here
        return True

    def process(self, input_statement, additional_response_selection_parameters):
        from chatterbot.conversation import Statement
        weather_data = call_to_api()
        response_statement = Statement(
            text=weather_data
            )
        response_statement.confidence = 1.0
        return response_statement

what I need:

class WeatherAPIAdapter(LogicAdapter):

    def __init__(self, chatbot, **kwargs):
        super().__init__(chatbot, **kwargs)
        # varibales 
    def can_process(self, statement):
        """
        Return true if the input statement contains
        'what' and 'is' and 'temperature'.
        """
        # verification code here
        return True

    def process(self, input_statement, additional_response_selection_parameters):
        from chatterbot.conversation import Statement
        weather_data = call_to_api()
        message_type = "weather_data"
        response_statement = Statement(
            text=weather_data,
            message_type=message_type
            )
        response_statement.confidence = 1.0
        return response_statement

and then be able to get the message_type using like the following:

message_type = bot.get_response(input_data).message_type
response = bot.get_response(input_data).text

or with the serialize() method:

data = bot.get_response(input_data).serialize()

Thank you very much for your help.

MurphyAdam commented 4 years ago

@gunthercox do you please think this is possible? Perhaps in future releases?