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.88k stars 4.63k forks source link

Custom action not working #2456

Closed rkkumar16 closed 5 years ago

rkkumar16 commented 6 years ago
**Rasa Core version**: 0.11.0a1 **Python version**: 3.6 **Operating system** (windows, osx, ...):windows 7 **Issue**: I am using rasa_core 0.11a1 version and I have created a custom action actions.py ``` from rasa_core_sdk import Action from rasa_core_sdk.events import SlotSet from bs4 import BeautifulSoup import requests class ActionGetDate(Action): def name(self): return "ask_from_date" def run(self, dispatcher, tracker, domain): print("Events: \n"+tracker.events) print("Past states: \n"+tracker.past_states()) print("Applied events"+tracker.applied_events()) #dispatcher.utter_message("Values have been saved") return [] ``` I also created endpoints yml file for this **endpoints.yml** ``` ask_from_date: url: "http://192.168.54.44:5005/chat/" ``` **Content of domain file** (if used & relevant): ```yaml slots: user_mail: type: text bot_action: type: text cancel_type: type: text cancel_from: type: text cancel_upto: type: text entities: - user_mail - bot_action - cancel_type - cancel_from - cancel_upto - htype intents: - start_bot - get_mail - trip_cancel - trip_type - get_from_date - get_to_date actions: - utter_greet - ask_from_date - utter_bot_action - utter_cancel_options - utter_cancel_from - utter_cancel_upto - utter_cancel_result templates: utter_greet: - text: "Hi, I'm Helpbot. How may I help you?" utter_ask_email: - text: "Can you please enter your email ID" utter_bot_action: - text: "Hi {user_mail}! How may I help you on trip cancellation. Pls opt one of the below options" buttons: - title: "Trip cancellation" payload: "Trip cancellation" - title: "Log a feedback" payload: "Log a feedback" utter_cancel_options: - text: "Hi {user_mail}! How may I help you on trip cancellation. Please select one of the below options" buttons: - title: "cancel pick" payload: "cancel pick" - title: "cancel drop" payload: "cancel drop" utter_cancel_from: - text: "OK. We will help you the cancellation. Pls mention from date in DD/MM/YYY format. For single day cancellation from and to date will be same" utter_cancel_upto: - text: "OK. Pls mention upto date in DD/MM/YYY format. For single day cancellation from and to date will be same" utter_cancel_result: - text: "Hi, {user_mail}! Your trip has been cancelled for the requested date." ``` ``` stories.md start_bot utter_ask_email get_mail{"user_mail": "xyz@gmail.com"} utter_bot_action trip_cancel{"bot_action": "Trip Cancel"} utter_cancel_options trip_type{"cancel_type": "pick up"} ask_from_date get_from_date{"cancel_from": "27/11/2018"} utter_cancel_upto get_to_date{"cancel_upto": "27/11/2018"} utter_cancel_result nlu.md intent:start_bot Hi Hi Hii Hii awake Hey Hey Hello Hello intent:get_mail My email is abc@gmail.com My email is saket.kumar@yahoo.com My email is testmail@remail.com My email is dummy@email.in My email is m@yahoo.in My email is what@dex.com My email is testmail@testmail.com My email is Myname@in.com My email is My@gmail.com My email is My@mail.com My email is My@dummy.in My email is My@whyme.com rahul.kumar@yahoo.com is my email address user.name@gmail.com is my email address intent:trip_type cancel pick cancel pick cancel pick cancel both cancel both cancel both cancel drop cancel drop cancel drop intent:bot_action Trip Cancellation Trip Cancellation Log a feedback Log a feedback intent:need_help Need help Need to contact Help required How can I get help intent:get_from_date from 27/11/2018 From 7/11/2018 from 2/11/2018 From 27/1/2018 From 27/11/2016 From 27/11/2012 from 27/11/2011 From 27/11/2010 From 27/11/2015 From 27/11/2014 intent:get_to_date to 28/11/2018 upto 28/11/2018 Till 28/11/2018 Uptill 28 Next 28 coming Monday ``` I followed the documentation and created endpoints file and got following log file ``` if diff: 2018-08-31 12:24:02 DEBUG rasa_core.processor - Received user message 'Hello' with intent '{'name': 'start_bot', 'confidence': 0.967832624912262}' and entities '[]' 2018-08-31 12:24:02 DEBUG rasa_core.processor - Logged UserUtterance - tracker now has 2 events 2018-08-31 12:24:02 DEBUG rasa_core.processor - Current slot values: action_type: None cancel_from: None cancel_type: None cancel_upto: None user_mail: None 2018-08-31 12:24:02 DEBUG rasa_core.policies.memoization - Current tracker state [None, None, None, None, {}, {'intent_start_bot': 1.0, 'prev_action_listen': 1.0}] 2018-08-31 12:24:02 DEBUG rasa_core.policies.memoization - There is a memorised next action '11' 2018-08-31 12:24:03 DEBUG rasa_core.policies.ensemble - Predicted next action using policy_0_MemoizationPolicy 2018-08-31 12:24:03 DEBUG rasa_core.processor - Predicted next action 'ask_from_date' with prob 1.00. 2018-08-31 12:24:03 ERROR rasa_core.processor - Encountered an exception while running action 'ask_from_date'. Bot will continue, but the actions events are lost. Make sure to fix the exception in your custom code. 2018-08-31 12:24:03 ERROR rasa_core.processor - The model predicted the custom action 'ask_from_date' but you didn't configure an endpoint to run this custom action. Please take a look at the docs and set an endpoint configuration.https://rasa.com/docs/core/customactions.html Traceback (most recent call last): File "d:\rohitkumar18\libs\rasa_core-master\rasa_core\processor.py", line 319, in _run_action events = action.run(dispatcher, tracker, self.domain) File "d:\rohitkumar18\libs\rasa_core-master\rasa_core\actions\action.py", line 306, in run "".format(self.name(), DOCS_BASE_URL)) Exception: The model predicted the custom action 'ask_from_date' but you didn't configure an endpoint to run this custom action. Please take a look at the docs and set an endpoint configuration.https://rasa.com/docs/core/customactions.html 2018-08-31 12:24:03 DEBUG rasa_core.processor - Action 'ask_from_date' ended with events '[]' 2018-08-31 12:24:03 DEBUG rasa_core.policies.memoization - Current tracker state [None, None, None, {}, {'intent_start_bot': 1.0, 'prev_action_listen': 1.0}, {'prev_ask_from_date': 1.0, 'intent_start_bot': 1.0}] 2018-08-31 12:24:03 DEBUG rasa_core.policies.memoization - There is a memorised next action '0' 2018-08-31 12:24:03 DEBUG rasa_core.policies.ensemble - Predicted next action using policy_0_MemoizationPolicy 2018-08-31 12:24:03 DEBUG rasa_core.processor - Predicted next action 'action_listen' with prob 1.00. 2018-08-31 12:24:03 DEBUG rasa_core.processor - Action 'action_listen' ended with events '[]' ``` So custom action is not working in new version. Can anybody help me solve this?
akelad commented 6 years ago

The logger links you to the documentation: http://rasa.com/docs/core/customactions/ The endpoint should be called action_endpoint

rkkumar16 commented 6 years ago

@akelad I changed the name as well but how I should include it in project? I have just created the file but how does I create endpoint and use for each custom action is my point here

akelad commented 6 years ago

I'm not sure I understand the question, the name of the action doesn't need to change, just what you put in your endpoint.yml. The link above describes exactly how to get the custom action server running

rkkumar16 commented 6 years ago

In earlier versions of rasa_core to run custom actions we just had to create classes like this

class ActionGetDate(Action):
    def name(self):
        return "ask_from_date"

    def run(self, dispatcher, tracker, domain):
        print("Events: \n"+tracker.events)
        print("Past states: \n"+tracker.past_states())
        print("Applied events"+tracker.applied_events())
        #dispatcher.utter_message("Values have been saved")
        return []

and then needed to add actions to domain file like this actions.ActionGetDate

It was so simple there. Now, I'm trying to do the same here. By reading documents I found out that I need to run these on server. I don't understand how can we create and run a custom action and what file needs to be created for that.

akelad commented 6 years ago

Nothing has changed about how you write your actions, they're still kept in the same file as before. But rather than writing actions.ActionGetDate in your domain file, you now write ask_from_date. And to starting the action server is simple since your actions are written in python, take a look at that section in the link I sent you. All you need to do is run this python -m rasa_core_sdk.endpoint --actions actions to start the server

rkkumar16 commented 6 years ago

@akelad earlier we did not need to run server for this. I just want to override the function so that I can apply some validations on it. Why do I need to create an endpoint for this. By this way I would need endpoints for every validation I need to do. Is there no any other way for this?

akelad commented 6 years ago

You don't need to define an endpoint for every action, it's just the one endpoint, called "action_endpoint", which you start with python -m rasa_core_sdk.endpoint --actions actions (if your python file is called actions.py, if it's called myactions.py then the command is python -m rasa_core_sdk.endpoint --actions myactions)

rkkumar16 commented 6 years ago

@akelad Thanks for the support I got it working with this