1

I have made a simple Chatbot which can answer static queries. It is working ok so far But the problem is when a query is asked which is not in training data, it is just picking any random statement. I want Chatbot to answer some default statement like "I am not trained for this","I don't know"..etc. my code is as below:

bot = ChatBot(
    "ChatBot",
    logic_adapters=[
        {
            'import_path': 'chatterbot.logic.BestMatch'
        },
        {
            'import_path': 'chatterbot.logic.BestMatch',
            'threshold': 0.65,
            'default_response': 'default_value'
        }
    ],
    response_selection_method=get_most_frequent_response,
    input_adapter="chatterbot.input.VariableInputTypeAdapter",
    output_adapter="chatterbot.output.OutputAdapter",
    storage_adapter="chatterbot.storage.SQLStorageAdapter",
    database="db.sqlite3"
)

def get_bot_response():
    userText = request.args.get('msg')
    botReply = str(bot.get_response(userText))
    if botReply is "default_value":
        botReply = str(bot.get_response('default_response'))

in my training data,I have set answers for 'default_response' as 'I don't know','I need to think on it' etc..

But, this is not working as botReply is already being parsed before going to if statement.

Can someone help on how to get default_response rather giving Random answers ?

Shikha
  • 321
  • 1
  • 4
  • 19
  • If I understand correctly, after you run `botReply = str(bot.get_response(userText))` then that will never be `"default_value"` and therefore can't match to override the response? – MyNameIsCaleb Oct 17 '19 at 14:56
  • Yes.and hence never getting a default response which is set as 'I don't know' etc..giving random response . So,how should I write the code so that it will go for default value and it will answer accordingly. – Shikha Oct 17 '19 at 17:05
  • So how would you know you wanted to get a default value or keep the initial value returned? @Shikha – MyNameIsCaleb Oct 17 '19 at 17:06
  • If the Question is not matching with any of training data for which answer is set then change the question as user_text =default_value and get the response for default_value as default_response – Shikha Oct 17 '19 at 17:10
  • What library are you using that gives you the `ChatBot` class? – MyNameIsCaleb Oct 17 '19 at 17:15
  • ChatterBotCorpusTrainer – Shikha Oct 17 '19 at 17:18
  • Hi @Shikha could you test the answer below and see if it works now? If not provide feedback so I can look at it further. If it does, mark answered, vote, and assign bounty so this question can be closed. – MyNameIsCaleb Oct 22 '19 at 16:16
  • I went through the docs and modified the code.its working now – Shikha Oct 23 '19 at 18:02
  • Great, please assign bounty as well. – MyNameIsCaleb Oct 23 '19 at 18:03

1 Answers1

3

There is an example of what you are doing in the docs for ChatterBot. I would assume the version you are using will operate the same. You can also read here where it discusses how it selects with multiple logic adapters.

bot = ChatBot(
    "ChatBot",
    logic_adapters=[
        {
            'import_path': 'chatterbot.logic.BestMatch',
            'threshold': 0.65,
            'default_response': 'default_value'
        }
    ],
    response_selection_method=get_most_frequent_response,
    input_adapter="chatterbot.input.VariableInputTypeAdapter",
    output_adapter="chatterbot.output.OutputAdapter",
    storage_adapter="chatterbot.storage.SQLStorageAdapter",
    database="db.sqlite3"
)

def get_bot_response():
    userText = request.args.get('msg')
    botReply = str(bot.get_response(userText))
    if botReply is "default_value":
        botReply = str(bot.get_response('default_response'))

You need to only use one logic_adapter which will automatically return the default value if under the threshold set. You can use multiple adapters but since you are doing the same type of logic for both it doesn't make sense to have two.

In your question you are getting a response without a threshold first and then potentially a second response or a default value without confidence, so the one with confidence is going to win.

MyNameIsCaleb
  • 4,409
  • 1
  • 13
  • 31