Documentation for the classic Watson Assistant experience has moved. For the most up-to-date version, see Building a complex dialog.
Building a complex dialog
In this tutorial, you will use the Watson Assistant service to 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 will understand 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 will take approximately 2 to 3 hours to complete.
Prerequisite
Before you begin, complete the Getting Started tutorial.
You will use the dialog skill 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 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'll 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 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 will add your own intent.
Add the #about_restaurant intent
-
From the Intents tab, click Create intent.
-
Enter
about_restaurant
in the Intent name field, and then click Create intent. -
Add the following user examples:
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 icon to finish adding the
#about_restaurant
intent.
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 the Dialog tab.
-
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 More icon on the
#General_Greetings
node, and then select Add node below. -
Start to type
#about_restaurant
into the If assistant recognizes field of this node. Then select the#about_restaurant
option. -
Add the following text as the response.
To copy the text, click the copy icon that is associated with the text block:
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.
-
Let's 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 the image response type up, so it is displayed in the response before the text is displayed. Click the Move up arrow to reorder the two response types.
-
Click to close the edit view.
Test the #about_restaurant dialog node
Test the intent by checking whether user utterances that are similar to, but not exactly the same as, the examples you added to the training data have successfully trained your assistant to recognize input with an #about_restaurant
intent.
-
Click the icon to open the "Try it out" pane.
-
Enter,
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.
Congratulations! You have added a custom intent, and a dialog node that knows how to handle it.
The #about_restaurant
intent is designed to recognize a variety of 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:
- The restaurant owners
- The restaurant history
- The philosophy
- The number of sites
- The days of operation
- The meals served
- The fact that the restaurant bakes cakes to order
For general, low-hanging fruit types of questions, a single, general answer is suitable.
Manage cake orders
Customers 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 will create a pattern entity to recognize the unique format that the restaurant uses to identify its orders. The syntax of order numbers 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 the Entities tab.
-
Click Create entity.
-
Enter
order_number
into the entity name field. -
Click Create entity.
-
Add
order_syntax
to the Value name field, and then 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 icon to finish adding the
@order_number
entity.
Add a cancel order intent
-
Click the Intents tab.
-
Click Create intent.
-
Enter
cancel_order
in the Intent name field, and then click Create intent. -
Add the following user examples:
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 icon to finish adding the
#cancel_order
intent.
Add a yes intent
Before you perform an action 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.
-
Click the Intents tab.
-
Click Create intent.
-
Enter
yes
in the Intent name field, and then click Create intent. -
Add the following user examples:
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 icon 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 the Dialog tab.
-
Find the
#menu
node. Click the More icon on the#menu
node, and then select Add node below. -
Start to type
#cancel_order
into the If assistant recognizes field of this node. Then select the#cancel_order
option. -
Add the following message in the response text field:
If the pickup time is more than 48 hours from now, you can cancel your order.
Before you can actually 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.
-
You define a context variable in the context editor. From the response section of the node, click the More icon, and then select Open context editor.
-
Enter the following context variable name and value pair:
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 defined by the @order_number pattern entity. It saves it to the$ordernumber
variable. -
Click to close the edit view.
Now, add child nodes that either ask for the order number or get confirmation from the user that she wants to cancel an order with the detected order number.
-
Click the More 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 will be adding. In the name field, add
Ask for order number
. Typetrue
into the If assistant recognizes field of this node. -
Add the following message in the response text field:
What is the order number?
-
Click to close the edit view.
Now, add another child node that informs the user that you are canceling the order.
-
Click the More icon on the
Ask for order number
node, and then select Add child node. -
Type
@order_number
into the If assistant recognizes field of this node. -
Open the context editor. Click the More icon, and select Open context editor.
-
Enter the following context variable name and value pair:
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 defined by the @order_number pattern entity. It saves it to the$ordernumber
variable. -
Add the following message in the response text field:
OK. The order $ordernumber is canceled. We hope we get the opportunity to bake a cake for you sometime soon.
-
Click to close the 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 More icon on the
@order_number
node, and then select Add node below. -
Type
true
into the If assistant recognizes field of this node. -
Add the following message in the response text field:
I need the order number to cancel the order for you. If you don't know the order number, please call us at 958-234-3456 to cancel over the phone.
-
Click to close the 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 More 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 name field, add
Number provided
. Type@order_number
into the If assistant recognizes field of this node. -
Add the following message in the response text field:
Just to confirm, you want to cancel order $ordernumber?
-
Click to close the edit view.
You must add child nodes that check for the user's response to your confirmation question.
-
Click the More icon on the
Number provided
node, and then select Add child node. -
Type
#yes
into the If assistant recognizes field of this node. -
Add the following message in the response text field:
OK. The order $ordernumber is canceled. We hope we get the opportunity to bake a cake for you sometime soon.
-
Click to close the edit view.
-
Click the More icon on the
#yes
node, and then select Add node below. -
Type
true
into the If assistant recognizes field of this node.Do not add a response. Instead, you will redirect users to the branch that asks for the order number details that you created earlier.
-
In the And finally section, choose Jump to.
-
Select the Ask for order number node's condition.
-
Click to close the edit view.
-
Move the Number provided node before the Ask for order number node. Click the More icon on the
Number provided
node, and then select Move. Select the Ask for order number node, and then click Above node. -
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, and then, in theAnd finally
section, selectSkip user input
.
Test order cancellations
Test whether your assistant can recognize character patterns that match the pattern used for product order numbers in user input.
-
Click the icon to open the "Try it out" pane.
-
Enter,
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?
-
Enter,
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. Enter,
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 at 958-234-3456 to cancel over the phone.
Add nodes to clarify order number format
If you do more testing, you might find that the dialog isn't very 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.
-
Find the
@order_number
node that is a child of the Ask order number node. -
Click the More icon on the
@order_number
node, and then select Add node below. -
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 the text response field, add the following 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 to close the edit view.
-
Click the More icon on the
input.text.find('\d')
node, and then select Add child node. -
Type
true
into the If assistant recognizes field of this node. -
Enable conditional responses by clicking Customize, scrolling down, and then setting the Multiple conditioned responses switch to On.
-
Click Apply.
-
In the newly-added If assistant recognizes field, type
@order_number
, and in the Respond with field, type:OK. The order $ordernumber is canceled. We hope we get the opportunity to bake a cake for you sometime soon.
-
Click Add response.
-
In the If assistant recognizes field, type
true
, and in the Respond with field, type:I need the order number to cancel the order for you. If you don't know the order number, please call us at 958-234-3456 to cancel over the phone.
-
Click to close the edit view.
Now, when you test, you can provide a set of number or a mix of numbers and text as input, and the dialog reminds you of the correct order number format. You have successfully tested your dialog, found a weakness in it, and corrected it.
Another way 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 just 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 the Dialog tab.
-
Find the
Welcome
node in the dialog tree. -
Click the More icon on the
Welcome
node, and then select Add node below. -
Start to type
#General_About_You
into the If assistant recognizes field of this node. Then select the#General_About_You
option. -
Add the following message in the response text field:
I am a virtual assistant that is designed to answer your questions about the Truck Stop Gourmand restaurant. What should I call you?
-
Click to close the edit view.
-
Click the More icon on the
#General_About_You
node, and then select Add child node. -
In the If assistant recognizes field of this node, enter
true
. -
Add the following message in the response text field:
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. Click the More icon, and select Open context editor.
-
Enter the following context variable name and value pair:
User name context variable details Variable Value username <? input.text ?>
The context variable value (
<? input.text ?>
) is a SpEL expression that captures the user name as it is specified by the user, and then saves it to the$username
context variable. -
Click to close the edit view.
If, at run time, the user triggers this node and provides a name, then you will know the user's name. If you know it, you should use it! Add conditional responses to the greeting dialog node you added previously to include a conditional response that uses the user name, if it is known.
Add the user name 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
#General_Greetings
node in the dialog tree, and click to open it in the edit view. -
Click Customize, scroll down, and then set the Multiple conditioned responses switch to On.
-
Click Apply.
-
Click Add response.
-
In the If assistant recognizes field, type
$username
, and in the Respond with field, add a new response:Good day to you, $username!
-
Click the up arrow for response number 2 to move it so it is listed before response number 1 (
Good day to you!
). -
Click to close the 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 the icon to open the "Try it out" pane.
-
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. -
Enter,
Hello.
Your assistant recognizes the
#General_Greetings
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.
Test the assistant from your web page integration
Now that you have built a more sophisticated version of the assistant, return to the public web page that you deployed as part of the previous tutorial, and then test the new capabilities you added.
-
Open the assistant.
-
Click Preview.
-
Copy and paste the URL from Share this link into a web browser.
An IBM-branded page is displayed with your assistant embedded in it as a chat window.
-
Repeat a few of the test utterances that you submited to the "Try it out" pane to see how the assistant behaves in a real integration.
Unlike when you send test utterances to your assistant from the "Try it out" pane, standard usage charges apply to API calls that result from utterances that are submited to the chat widget.