Rasa SDK
Rasa SDK
Rasa SDK provides the tools you need to write custom actions in python.
- Installation
Usepipto installrasa-sdkon your action server.
pip install rasa-sdk
Note: You do not need to install rasa for your action server.
E.g. if you are running Rasa in a docker container, it is recommended to create a separate container for your action server. In this separate container, you only need to install rasa-sdk.
Running the Action Server
If you have rasa installed, run this command to start your action server:
rasa run actions
Otherwise, if you do not have rasa installed, run this command:
python -m rasa_sdk --actions actions
You can verify that the action server is up and running with the command:
curl http://localhost:5055/health
You can get the list of registered custom actions with the command:
curl http://localhost:5055/actions
The file that contains your custom actions should be called actions.py. Alternatively, you can use a package directory called actions or else manually specify an actions module or package with the --actions flag.
Actions
The Action class is the base class for any custom action. It has two methods that both need to be overwritten, name() and run().
Example:
from rasa_sdk import Action
from rasa_sdk.events import SlotSet
class ActionCheckRestaurants(Action):
def name(self) -> Text:
return "action_check_restaurants"
def run(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
cuisine = tracker.get_slot('cuisine')
q = "select * from restaurants where cuisine='{0}' limit 1".format(cuisine)
result = db.query(q)
return [SlotSet("matches", result if result is not None else [])]
You should add the action name action_check_restaurants to the actions in your domain file. The action’s run() method receives three arguments. You can access the values of slots and the latest message sent by the user using the tracker object, and you can send messages back to the user with the dispatcher object, by calling dispatcher.utter_message.
Details of the run() Method
Execute the side effects of this action.
Parameters
- dispatcher – the dispatcher which is used to send messages back to the user. Use
dipatcher.utter_message()or any otherrasa_sdk.executor.CollectingDispatchermethod. - tracker – the state tracker for the current user. You can access slot values using
tracker.get_slot(slot_name), the most recent user message istracker.latest_message.textand any otherrasa_sdk.Trackerproperty. - domain – the bot’s domain
Returns
A dictionary of rasa_sdk.events.Event instances that is returned through the endpoint.
Events
An action’s run() method returns a list of events. For more information on the different types of events, see Events. If the action should not trigger any other events, it should return an empty list.
Tracker
The rasa_sdk.Tracker lets you access the bot’s memory in your custom actions. You can get information about past events and the current state of the conversation through Tracker attributes and methods.
Available attributes:
sender_id- The unique ID of person talking to the bot.slots- The list of slots that can be filled as defined in the domains.latest_message- The attributes of the latest message:intent,entitiesandtext.events- A list of all previous events.active_form- The name of the currently active form.latest_action_name- The name of the last action the bot executed.
Available methods:
Tracker.get_slot(key)- Retrieves the value of a slot.Tracker.latest_message- Access latest user message.
👋 I can help you get started with Rasa and answer your technical questions.