Tutorial: Building Assistants
Tutorial: Building Assistants
After following the basics of setting up an assistant in the Rasa Tutorial, we’ll now walk through building a basic FAQ chatbot and then build a bot that can handle contextual conversations.
Building a simple FAQ assistant
FAQ assistants are the simplest assistants to build and a good place to get started. These assistants allow the user to ask a simple question and get a response. We’re going to build a basic FAQ assistant using features of Rasa designed specifically for this type of assistant.
In this section we’re going to cover the following topics:
- Responding to simple intents with the MemoizationPolicy
- Handling FAQs using the ResponseSelector
We’re going to use content from Sara, the Rasa assistant that, amongst other things, helps the user get started with the Rasa products. You should first install Rasa using the Step-by-step Installation Guide and then follow the Rasa Tutorial to make sure you know the basics.
To prepare for this tutorial, we’re going to create a new directory and start a new Rasa project.
mkdir rasa-assistant
rasa init
Let’s remove the default content from this bot, so that the nlu.md, stories.md and domain.yml files are empty.
Memoization Policy
The MemoizationPolicy remembers examples from training stories for up to a max_history of turns. The number of “turns” includes messages the user sent, and actions the assistant performed. For the purpose of a simple, context-less FAQ bot, we only need to pay attention to the last message the user sent, and therefore we’ll set that to 1.
You can do this by editing your config.yml file as follows (you can remove TEDPolicy for now):
policies:
- name: MemoizationPolicy
max_history: 1
- name: MappingPolicy
Note: The MappingPolicy is there because it handles the logic of the /restart intent, which allows you to clear the conversation history and start fresh.
Now that we’ve defined our policies, we can add some stories for the goodbye, thank and greet intents to the stories.md file:
## greet
* greet
- utter_greet
## thank
* thank
- utter_noworries
## goodbye
* bye
- utter_bye
We’ll also need to add the intents, actions and responses to our domain.yml file in the following sections:
intents:
- greet
- bye
- thank
responses:
utter_noworries:
- text: No worries!
utter_greet:
- text: Hi
utter_bye:
- text: Bye!
Finally, we’ll copy over some NLU data from Sara into our nlu.md file (more can be found here):
## intent:greet
- Hi
- Hey
- Hi bot
- Hey bot
- Hello
- Good morning
- hi again
- hi folks
## intent:bye
- goodbye
- goodnight
- good bye
- good night
- see ya
- toodle-oo
- bye bye
- gotta go
- farewell
## intent:thank
- Thanks
- Thank you
- Thank you so much
- Thanks bot
- Thanks for that
- cheers
You can now train a first model and test the bot, by running the following commands:
rasa train
rasa shell
This bot should now be able to reply to the intents we defined consistently, and in any order.
Handling business logic
A lot of conversational assistants have user goals that involve collecting a bunch of information from the user before being able to do something for them. This is called slot filling. For example, in the banking industry you may have a user goal of transferring money, where you need to collect information about which account to transfer from, whom to transfer to and the amount to transfer. This type of behavior can and should be handled in a rule-based way, as it is clear how this information should be collected.
For this type of use case, we can use Forms and our FormPolicy. The FormPolicy works by predicting the form as the next action until all information is gathered from the user.
As an example, we will build out the SalesForm from Sara. The user wants to contact our sales team, and for this we need to gather the following pieces of information:
- Their job
- Their bot use case
- Their name
- Their email
- Their budget
- Their company
We will start by defining the SalesForm as a new class in the file called actions.py.
from rasa_sdk.forms import FormAction
class SalesForm(FormAction):
"""Collects sales information and adds it to the spreadsheet"""
def name(self):
return "sales_form"
Next we have to define the required_slots method which specifies which pieces of information to ask for, i.e. which slots to fill.
@staticmethod
def required_slots(tracker):
return [
"job_function",
"use_case",
"budget",
"person_name",
"company",
"business_email",
]
Once you’ve done that, you’ll need to specify how the bot should ask for this information. This is done by specifying utter_ask_{slotname} responses in your domain.yml file. For the above we’ll need to specify the following:
utter_ask_business_email:
- text: What's your business email?
utter_ask_company:
- text: What company do you work for?
utter_ask_budget:
- text: "What's your annual budget for conversational AI? 💸"
utter_ask_job_function:
- text: "What's your job? 🕴"
utter_ask_person_name:
- text: What's your name?
utter_ask_use_case:
- text: What's your use case?
We’ll also need to define all these slots in our domain.yml file:
slots:
company:
type: unfeaturized
job_function:
type: unfeaturized
person_name:
type: unfeaturized
budget:
type: unfeaturized
business_email:
type: unfeaturized
use_case:
type: unfeaturized
These methods will allow your bot to effectively collect and respond based on user input, enhancing the user's interaction and providing guided support through various tasks.