Custom Actions Flashcards
What are custom actions?
Custom Actions allows you to run any code you want, like adding an event to a calendar, calling an API, sending emails, or anything else according to your requirement.
How can we setup a custom action to fill slots and send emails?
We need to run an action server by defining action endpoint in the endpoints.yml file.
E.g.:
action_endpoint:
url: 'http://localhost:5055/webhook'
What are slots?
- Slots are our bot’s memory.
- Slots act as key-value store which can be used to store useful information that users provide.
- These information can be used later during conversation. Slots are also useful to gather information from the outside world.
- Results of a database query
what ways can a slots be set?
- using custom actions
- from NLU
- from buttons in the UI
4.
How are slots set in custom actions?
Slots can only be set by returning it in the custom action, as shown below:
class AskReason(Action):
def name(self):
return ‘action_ask_reason’
~~~
def run(self, dispatcher, tracker, domain): personal = tracker.latest_message.get('text') return [SlotSet('reason',personal)]
~~~
Where should every single custom action and slots used be defined?
Every single custom action and slots used should be defined in our domain file i.e domain.yml.
What are the possible types of slots?
Slots can be of different types for eg: text — for text type value, boolean — for true or false values, Category, List, unfeaturized — to not influence the dialog flow, and so on.
What is SMTP?
Simple Mail Transfer Protocol (SMTP) is a protocol, which handles sending an e-mail and routing email between mail servers.
What is Python’s built-in module for sending emails?
The smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon. The smtplib modules is useful for communicating with mail servers to send mail.
What does Rasa NLU do to understand information?
It does intent classification and entity extraction.
What is the purpose of stories.md?
Stories are used to teach Rasa real conversation designs to learn from providing the basis for a scalable machine learning dialogue management.
Rasa stories are a form of training data used to train the Rasa’s dialogue management models.
A story is a representation of a conversation between a user and an AI assistant, converted into a specific format where user inputs are expressed as corresponding intents (and entities where necessary) while the responses of an assistant are expressed as corresponding action names.
A training example for the Rasa Core dialogue system is called a story.
What is interactive learning?
Interactive learning is the way of providing feedback to your bot while you talk to it.
What are the benefits of interactive learning?
- To generate training stories while talking to your bot.
- To do debuggings and see if our intents and entities are being correctly classified using interactive learning.
- To generate new training stories by asking newer and surprising questions to the bot.
- If the information does not fall on any of the given intents, we can also create a new intent and assign it there.
- To teach the bot something it doesn’t know yet.
All conversations you have with your bot in an interactive learning session can later be exported as NLU and dialogue training examples and attached to your original training data sample.
What is required to train the bot in an interactive training session?
To train your bot in interactive learning session, you should have few training stories beforehand.
What labels do you need to to train the model?
the intents for pieces of text
What are the preprocessing steps for raw data?
- Cleaning the text by removing stopwords and stemming
Identifying keywords - Calculating term frequencies
- K-means clustering in the space of term frequency vectors.
If the value of a slot is important what types to use and why?
- If the value itself is important, use a categorical or a bool slot.
- There are also float, and list slots.
- If you just want to store some data, but don’t want it to affect the flow of the conversation, use an unfeaturized slot.
What is the purpose of a text slot?
- A text slot only tells Rasa Core whether the slot has a value.
- The specific value of a text slot (e.g. Bangalore or New York or Hong Kong) doesn’t make any difference.
How are Slots Set from NLU?
If your NLU model picks up an entity, and your domain contains a slot with the same name, the slot will be set automatically. For example:
# story_01 * greet{"name": "Ali"} - slot{"name": "Ali"} - utter_greet
How disable auto filling of slots by the nlu?
To disable this behavior for a particular slot, you can set the auto_fill attribute to False in the domain file:
slots:
name:
type: text
auto_fill: False
~~~
~~~
How are Slots Set By Clicking Buttons?
Rasa Core will send messages starting with a / to the RegexInterpreter, which expects NLU input in the same format as in story files, e.g. /intent{entities}.
For example, if you let users choose a color by clicking a button, the button payloads might be /choose{“color”: “blue”} and /choose{“color”: “red”}.
What tool can we user to create training data?
You can install and use rasa-nlu-trainer:
npm i -g rasa-nlu-trainer
What is the purpose of the configuration file config_spacy.json
- To specify configuration parameters used in training like:
- pipeline
- path
- data
What is the purpose of the pipeline parameter in file config_spacy.json
It specifies
- what featurizers/ feature extractors will be used to extract information
What are the options pipeline parameter in file config_spacy.json
- spacy_sklearn
- ???
what are the basic imports for model training?
from rasa_nlu.training_data import load_data from rasa_nlu import config from rasa_nlu.model import Trainer from rasa_nlu.model import Metadata, Interpreter
How do you set slots programmatically?
Slots Set by Actions
The second option is to set slots by returning events in custom actions. In this case, your stories need to include the slots. For example, you have a custom action to fetch a user’s profile, and you have a categorical slot called account_type. When the fetch_profile action is run, it returns a rasa.core.events.SlotSet event:
slots: account_type: type: categorical values: - premium - basic from rasa_sdk.actions import Action from rasa_sdk.events import SlotSet import requests
class FetchProfileAction(Action): def name(self): return "fetch_profile"
def run(self, dispatcher, tracker, domain): url = "http://myprofileurl.com" data = requests.get(url).json return [SlotSet("account_type", data["account_type"])] # story_01 * greet - action_fetch_profile - slot{"account_type" : "premium"} - utter_welcome_premium
# story_02 * greet - action_fetch_profile - slot{"account_type" : "basic"} - utter_welcome_basic In this case you do have to include the - slot{} part in your stories. Rasa Core will learn to use this information to decide on the correct action to take (in this case, utter_welcome_premium or utter_welcome_basic).
How to set slots by clicking buttons?
Slots Set By Clicking Buttons
You can use buttons as a shortcut. Rasa Core will send messages starting with a / to the RegexInterpreter, which expects NLU input in the same format as in story files, e.g. /intent{entities}. For example, if you let users choose a color by clicking a button, the button payloads might be /choose{“color”: “blue”} and /choose{“color”: “red”}.
utter_ask_color: - text: "what color would you like?" buttons: - title: "blue" payload: '/choose{"color": "blue"}' - title: "red" payload: '/choose{"color": "red"}'
What does an example of a Text Slot look like?
Use For: User preferences where you only care whether or not they’ve been specified. Example: """slots: cuisine: type: text """ Description: Results in the feature of the slot being set to 1 if any value is set. Otherwise the feature will be set to 0 (no value is set).
What is an example of of a boolean type slot?
Write out on paper.
Boolean Slot
bool
Use For:
True or False
Example: slots: is_authenticated: type: bool Description: Checks if slot is set and if True
What is an example of of a Categorical type slot?
Write out on paper.
Categorical Slot
categorical
Use For:
Slots which can take one of N values
Example: slots: risk_level: type: categorical values: - low - medium - high Description: Creates a one-hot encoding describing which of the values matched.
What is an example of of a Float type slot?
Write out on paper.
Float Slot
float
Use For:
Continuous values
Example: slots: temperature: type: float min_value: -100.0 max_value: 100.0 Defaults: max_value=1.0, min_value=0.0
Description:
All values below min_value will be treated as min_value, the same happens for values above max_value. Hence, if max_value is set to 1, there is no difference between the slot values 2 and 3.5 in terms of featurization (e.g. both values will influence the dialogue in the same way and the model can not learn to differentiate between them).
What is an example of of a List type slot?
Write out on paper.
List Slot
list
Use For:
Lists of values
Example:
slots:
shopping_items:
type: list
Description:
The feature of this slot is set to 1 if a value with a list is set, where the list is not empty. If no value is set, or the empty list is the set value, the feature will be 0. The length of the list stored in the slot does not influence the dialogue.
What is an example of of a Unfeaturized type slot?
Write out on paper.
Unfeaturized Slot
unfeaturized
Use For:
Data you want to store which shouldn’t influence the dialogue flow
Example:
slots:
internal_user_id:
type: unfeaturized
Description:
There will not be any featurization of this slot, hence its value does not influence the dialogue flow and is ignored when predicting the next action the bot should run.
What is an example of of a Custom type slot?
Write out on paper.
Custom Slot Types
Maybe your restaurant booking system can only handle bookings for up to 6 people. In this case you want the value of the slot to influence the next selected action (and not just whether it’s been specified). You can do this by defining a custom slot class.
In the code below, we define a slot class called NumberOfPeopleSlot. The featurization defines how the value of this slot gets converted to a vector to our machine learning model can deal with. Our slot has three possible “values”, which we can represent with a vector of length 2.
(0,0) not yet set
(1,0) between 1 and 6
(0,1) more than 6
from rasa.core.slots import Slot
class NumberOfPeopleSlot(Slot):
def feature_dimensionality(self): return 2
def as_feature(self): r = [0.0] * self.feature_dimensionality() if self.value: if self.value <= 6: r[0] = 1.0 else: r[1] = 1.0 return r Now we also need some training stories, so that Rasa Core can learn from these how to handle the different situations:
""" # story1 ... * inform{"people": "3"} - action_book_table ... # story2 * inform{"people": "9"} - action_explain_table_limit """