Building a complex dialog
In this tutorial, you create a dialog for an assistant that helps users with inquiries about a fictitious restaurant called Truck Stop Gourmand.
Learning objectives
By the time you finish the tutorial, you learn how to:
- Plan a dialog.
- Define custom intents.
- Add dialog nodes that can handle your intents.
- Add entities to make your responses more specific.
- Add a pattern entity, and use it in the dialog to find patterns in user input.
- Set and reference context variables.
Duration
This tutorial takes approximately 2 to 3 hours to complete.
Prerequisite
Before you begin, complete the Getting started with dialog tutorial.
You use the dialog that you created, and add nodes to the simple dialog that you built as part of the getting started exercise.
Plan the dialog
You are building an assistant for a restaurant that is named Truck Stop Gourmand that has one location and a thriving cake-baking business. You want the simple assistant to answer user questions about the restaurant, its menu, and to cancel customer cake orders. Therefore, you need to create intents that handle inquiries related to the following subjects:
- Restaurant information
- Menu details
- Order cancellations
You start by creating intents that represent these subjects, and then build a dialog that responds to user questions about them.
Answer questions about the restaurant
Add an intent that recognizes when customers ask for details about the restaurant itself. An intent is the purpose or goal that is expressed in user input. The #General_About_You
intent that is provided with the General content catalog serves a similar function, but its user examples are designed to focus on queries about the assistant as opposed to the business that is using the assistant to help its customers. So, you add your own intent.
Add the #about_restaurant intent
-
On the Intents page, click Create intent.
-
Add the following intent name, and then click Create intent:
about_restaurant
The #about_restaurant intent is added. A number sign (
#
) prefix is added to the intent name to label it as an intent. This naming convention helps you and others recognize the intent as an intent. It has no example user utterances that are associated with it yet. -
In the Add user examples field, enter the following utterances:
Tell me about the restaurant i want to know about you who are the restaurant owners and what is their philosophy? What's your story? Where do you source your produce from? Who is your head chef and what is the chef's background? How many locations do you have? do you cater or host functions on site? Do you deliver? Are you open for breakfast?
-
Click the Close arrow to finish adding the
#reservation
intent and its example utterances.
You added an intent and provided examples of utterances that real users might enter to trigger this intent.
Add a dialog node that is triggered by the #about_restaurant intent
Add a dialog node that recognizes when the user input maps to the intent that you created in the previous step, meaning its condition checks whether your assistant recognized the #about_restaurant
intent from the user input.
-
Click Dialog to open the dialog tree.
-
Find the #General_Greetings node in the dialog tree.
You will add a node that checks for questions about the restaurant after this initial greeting node to reflect the flow you might expect to encounter in a normal conversation. For example,
Hello.
thenTell me about yourself.
-
Click the Node options icon on the Greet customers node, and then select Add node below.
-
Start typing
#about_restaurant
in the If assistant recognizes field, and then select it from the list.This node is used if the user input matches the
#about_restaurant
intent. -
In Assistant responds, enter the text response:
Truck Stop Gourmand is the brainchild of Gloria and Fred Smith. What started out as a food truck in 2004 has expanded into a thriving restaurant. We now have one brick-and-mortar restaurant in downtown Portland. The bigger kitchen brought with it new chefs, but each one is faithful to the philosophy that made the Smith food truck so popular to begin with: deliver fresh, local produce in inventive and delicious ways. Join us for lunch or dinner seven days a week. Or order a cake from our bakery.
Add an image to the response also.
-
Click Add response type.
-
Select Image from the drop-down list. In the Image source field, add
https://www.ibmlearningcenter.com/wp-content/uploads/2018/02/IBM-Learning-Center-Food4.jpg
. -
Move up the image response type, so it is displayed in the response before the text is displayed. Click the Move up arrow to reorder the two response types.
-
Click the close icon to close the node edit view.
Test the #about_restaurant dialog node
Test the intent by checking whether user utterances that are similar to, but not the same as, the examples you added have trained your assistant to recognize input with an #about_restaurant
intent.
-
Click Try it.
-
Type
I want to learn more about your restaurant.
Your assistant indicates that the
#about_restaurant
intent is recognized, and returns a response with the image and text that you specified for the dialog node.
You added a custom intent, and a dialog node that knows how to handle it.
The #about_restaurant
intent is designed to recognize various general questions about the restaurant. You added a single node to capture such questions. The response is long, but it is a single statement that can potentially answer
questions about all of the following topics:
- Restaurant owners
- Restaurant history
- Philosophy
- Number of sites
- Days of operation
- Meals served
- The restaurant bakes cakes to order
For general questions, a single, general answer is suitable.
Manage cake orders
Customers can place orders in person, over the phone, or by using the order form on the website. After the order is placed, users can cancel the order through the virtual assistant. First, define an entity that can recognize order numbers. Then, add an intent that recognizes when users want to cancel a cake order.
Adding an order number pattern entity
You want the assistant to recognize order numbers, so you create a pattern entity to recognize the unique format that the restaurant uses to identify its orders. The syntax of order numbers that are used by the restaurant's bakery is two uppercase
letters followed by 5 numbers. For example, YR34663
. Add an entity that can recognize this character pattern.
-
Click My entities to open the Entities page.
-
Click Create entity.
-
Enter
order_number
into the entity name field. -
Click Create entity.
-
Add
order_syntax
to the Value name field. -
Click the down arrow next to Synonyms to change the type to Patterns.
-
Add the following regular expression to the Pattern field:
[A-Z]{2}\d{5}
-
Click Add value.
-
Click the Close arrow to finish adding the
@order_number
entity.
Add a cancel order intent
-
On the Intents page, click Create intent.
-
Add the following intent name, and then click Create intent:
cancel_order
-
In the Add user examples field, enter the following utterances:
I want to cancel my cake order I need to cancel an order I just placed Can I cancel my cake order? I'd like to cancel my order There's been a change. I need to cancel my bakery order. please cancel the birthday cake order I placed last week The party theme changed; we don't need a cake anymore that order i placed, i need to cancel it.
-
Click the Close arrow to finish adding the
#cancel_order
entity.
Add a yes intent
Before you complete a task on the user's behalf, you must get confirmation that you are taking the proper action. Add a #yes intent to the dialog that can recognize when a user agrees with what your assistant is proposing.
-
On the Intents page, click Create intent.
-
Add the following intent name, and then click Create intent:
yes
-
In the Add user examples field, enter the following utterances:
Yes Correct Please do. You've got it right. Please do that. that is correct. That's right yeah Yup Yes, I'd like to go ahead with that.
-
Click the Close arrow to finish adding the
#yes
intent.
Add dialog nodes that can manage requests to cancel an order
Now, add a dialog node that can handle requests to cancel a cake order.
-
Click Dialog to open the dialog tree.
-
Find the #menu node in the dialog tree.
-
Click the Node options icon on the #menu node, and then select Add node below.
-
Start typing
#cancel_order
into the If assistant recognizes field of this node, then select it from the list. -
In Assistant responds, enter the text response:
If the pickup time is more than 48 hours from now, you can cancel your order.
Before you can cancel the order, you need to know the order number. The user might specify the order number in the original request. So, to avoid asking for the order number again, check for a number with the order number pattern in the original input. To do so, define a context variable that would save the order number if it is specified.
-
In Assistant responds, click the Options icon icon, and then select Open context editor.
-
Set the context by using these values:
Order number context variable details Variable Value $ordernumber
<? @order_number.literal ?>
The context variable value (
<? @order_number.literal ?>
) is a SpEL expression that captures the number that the user specifies that matches the pattern that is defined by the @order_number pattern entity. It saves it to the$ordernumber
variable. -
Click the Close icon to close the node edit view.
Now, add child nodes that either ask for the order number or get confirmation from the user that they want to cancel an order with the detected order number.
-
Click the Node options icon on the #cancel_order node, and then select Add child node.
-
Add a label to the node to distinguish it from other child nodes you are adding. In the Enter node name field, add
Ask for order number
. -
In If assistant recognizes, enter
true
as the condition. -
In Assistant responds, enter the text response:
What is the order number?
-
Click the Close icon to close the node edit view.
Now, add another child node that informs the user that you are canceling the order.
-
Click the Node options icon on the Ask for order number node, and then select Add child node.
-
In If assistant recognizes, enter
@order_number
as the condition. -
In Assistant responds, click the Options icon icon, and then select Open context editor.
-
Set the context by using these values:
Order number context variable details Variable Value $ordernumber
<? @order_number.literal ?>
-
In Assistant responds, enter the text response:
OK. The order $ordernumber is canceled. We hope we get the opportunity to bake a cake for you sometime soon.
-
Click the Close icon to close the node edit view.
Add another node to capture the case where a user provides a number, but it is not a valid order number.
-
Click the Node options icon on the @order_number node, and then select Add node below.
-
In If assistant recognizes, enter
true
as the condition. -
In Assistant responds, enter the text response:
I need the order number to cancel the order for you. If you don't know the order number, please call us to cancel over the phone.
-
Click the Close icon to close the node edit view.
Add a node after the initial order cancellation request node that responds in the case where the user provides the order number in the initial request, so you don't have to ask for it again.
-
Click the Node options icon on the #cancel_order node, and then select Add child node.
-
Add a label to the node to distinguish it from other child nodes. In the Enter node name field, add
Number provided
. -
In If assistant recognizes, enter
@order_number
as the condition. -
In Assistant responds, enter the text response:
Just to confirm, you want to cancel order $ordernumber?
-
Click the Close icon to close the node edit view.
You must add child nodes that check for the user's response to your confirmation question.
-
Click the Node options icon on the Number provided node, and then select Add child node.
-
In If assistant recognizes, enter
#yes
as the condition. -
In Assistant responds, enter the text response:
OK. The order $ordernumber is canceled. We hope we get the opportunity to bake a cake for you sometime soon.
-
Click the Close icon to close the node edit view.
-
Click the Node options icon on the #yes node, and then select Add node below.
-
In If assistant recognizes, enter
true
as the condition.
Don't add a response to Assistant responds. Instead, you redirect users to the branch that asks for the order number details that you created earlier.
-
In Then assistant should choose Jump to.
-
Select the Ask for order number node.
-
Choose If assistant recognizes (condition).
-
Click the Close icon to close the node edit view.
Force the conversation to evaluate the child nodes under the #cancel_order node at run time.
-
Click to open the
#cancel_order
node in the edit view. -
In Then assistant should select
Skip user input
. -
Click the Close icon to close the node edit view.
Test order cancellations
Test whether your assistant can recognize character patterns that match the pattern that is used for product order numbers in user input.
-
Click Try it.
-
Type
i want to cancel my order number TW12345.
Your assistant recognizes both the
#cancel_order
intent and the@order_number
entity. It responds with,If the pickup time is more than 48 hours from now, you can cancel your order. Just to confirm, you want to cancel order TW12345?
. -
Type
Yes
.Your assistant recognizes the
#yes
intent and responds with,OK. The order TW12345 is canceled. We hope we get the opportunity to bake a cake for you sometime soon.
.Now, try it when you don't know the order number.
-
Click Clear in the "Try it out" pane to start over.
-
Type
I want to cancel my order.
Your assistant recognizes the
#cancel_order
intent, and responds with,If the pickup time is more than 48 hours from now, you can cancel your order. What is the order number?
. -
Enter,
I don't know.
Your assistant responds with,
I need the order number to cancel the order for you. If you don't know the order number, please call us to cancel over the phone.
.
Add nodes to clarify the order number format
If you do more testing, you might find that the dialog isn't helpful in scenarios where the user does not remember the order number format. The user might include only the numbers or the letters too, but forget that they are meant to be uppercase. So, it would be a nice touch to give them a hint in such cases, correct? If you want to be kind, add another node to the dialog tree that checks for numbers in the user input.
-
Click the Node options icon on the @order_number node, and then select Add node below.
-
In If assistant recognizes, enter
input.text.find('\d')
as the condition. -
In the condition field, add
input.text.find('\d')
, which is a SpEL expression that says if you find one or more numbers in the user input, trigger this response. -
In Assistant responds, enter the text response:
The correct format for our order numbers is AAnnnnn. The A's represents 2 uppercase letters, and the n's represent 5 numbers. Do you have an order number in that format?
-
Click the Close icon to close the node edit view.
-
Click the Node options icon on the input.text.find('\d') node, and then select Add child node.
-
In If assistant recognizes, enter
true
as the condition. -
Click Customize, set the Multiple conditioned responses switch to On, and then click Apply.
-
In **Assistant responds, add responses with the following conditions:
Condition details If assistant recognizes Assistant responds Then assistant should @order_number
OK. The order $ordernumber is canceled. We hope we get the opportunity to bake a cake for you sometime soon.
Default to node settings true
I need the order number to cancel the order for you. If you don't know the order number, please call us to cancel over the phone.
-
Click the Close icon to close the node edit view.
Now, when you test, you can provide a set of numbers or a mix of numbers and text as input, and the dialog reminds you of the correct order number format. You tested your dialog, found a weakness in it, and corrected it.
Another way that you can address this type of scenario is to add a node with slots. See the Adding a node with slots to a dialog tutorial to learn more about using slots.
Add the personal touch
If the user shows interest in the bot itself, you want the virtual assistant to recognize that curiosity and engage with the user in a more personal way. You might remember the #General_About_You
intent, which is provided with the
General content catalog, that we considered using earlier before you added your own custom #about_restaurant
intent. It is built to recognize such questions from the user. Add a node that conditions on this intent. In
your response, you can ask for the user's name and save it to a $username variable that you can use elsewhere in the dialog, if available.
Add a node that handles questions about the bot
Add a dialog node that can recognize the user's interest in the bot, and respond.
-
Click Dialog to open the dialog tree.
-
Click the Node options icon on the Welcome node, and then select Add node below.
-
Start typing
#General_About_You
in the If assistant recognizes field, and then select it from the list. -
In Assistant responds, enter the text response:
I am a virtual assistant that is designed to answer your questions about the Truck Stop Gourmand restaurant. What should I call you?
-
Click the Close icon to close the node edit view.
-
Click the Node options icon on the #General_About_You node, and then select Add child node.
-
In If assistant recognizes, enter
true
as the condition. -
In Assistant responds, enter the text response:
Hello, <? input.text ?>! It's lovely to meet you. How can I help you today?
To capture the name that the user provides, add a context variable to the node.
-
In Assistant responds, click the options menu and select Open context editor.
-
Set the context by using these values:
Set context Variable Value $username
"<? input.text ?>"
The context variable value (
<? input.text ?>
) is a SpEL expression that captures the username as it is specified by the user, and then saves it to the$username
context variable. -
Click the Close icon to close the node edit view.
If at run time, the user triggers this node and provides a name, then you record the user's name. If you know it, you should use it! Add conditional responses to the greeting dialog node that you added previously to include a conditional response that uses the username, if it is known.
Add the username to the greeting
If you know the user's name, you should include it in your greeting message. To do so, add conditional responses, and include a variation of the greeting that includes the user's name.
-
Find the Greet customers node in the dialog tree, and click to open it in the edit view.
-
Click Customize, set the Multiple conditioned responses switch to On, and then click Apply.
-
Click Add response.
-
In If assistant recognizes, enter
$username
. -
In Respond with, enter a text response:
Good day to you, $username!
-
Click the Move up arrow to reorder the two response types.
-
Click the Close icon to close the node edit view.
Test personalization
Test whether your assistant can recognize and save a user's name, and then refer to the user by it later.
-
Click Try it.
-
Click Clear to restart the conversation session.
-
Enter
Who are you?
Your assistant recognizes the
#General_About_You
intent. Its response ends with the question,What should I call you?
. -
Enter
Jane
.Your assistant saves
Jane
in the$username
variable and respondsHello, Jane! It's lovely to meet you. How can I help you today?
. -
To test the
Greet customers
node, enterHello.
.Your assistant recognizes the
Greet customers
intent and says,Good day to you, Jane!
. It uses the conditional response that includes the user's name because the$username
context variable contains a value at the time that the greeting node is triggered.
You can add a conditional response that conditions on and includes the user's name for any other responses where personalization would add value to the conversation.
Summary
You created a more complex dialog that reacts to questions and requests from the customer.