Rasa SDK
These docs are for version 1.x of Rasa Open Source.
User Guide
- Installation
- Tutorial: Rasa Basics
- Tutorial: Building Assistants
- Command Line Interface
- Architecture
- Messaging and Voice Channels
- Testing Your Assistant
- Setting up CI/CD
- Validate Data
- Configuring the HTTP API
- Deploying Your Rasa Assistant
- Cloud Storage
NLU
- About
- Using NLU Only
- Training Data Format
- Language Support
- Choosing a Pipeline
- Components
- Entity Extraction
Core
- About
- Stories
- Domains
- Responses
- Actions
- Reminders and External Events
- Policies
- Slots
- Forms
- Retrieval Actions
- Interactive Learning
- Fallback Actions
- Knowledge Base Actions
Conversation Design
API Reference
- Action Server
- HTTP API
- Jupyter Notebooks
- Agent
- Custom NLU Components
- Rasa SDK
- Events
- Tracker
- Tracker Stores
- Event Brokers
- Lock Stores
- Training Data Importers
- Featurization of Conversations
- TensorFlow Configuration
- Migration Guide
- Rasa Open Source Change Log
Migrate from (beta)
Reference
Rasa SDK
Rasa SDK provides the tools you need to write custom actions in python.
Installation
Use pip to install rasa-sdk on your action server.
pip 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
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().
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 [])]
Customizing the session start action
The default behavior of the session start action is to take all existing slots and to carry them over into the next session. Let’s say you do not want to carry over all slots, but only a user’s name and their phone number. To do that, you’d override the action_session_start with a custom action that might look like this:
from typing import Text, List, Dict, Any
from rasa_sdk import Action, Tracker
from rasa_sdk.events import SlotSet, SessionStarted, ActionExecuted, EventType
from rasa_sdk.executor import CollectingDispatcher
class ActionSessionStart(Action):
def name(self) -> Text:
return "action_session_start"
@staticmethod
def fetch_slots(tracker: Tracker) -> List[EventType]:
slots = []
for key in ("name", "phone_number"):
value = tracker.get_slot(key)
if value is not None:
slots.append(SlotSet(key=key, value=value))
return slots
async def run(
self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any],
) -> List[EventType]:
events = [SessionStarted()]
events.extend(self.fetch_slots(tracker))
events.append(ActionExecuted("action_listen"))
return events
Events
An action’s run() method returns a list of events. For more information on the different types of events, see Events. There is an example of a SlotSet event above.
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.
The following are available as attributes of a Tracker object:
sender_id- The unique ID of person talking to the bot.slots- The list of slots that can be filled as defined in the “ref” domains.latest_message- A dictionary containing 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.