As a content writer and a Rasa enthusiast, I understand the importance of validating slots in Rasa. For those who are new to Rasa, slots are used to store information that is collected from the user during a conversation.
Validating these slots is crucial as it ensures that the data collected is accurate and reliable. In this tutorial, we will go through the process of validating slots in Rasa.
Exclusive Slots & Free Spins Offers:
Step 1: Define your Slot
To validate a slot, you first need to define it in your domain file. The domain file is where you define all the intents, entities, actions, and slots that your chatbot will use. For this tutorial, we will assume that you have already defined your slot in the domain file.
Step 2: Define Your Form
Once you have defined your slot, you need to create a form that will collect information from the user. Forms are used to collect multiple pieces of information from the user before executing an action. You can also use forms to validate individual slots.
To create a form, you need to create an action in your actions.py file. The action should inherit from the FormAction class provided by Rasa. Here is an example of how to create a simple form:
“`
from typing import Dict, Text, Any, List
from rasa_sdk import Action
from rasa_sdk.events import SlotSet
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.forms import FormAction
class MyForm(FormAction):
def name(self) -> Text:
return “my_form”
@staticmethod
def required_slots(tracker: Tracker) -> List[Text]:
return [“slot1”, “slot2”]
def slot_mappings(self) -> Dict[Text, Any]:
return {
“slot1”: self.from_text(),
“slot2”: self.from_text()
}
def submit(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict]:
# do something with the collected slots
return []
“`
In this example, we have created a form called “my_form” that collects two slots – “slot1” and “slot2”. The required_slots method specifies which slots are required for the form to be considered complete.
The slot_mappings method maps the user input to the appropriate slot. Finally, the submit method is called when all required slots have been filled.
Step 3: Validate Your Slot
To validate a slot in Rasa, you need to implement a validate_slotname method in your form action. For example, if your slot is called “email”, you would implement a validate_email method.
Here is an example of how to validate a slot:
“`
class MyForm(FormAction):
def name(self) -> Text:
return “my_form”
@staticmethod
def required_slots(tracker: Tracker) -> List[Text]:
return [“email”]
def slot_mappings(self) -> Dict[Text, Any]:
return {
“email”: self.from_text()
}
def validate_email(
self,
value: Text,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]
) -> Dict[Text, Any]:
if not value:
dispatcher.utter_message(text=”Please provide an email address.”)
return {“email”: None}
if “@” not in value:
dispatcher.utter_message(text=”This doesn’t seem like a valid email address.”)
return {“email”: None}
# validation succeeded
return {“email”: value}
“`
In this example, we are validating the email slot by checking whether it contains an “@” symbol. If it does not, we prompt the user to provide a valid email address. If the validation succeeds, we return the value of the slot.
Step 4: Add Your Form to Your Story
The final step is to add your form to your story. A story is a sequence of user inputs and bot responses that represent a conversation. Here is an example of how to add your form to a story:
“`
## My Story
* greet
– utter_greet
* my_form
– my_form
* goodbye
– utter_goodbye
“`
In this example, we are defining a story called “My Story” that starts with the user greeting the bot, then filling out our form (“my_form”), and finally saying goodbye.
Conclusion
In conclusion, validating slots in Rasa is an important aspect of building reliable chatbots. By following these steps, you can ensure that the data collected from users is accurate and reliable.
Remember to define your slot in your domain file, create a form, validate your slot in the form action, and add your form to your story. With these steps in mind, you will be well on your way to building powerful and reliable chatbots with Rasa!