RasaHQ / rasa

💬 Open source machine learning framework to automate text- and voice-based conversations: NLU, dialogue management, connect to Slack, Facebook, and more - Create chatbots and voice assistants
https://rasa.com/docs/rasa/
Apache License 2.0
18.94k stars 4.64k forks source link

how to display a default message if outside intents ? #1191

Closed tushar1328 closed 6 years ago

tushar1328 commented 6 years ago

I used the fallback method to handle this but still getting no success.

wrathagom commented 6 years ago

You're going to to need to provide more information on what exactly you mean by I used the fallback method to handle this but still getting no success. What problem is it that you are facing?

tushar1328 commented 6 years ago

Here is my training file

train_init.py

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import logging

from rasa_core.agent import Agent
from rasa_core.policies.keras_policy import KerasPolicy
from rasa_core.policies.memoization import MemoizationPolicy
from rasa_core.policies.fallback import FallbackPolicy

if __name__ == '__main__':
    logging.basicConfig(level='INFO')

    training_data_file = './data/stories.md'
    model_path = './models/dialogue'
    fallback = FallbackPolicy(fallback_action_name="utter_default",core_threshold=0.7,nlu_threshold=0.7)
    agent = Agent('domain.yml', policies = [MemoizationPolicy(), KerasPolicy(),fallback])

    agent.train(
            training_data_file,
            epochs = 300,
            batch_size = 10,
            validation_split = 0.2)

    agent.persist(model_path)

Here is my app file

run_app.py

from rasa_core.channels import HttpInputChannel
from rasa_core.agent import Agent
from rasa_core.interpreter import RasaNLUInterpreter
from rasa_slack_connector import SlackInput

nlu_interpreter = RasaNLUInterpreter('./models/nlu/default/test')
agent = Agent.load('./models/dialogue', interpreter = nlu_interpreter)

input_channel = SlackInput('xoxb-5148175159-385053278679-bQ74', 'xoxb-5148175159-385053278679-bQ74zXfnZRyz3FAHL',
                            True)

agent.handle_channel(HttpInputChannel(5004, '/', input_channel))

Here is my domain file

domain.yml

slots:
  device:
    type: text
  account:
    type: text

intents:
 - greet
 - goodbye
 - question
 - appreciate
 - interest
 - default

entities:
 - device
 - account

templates:
  utter_greet:
  - text: "Welcome"
  utter_goodbye:
    - 'Bye bye :('
  utter_select_device:
  - text: "Banking"
    buttons:
    - title: "Mobile Banking"
      payload: "Mobile Banking"
    - title: "Phone Banking"
      payload: "Phone Banking"
    - title: "Internet Banking"
      payload: "Internet Banking"
    - title: "ATM"
      payload: "ATM"
    - title: "Branch"
      payload: "Branch"
  utter_que:
  - text: "Anything else I can help you?"
  utter_appreciate:
  - text: "Welcome"
  utter_account:
  - text: "Account Type"
    buttons:
    - title: "Savings Account"
      payload: "Savings Account"
    - title: "Current Account"
      payload: "Current Account"
  utter_default:
  - text: "Sorry was unable to solve"

actions:
 - utter_greet
 - utter_goodbye
 - utter_select_device
 - actions.ActionMobile
 - utter_que
 - utter_appreciate
 - actions.ActionAccount
 - utter_account
 - utter_default

just removed the slack details for safety.

although inserting fallback policy to the code i still get the reply beside the utter_default.

akelad commented 6 years ago

Can you please post the logs of an example conversation where you would want the default message to appear?

wrathagom commented 6 years ago

@tushar1328 did you solve your problem?

tushar1328 commented 6 years ago

@wrathagom I have not yet solved the problem. Need to tweak the code please help, have a look to image. errors error 1

the image shows the fallback not been called, after some of the iterations the you can see that the bot replies that "didn't get what your are saying." this also a line in the action which is not the fallback answer. the fallback answer is " Sorry,I did not understand what you mean, Please elaborate again !!!! :) ". the fallback answer should appear if the connect rather than the intents is being asked.

I have used spacy model. I have attached all the required files to the comment. Please help

domain.yml

slots:
  device:
    type: text
  account:
    type: text
  card:
    type: text
  move:
    type: text
  time:
    type: text
  query:
    type: text

intents:
 - greet
 - goodbye
 - question
 - appreciate
 - default

entities:
 - device
 - account
 - card
 - time
 - query

templates:
  utter_greet:
  - text: "Welcome, How may I help you ?"
  utter_goodbye:
    - 'Bye bye :('
  utter_select_device:
  - text: "Addhar Card can be linked in many ways how would you like to?"
    buttons:
    - title: "Mobile Banking"
      payload: "Mobile Banking"
    - title: "Phone Banking"
      payload: "Phone Banking"
    - title: "Internet Banking"
      payload: "Internet Banking"
    - title: "ATM"
      payload: "ATM"
    - title: "Branch"
      payload: "Branch"
  utter_que:
  - text: "Anything else I can help you?"
  utter_appreciate:
  - text: "Welcome"
  utter_account:
  - text: "For what type of account do you need information"
    buttons:
    - title: "Savings Account"
      payload: "Savings Account"
    - title: "Current Account"
      payload: "Current Account"
  utter_default:
  - text: "Sorry,I did not understand what you mean, Please elaborate again !!!! :) "

actions:
 - utter_greet
 - utter_goodbye
 - utter_select_device
 - utter_account
 - utter_default
 - utter_que
 - utter_appreciate
 - actions.ActionMobile
 - actions.ActionAccount
 - actions.ActionFallback

Action.py

from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

from rasa_core.actions.action import Action
from rasa_core.events import SlotSet
from rasa_core.events import AllSlotsReset
import json
import requests 
import urllib.parse
url=('http://192.168.1.29:1111/rasa/faq/')
class ActionMobile(Action):
    def name(self):
        return 'action_mobile'

    def run(self, dispatcher, tracker, domain):
        d = str(tracker.get_slot('device'))
        d = d.lower()
        c = str(tracker.get_slot('card'))
        c = c.lower()
        m = str(tracker.get_slot('move'))
        m = m.lower()
        if m == "link" and ( c == "addhar card" or c == "aadhaar card") and (d == "mobile banking" or d == "mobile"):

            url_append=urllib.parse.urljoin(url,"2")
            print(url_append)
            req = requests.get(url_append)
            l=req.json()
            main=l["data"]['faq_ans']
            repo="""Here is what you need\n {} """.format (main)
            dispatcher.utter_message(repo)  
        elif m == "link" and ( c == "addhar card" or c == "aadhaar card") and d == "internet banking":

            url_append=urllib.parse.urljoin(url,"3")
            print(url_append)
            req = requests.get(url_append)
            l=req.json()
            main=l["data"]['faq_ans']
            repo="""Here is what you need\n {} """.format (main)
            dispatcher.utter_message(repo)  
        elif m == "link" and ( c == "addhar card" or c == "aadhaar card") and d == "phone banking":

            url_append=urllib.parse.urljoin(url,"4")
            print(url_append)
            req = requests.get(url_append)
            l=req.json()
            main=l["data"]['faq_ans']
            repo="""Here is what you need\n {} """.format (main)
            dispatcher.utter_message(repo)
        elif m == "link" and ( c == "addhar card" or c == "aadhaar card") and d == "atm":

            url_append=urllib.parse.urljoin(url,"5")
            print(url_append)
            req = requests.get(url_append)
            l=req.json()
            main=l["data"]['faq_ans']
            repo="""Here is what you need\n {} """.format (main)
            dispatcher.utter_message(repo)
        elif m == "link" and (c == "addhar card" or c == "aadhaar card") and d == "branch":
            url_append=urllib.parse.urljoin(url,"6")
            print(url_append)
            req = requests.get(url_append)
            l=req.json()
            main=l["data"]['faq_ans']
            repo="""Here is what you need\n {} """.format (main)
            dispatcher.utter_message(repo)
        else:
            dispatcher.utter_message("I don't get what are you saying")
        #return [SlotSet("move" and "card",m and p if m =="link" and (p == "addhar card" or p == "aadhaar card") else AllSlotsReset()]
        return [AllSlotsReset()

class ActionAccount(Action):

        def name(self):
            return 'action_account'

        def run(self, dispatcher, tracker, domain):
            a = str(tracker.get_slot('account'))
            a = a.lower()
            c = str(tracker.get_slot('card'))
            c = c.lower()
            m = str(tracker.get_slot('move'))
            m = m.lower()   
            t = str(tracker.get_slot('time'))
            t = t.lower()   
            q = str(tracker.get_slot('query'))
            q = q.lower()   
            if c == "debit card" and m == "customise" and a == "savings account":
                url_append=urllib.parse.urljoin(url,"14")
                print(url_append)
                req = requests.get(url_append)
                l=req.json()
                main=l["data"]['faq_ans']
                repo="""Here is what you need\n {} """.format (main)
                dispatcher.utter_message(repo)
            elif c == "debit card" and m == "customise" and a == "current account":
                url_append=urllib.parse.urljoin(url,"14")
                print(url_append)
                req = requests.get(url_append)
                l=req.json()
                main=l["data"]['faq_ans']
                repo="""Here is what you need\n {} """.format (main)
                dispatcher.utter_message(repo)              
            elif c == "credit card" and m == "customise":
                url_append=urllib.parse.urljoin(url,"15")
                print(url_append)
                req = requests.get(url_append)
                l=req.json()
                main=l["data"]['faq_ans']
                repo="""Here is what you need\n {} """.format (main)
                dispatcher.utter_message(repo)
            elif a == "current account" and m == "type":
                url_append=urllib.parse.urljoin(url,"7")
                print(url_append)
                req = requests.get(url_append)
                l=req.json()
                main=l["data"]['faq_ans']
                repo="""Here is what you need\n {} """.format (main)
                dispatcher.utter_message(repo)              
            elif (q == "free transactions" or q == "free transaction") and t == "month" and (m == "none" or m == "decide"):
                url_append=urllib.parse.urljoin(url,"8")
                print(url_append)
                req = requests.get(url_append)
                l=req.json()
                main=l["data"]['faq_ans']
                repo="""Here is what you need\n {} """.format (main)
                dispatcher.utter_message(repo)
            elif (a == "savings account") and (t == "month") and (m == "get") and (q == "free cheque"):
                url_append=urllib.parse.urljoin(url,"16")
                print(url_append)
                req = requests.get(url_append)
                l=req.json()
                main=l["data"]['faq_ans']
                repo="""Here is what you need\n {} """.format (main)
                dispatcher.utter_message(repo)
            elif (a == "current account") and (t == "month") and (m == "get") and (q == "free cheque"):
                url_append=urllib.parse.urljoin(url,"17")
                print(url_append)
                req = requests.get(url_append)
                l=req.json()
                main=l["data"]['faq_ans']
                repo="""Here is what you need\n {} """.format (main)
                dispatcher.utter_message(repo)
            elif (a == "none") and (t == "month") and (m == "get") and (q == "free cheque"):
                url_append=urllib.parse.urljoin(url,"9")
                print(url_append)
                req = requests.get(url_append)
                l=req.json()
                main=l["data"]['faq_ans']
                repo="""Here is what you need\n {} """.format (main)
                dispatcher.utter_message(repo)              
            else:               
                dispatcher.utter_message("Didn't get what were you saying")
            return [AllSlotsReset()]

class ActionFallback(Action):

    def name(self):
        return 'fallback'

    def run(self, dispatcher, tracker, domain):
        from rasa_core.events import UserUtteranceReverted

        dispatcher.utter_message("Sorry, didn't get that. Try again.")
        return [UserUtteranceReverted()]
akelad commented 6 years ago

@tushar1328 can you please try the latest master of Core and see if that works for you? use the name "action_default_fallback" as the fallback action for the fallback policy instead of your custom defined one.

tushar1328 commented 6 years ago

@akelad it worked with great with "action_default" It just needed proper training to be done with more bunch of stories. Thank for the help