Tutorial: Building Assistants
These docs are for version 1.x of Rasa Open Source.
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
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 are empty.
Memoization Policy
The MemoizationPolicy remembers examples from training stories for up to a max_history of turns. 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:
policies:
- name: MemoizationPolicy
max_history: 1
- name: MappingPolicy
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 templates to our domain.yml file:
intents:
- greet
- bye
- thank
actions:
- utter_greet
- utter_noworries
- utter_bye
templates:
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:
## intent:greet
- Hi
- Hey
- Hi bot
- Hey bot
- Hello
- Good morning
## intent:bye
- goodbye
- goodnight
## intent:thank
- Thanks
- Thank you
- Thank you so much
You can now train a first model and test the bot, by running the following commands:
rasa train
rasa shell
Response Selectors
The Response Selector NLU component is designed to manage dialogue elements like Small Talk and FAQ messages in a simple manner. By using the ResponseSelector, you only need one story to handle all FAQs.
Next, we’ll need to define the responses associated with these FAQs in a new file called responses.md in the data/ directory:
## ask channels
* faq/ask_channels
- We have a comprehensive list of [supported connectors](/content/docs/core/connectors/index.html).
## ask languages
* faq/ask_languages
- You can use Rasa to build assistants in any language you want!
## ask rasa x
* faq/ask_rasax
- Rasa X is a tool to learn from real conversations and improve your assistant. Read more [here](/content/docs/rasa-x/index.html)
Business logic
A lot of conversational assistants have user goals that involve collecting a bunch of information from the user. This is called slot filling. We will start by defining a SalesForm class to gather the necessary pieces of information:
from rasa_sdk.forms import FormAction
class SalesForm(FormAction):
def name(self):
return "sales_form"
@staticmethod
def required_slots(tracker):
return ["job_function", "use_case", "budget", "person_name", "company", "business_email"]
Next, you’ll need to specify how the bot should ask for this information in your domain file.
utter_ask_business_email:
- text: What's your business email?
You’ll need to create an intent to activate the form as well as an intent for providing all the information the form asks the user for:
## intent:contact_sales
- I wanna talk to your sales people.
Fallback Policy
Adding the TwoStageFallbackPolicy can help manage low NLU confidence:
policies:
- name: TwoStageFallbackPolicy
nlu_threshold: 0.8
To handle generic out of scope requests, you can define an out_of_scope intent:
* out_of_scope
utter_out_of_scope
This approach allows you to handle unexpected user inputs gracefully.
More Complex Contextual Conversations
This section will cover handling various user interactions, such as faking sales requests or general inquiries not explicitly defined in your skill set. To effectively manage these conversations, you’ll need to structure your stories accordingly, ensuring that your bot can respond appropriately based on the previous interactions from the user. This will ensure a smooth flow of dialogue.
Utilizing the principles shared in this tutorial, you can build a robust FAQ assistant capable of handling specific requests while keeping room for development towards a more contextual assistant.