Link Search Menu Expand Document

Customized Action

In the bot response panel, the designer could click “+Action” to add an Action function written in Python, which will give the designer the tool of inserting python code with more complex logic. The Action python code will be placed to the Actions. py file in Rasa and will be triggered when the bot gives the current response.  

action-02

In the pop up window, the designer can write python code of the Action function.  

action-01

Rasa conversion

Now let’s look at an example that shows how we add the Action code into Rasa files:  

  • actions/Actions.py
  • domain.yml

Actions.py

Action is introduced in Rasa Action. It is a way that Rasa provides to control dialogue from customized Python code. It has a fixed structure, the name method declare this action class, and the run method will be executed during the conversation. We hide the editing of class and name, and only expose the implementation of the run function, which is the Action function. This is an example of Action Function.

Chatbot: I can calculate your BMI. Can you tell me your height?
User: Sure. I'm 165cm.                                               [intent: intent_height, {"height": "165"}]
Chatbot: Great. What about your weight?
User: Maybe 60kg.                                                    [intent: intent_weight, {"weight": "60"}]
Chatbot: {action: calculate_BMI}

In order to complete the conversation, we can use the Action Function to do the calculation.


class BotCustomaction_cp_ce1atgfk53b4_0(Action):
    def name(self) -> Text:
        return "action_cp_ce1atgfk53b4_0"
    async def run(self,
                dispatcher,
                tracker: Tracker,
                domain: Dict[Text, Any],) -> List[Dict[Text, Any]]:
        height = float(tracker.get_slot("height"))
        weight = float(tracker.get_slot("weight"))
        bmi = height / (weight * weight)
        dispatcher.utter_message(text="I get it, your BMI is: " + bmi)
        return []

The class names / method names in the code above:

  • Line 1: class BotCustomaction_uniqueid_index(Action) This is the class name. This is an unique id generated by system.
  • Line 2: def name(self) -> Text return the name of this action. This string will be declared in domain.yml. It is also generated by system.
  • Line 4: async def run() edited by the developer.

Parameters in run function

This is an introduction to some helpful parameters in run function. In the run method, you can access all the chatbot information using the dispatcher and tracker parameters.

  • dispatcher : the output object. dispatcher.utter_message() method can be used to pass a bot response.
    class BotCustomaction_test_dispatcher(Action):
        def name(self) -> Text:
            return "action_test_dispatcher"
        async def run(self,
                    dispatcher,
                    tracker: Tracker,
                    domain: Dict[Text, Any],) -> List[Dict[Text, Any]]:
            dispatcher.utter_message(text="This is a text response")
            dispatcher.utter_message(text="The second response")
            return []
    

    In the conversation:

    Chatbot: This is a text response
    Chatbot: The second response
    
  • tracker :the current user’s tracker. By using tracker.get_slot(slot_name) access to the slot value(that is, the value corresponding to the variable) ,the latest user’s message tracker.latest_message.
        class BotCustomaction_test_tracker(Action):
            def name(self) -> Text:
                return "action_test_tracker"
            async def run(self,
                        dispatcher,
                        tracker: Tracker,
                        domain: Dict[Text, Any],) -> List[Dict[Text, Any]]:
                query = tracker.latest_message["text"]
                height = tracker.get_slot("height")
                dispatcher.utter_message(text="you said: " + query)
                dispatcher.utter_message(text="This is the slot value of height" + height)
                return []
    

    In the conversation:

      user: I am 170cm.
      Chatbot: you said: I am 170cm.
      Chatbot: This is the slot value of height: 170
    

domain.yml

version: '3.2'
actions:
  - action_cp_ce1atgfk53b4_0
  - other_action

Each action has a unique id, like action_cp_ce1atgfk53b4_0, which would be declared in the domain.yml. Only actions that have been declared can be executed properly.