Session 7 Integrating Conversational Solution Lab Instructions 181112

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 42

DownloadSession 7 - Integrating Conversational Solution Lab Instructions 181112
Open PDF In BrowserView PDF
Session 7: Integrating

IBM Watson Assistant

Lab Instructions

Laurent Vincent

Content
Let’s get started ........................................................................................................................ 3
1.

Overview ...................................................................................................................... 3

2.

Objectives .................................................................................................................... 3

3.

Prerequisites ................................................................................................................ 4

4.

Scenario ....................................................................................................................... 4

5.

What to expect when you are done............................................................................. 5

Architecture ............................................................................................................................... 7
6.

Overview ...................................................................................................................... 7

7.

Project structure ........................................................................................................... 7

Chatbot application in Node-Red ............................................................................................ 8
8.

Create the Node-Red application in Bluemix .............................................................. 8

9.

Create the Chatbot application flow with the Node-Red flow editor. ........................ 10

10.

Configure the Message Conversation API call ...................................................... 12

11.

Test your settings ................................................................................................... 14

Context variables .................................................................................................................... 17
12.

Maintaining state ..................................................................................................... 17

13.

Public Context Variables ........................................................................................ 21

Private Context Variables ...................................................................................................... 23
14.

Storing a user input................................................................................................. 23

15.

Augmenting a response.......................................................................................... 25

16.

Review the stored Data .......................................................................................... 26

Call an external API (Tone Analyzer) .................................................................................... 32
17.

Configure the Tone Analyzer service and node .................................................... 32

18.

Update Dialog ......................................................................................................... 34

19.

Test it ...................................................................................................................... 36

Enrich user inputs with Natural Language Understanding (NLU) .................................... 38
20.

Configure the NLU service and node ..................................................................... 38

21.

Test it ...................................................................................................................... 40

Page 2 of 42

Let’s get started
1. Overview
The IBM Watson Developer Cloud (WDC) offers a variety of services for developing
cognitive applications. Each Watson service provides a Representational State
Transfer (REST) Application Programming Interface (API) for interacting with the
service. Some services, such as the Speech to Text service, provide additional
interfaces.
The Watson Conversation service combines several cognitive techniques to help you
build and train a bot - defining intents and entities and crafting dialog to simulate
conversation. The system can then be further refined with supplementary technologies
to make the system more human-like or to give it a higher chance of returning the right
answer. Watson Conversation allows you to deploy a range of bots via many channels,
from simple, narrowly focused bots to much more sophisticated, full-blown virtual
agents across mobile devices, messaging platforms like Slack, or even through a
physical robot.
The illustrating screenshots provided in this lab guide could be slightly different from
what you see in the Watson Assistant service interface that you are using. If there are
colour or wording differences, it is because there have been updates to the service
since the lab guide was created.

2. Objectives
This chapter describes how to integrate a chatbot application quickly without coding
and integrate it with the Watson Assistant service. For this use case example, you
continue with your chatbot, however you can customize the chatbot to take any other
role such as delivery service, Q&A, student assistant, and more.
To create the chatbot application, you use the Node-RED programming tool. With this
powerful tool you can create, edit, and deploy applications quickly. Node-RED is a
programming tool for wiring together hardware devices, APIs and online services in
new and interesting ways. It provides a browser-based editor that makes it easy to
wire together flows using the wide range of nodes in the palette that can be deployed
to its runtime in a single-click.
Node-RED, created by IBM but now part of JS Foundation, provides full integration
with Watson APIs, allowing you to make great applications quickly and easy.
The following topics are covered in this chapter:
Getting started
Architecture
Step-by-step implementation
Quick deployment of application

Page 3 of 42

3. Prerequisites
Before you start the exercises in this guide, you will need to complete the session
building a dialog.
Bluemix URLs per location:
Location

URL

US

https://console.ng.bluemix.net/

UK

https://console.eu-gb.bluemix.net/

Sidney

https://console.au-syd.bluemix.net/

Germany

https://console.eu-de.bluemix.net/

4. Scenario
Use case:

A Hotel Concierge Virtual assistant that is accessed from the guest room
and the hotel lobby.

End-users: Hotel customers

Page 4 of 42

5. What to expect when you are done
At the end of session, you should have a Node-Red flow to simulate your chatbot
application.

Page 5 of 42

and following dialog

Page 6 of 42

Architecture
6. Overview

Notice that the flow shown in the figure represents one loop of a conversation,
therefore this cycle repeats several times during a conversation:
1. The user sends a message to the web front-end (chatservice).
2. The chat service (for example, Slack, Facebook Messenger, webapp)
determines whether the message is for the Assistant chatbot application. If the
message is for the chatbot, then the chat service sends the message to your
chatbot application (Node-RED).
3. Your application parses the message and sends the filtered message to the
Watson Conversation service for processing.
4. The Watson Conversation service processes the message and provides a
response.
5. The response is received and filtered by your application, which then sends the
response to the chat service.
6. The chat service identifies that the input share from the Assistant chatbot and
presents the message as a response from the chatbot to the user.

7. Project structure
These are the components you use in this use case:
•
•
•

A Node-RED instance that is created in Bluemix, which is cloud-based, so
installing software is not necessary
A Watson Conversation service instance
Watson Tone Analyser and Natural Language Understanding services

Page 7 of 42

Chatbot application in Node-Red
Node-RED is a useful tool to create applications without having to write code. Instead,
it uses simple visual components that you configure and connect.
To make this task even easier, you do not need to install Node-RED, because it is
available in Bluemix. In this section, you create a Node-RED application Bluemix and
configure the flow.
•
•
•

Create the Node-RED application in Bluemix
Create the chatbot application flow with the Node-RED flow editor
Configure the chatbot application in Node-RED

8. Create the Node-Red application in Bluemix
1. Go to the IBM Cloud Catalog.
2. In the Catalog, go to > Starter Kits and click on Node-Red Starter.

3. Enter the name of your application and host as Nodered-Conversation-XXXX-YY.
Replace XXX with the date of the day and YY with your initials. Accept the default
values for the remaining fields and click on Create

Note: Wait until the application is created and it is started. The application status
should be Running before you can proceed.
Page 8 of 42

4. Once the application is Running, Click Visit App URL (on the right of the status)

5. Click Next for each step of the service creation (don’t secure your editor)
6. Click Go to your Node-Red flow Editor
Note: When you first run this application, you are presented with some options to
secure the Node-RED flow editor with a username and password. Securing the editor
is optional but it is a good practice to do so. Skip through optional windows for this
example until you get to the window below.

Page 9 of 42

9. Create the Chatbot application flow with the Node-Red
flow editor.
The Node-RED flow editor opens. The panel on the left shows a palette of nodes that
you can drag to the workspace. You can connect them together (wire them) to create
an application. After dragging a node to a workspace, you can double-click the node
to open the Edit (configuration) dialog to provide values for the node.

Now you can start to create flows. You use the Node-Red flow editor to add node and
Values and create and wire flows.
On the IBM box folder, open conversation-nodered-project file with any text editor.
1. Copy the content of this files to your clipboard

Page 10 of 42

2. To import the nodes, click hamburger menu at the top-right and select Import ->
Clipboard.

3. Paste your content in the window.

Now your flow is ready to use, you must edit the nodes and add the authentication
values based on your Conversation service instance credentials, workspace ID.

Page 11 of 42

10. Configure the Message Conversation API call
Below the nodes which manages the call to your Conversation service:

1. Double click on Conversation Call.
As describe in the API documentation here
You are going to use
Method : POST
URL : https://gateway.watsonplatform.net/assistant/api/v1/workspaces/
/message?version=2018-07-10
Username : apikey
Password : 
2. Set the properties according to your inputs, select “Use basic authentication”
option.

Page 12 of 42

you can retrieve your WorkspaceID, Username and Password on the Credentials page
of your Assistant Service.

3. Click Done to save your node updates (top right)
4. Click Deploy to save your flow updates (top right)

To understand the data exchanged between your chatbot application and
Conversation Service, you are going to use the debug pane.
5.

Click on Hamburger menu (top right), Select View / Debug messages.

Then the debug is ready to use:

Page 13 of 42

11. Test your settings
For test purpose a HTML page have been embedded in your Node-Red Flow

1. DoubleClick on the HTML node, it gives you the extension of the URL you will use
in your browser.

Note: So the Final URL to access to the chatbot UI should be the aggregation of
your node red application and the extension defined in the HTML node:
https://.mybluemix.net/watson-chatbot
it means :
https://nodered-conversation-XXXX-YY.eu-gb.mybluemix.net/watson-chatbot
2. Click Cancel to close the window
3. Use the URL in your browser to access to a simple page to enter your inputs:

Page 14 of 42

4. Click Submit
As you don’t provide any input, the service returns the “Start of the conversation”
responses:

5. Enter Hi and try to enter some other inputs
You should get the same responses from the service

Page 15 of 42

Go back to your Node-Red application and review the information displayed in the
debug panel.

The Conversation service is detecting the correct intents, and yet every turn of the
conversation returns the welcome message from the start of conversation node
(Good Afternoon! I am Watson, …).
This is happening because the Conversation service is stateless; it is the responsibility
of the application to maintain state information. Because you are not yet doing
anything to maintain state, the Conversation service sees every round of user input as
the first turn of a new conversation, triggering the conversation_start condition.

Page 16 of 42

Context variables
State information for your conversation is maintained using the context. The context
is a JSON object that is passed back and forth between your application and the
Conversation service. It is the responsibility of your application to maintain the context
from one turn of the conversation to the next.
The context includes a unique identifier for each conversation with a user, as well as
a counter that is incremented with each turn of the conversation. Our previous version
of the example did not preserve the context, which means that each round of input
appeared to be the start of a new conversation. We can fix that by saving the context
and sending it back to the Conversation service each time.
In addition to maintaining our place in the conversation, the context can also be used
to store any other data you want to pass back and forth between your application and
the Conversation service. This can include persistent data you want to maintain
throughout the conversation (such as a customer's name or account number), or any
other data you want to track (such as the current status of option settings).

12. Maintaining state
The only change from the previous example is that with each round of the
conversation, we now send back the context object we received in the previous round:

1. DoubleClick on Pre Processing
The application manages store the context in global variable payloadRed. This object
is populated during the first turn of the conversation service.

Page 17 of 42

2. Uncomment the line 13, “Context” : lpayload.context,
3. Click Done, Click Deploy.

4. To reset the context, Click reset context inject node.

Page 18 of 42

5. Go back to the Web Form, Click Submit
As you don’t provide any input, the service returns the “Start of the conversation”
responses:

6. Enter Hi inputs
You should get the right responses from the service

Page 19 of 42

7. Go back to your Node-Red application and review the information displayed in the
debug pane.

So now, it works as expected.

Page 20 of 42

13. Public Context Variables
To illustrate the way to manage context variable, you are going to use the Hotel
Amenities Management of your dialog. To manage it, you are using the entity
@hotel_amenity and the context variable $hotel_amenity.

When you enter where is it?, Conversation needs more details about the amenity you
are looking for. So,if you are doing some tests you will get this behavior:

That’s because $hotel_amenity variable must be captured by Assistant. Such an
information could be stored in your profile preference for instance and could be
populated by your chatbot application.
You will simulate the acquisition of $hotel_amenity and set up to hotel restaurant.
1. Go back to your Node-Red application
2.

DoubleClick on Set restaurant variable

Page 21 of 42

the global variable is updated like defined below:

3. Click Cancel button
4. To update the variable, click reset context inject node
5. click set context variable inject node.
6. On the chat window, click submit then enter where is it?
The variable has been set correctly and you should get the following behaviour.

Page 22 of 42

Private Context Variables
An application must be based around the idea of managing two sets context
information, public and private
Public Context is the context object that is sent to Watson Conversation as part of
the request. It will be available inside of Watson Conversation through the standard
API and will be visible in plain text through the application.
Private Context is maintained in the application's memory and is never sent to
Watson Conversation. This makes it appropriate for data that may be sensitive, or
simply if it's not important to send to Watson Conversation.
Both public and private context can be used to store information to use in API calls as
well as to augment the response to the user.

14. Storing a user input
Sometimes a developer will need to store a user's next response, for instance, Watson
may ask the user a question and need to store that information for later. Your NodeRed application allows for this situation to be quickly and simply addressed with the
following syntax on Watson Assistant.
{
"output": {
"generic": {…},
"updatesContext": {
"value": "name",
"context": "private"
}
}
}
or
{
"output": {
"generic": {…},
"updatesContext": {
"value": "name",
"context": "public"
}
}
}
The presence of this property will indicate to the application that the next response
from the user will be stored in the variable name as public or private context.
1. Go back to your Conversation service and open Dialog tab
2. Add a new node before the anything else node.

Page 23 of 42

3. Fill it as below:

Copy of the response:
{
"output": {
"generic": [
{
"response_type": "text",
"values": [
{
"text": "Ok. What should I call you?"
}
],
"selection_policy": "sequential"
}
],
"updatesContext": {
"value": "name",
"context": "private"
}
}
}

Page 24 of 42

15. Augmenting a response
For the chatbot to truly be dynamic, it's not enough for the bot to simply call external
APIs and internal functions, but it needs to tailor its response based on the information
retrieved in these integrations. Since each API call requires that it returns
a Promise that will update context and privateContext, we need a way to access this
information quickly.
The Node-Red application allows you to include the following syntax {{fieldName}} in
their responses. The application will update these references and replace them with
the first matching option from the ordered list:
•
•
•

publicContext.fieldName ,
privatecontext.fieldName
or keep the placeholder indicating the value was not found.

1. Select Update Profile node, and add a child node
2. Fill it as below:

Copy of the response:
Thanks {{name}}. I'll use that from now on.
Storing a user's next response as a context or privateContext field just takes 1 dialog
node. In this example, we'll confirm that the value was stored with the second node.

Page 25 of 42

16. Review the stored Data
1. In the chatbot window, enter update my profile
2. Conversation will ask you to provide your new name, enter the one you want to
use.
3. enter your name
You should get something like below:

Page 26 of 42

4. Let’s go to improve page of your Assistant service
5.

Select the right data source (ChabotProduction alias is not used in Nodered)

6.

Select User Conversations tab and select #update_profile intent as filter

7. To review the saved data, click Open conversation of the latest record

You have got the confirmation that the variable name is not captured by Conversation.

Page 27 of 42

On the debug pane of your Node-Red application, you can see that
the data captured is laurent
but the data sent to conversation is empty

Page 28 of 42

8. You can test the application behaviour if the variable is public. In the dialog page,
edit the node Update Profile
9. Open the Json Editor
10. Replace private with public

11. Repeat the previous test, and review the collected data.
The behaviour doesn’t change from the user point of view

Conversation capture the provided name

Page 29 of 42

You should get the log details as below.

Page 30 of 42

Page 31 of 42

Call an external API (Tone Analyzer)
In addition to the output text to be displayed to the user, our dialog uses the output
object in the response JSON to signal when the application needs to carry out an
action, based on the detected intents.
These action flags are sent using the action property, which our dialog defines as part
of the response JSON. When the dialog determines that the application needs to do
something, it sets the value of action to the appropriate value in your case a call to
the Tone Analyzer API.
Keep in mind that output is just a JSON object, and you can add any valid content to
it. For a more complex application, you might use an array with multiple action flags.
But in our example, we're using a simple key/value pair that supports a single action
flag. Our application code needs to check the value of the action property in the
response and then carry out any specified action.

17. Configure the Tone Analyzer service and node
1.

Go back to the IBM Cloud catalog,

2.

Look for Tone Analyzer, Click on Tone Analyzer tile

3. Set a name and keep United Kingdom as region, hen click Create

Page 32 of 42

4. Copy the credentials username and password on your clipboard.
5. Go back to your Node-Red application
6. Doubleclick on Tone Analyser node

7. Fill the username and password with the values of your Tone Analyzer service
8. Click Done, then Deploy
Your Tone Analyzer is ready!

Page 33 of 42

18. Update Dialog
You are going to create 2 nodes.
The first captures the #feedback intent and request a call to action
the second displays the value $emotion with the best confidence rate returned
by Tone Analyzer.

1. Create the branch as described above, you can get the details of each node below:
First node

Page 34 of 42

Copy of the response:
{
"output": {
"generic": [
{
"values": [],
"response_type": "text",
"selection_policy": "sequential"
}
],
"CallToneAnalyser": {
"emotion": "public"
}
}
}
Second node

Copy of the response:
I understood you are $emotion!

Page 35 of 42

19. Test it
You can work with this new integration:
1.

On the Chatbot form enter successively the following sentences:
it's really horrendous how badly these nodes are documented - There is absolutely
no way anybody can follow that! Infuriating!
I'm finding it very difficult to make any of this work
That’s the great implementation, and I am impatient to start working with

Watson!
You should get:

Tone analyzer returns successively:
$emotion = impolite
$emotion = sad
$emotion = satisfied

Page 36 of 42

The debug pane should display for the last utterance

Page 37 of 42

Enrich user inputs with Natural
Language Understanding (NLU)
In addition to the input text provided by the user, it could be useful to extract entities,
keywords which cannot be identify directly by Watson conversation Service.
To do this , you are going to use NLU to extract entities, keywords, document emotion
or language. It will be the opportunity to enrich variable context.

20. Configure the NLU service and node
1. Go back to the IBM Cloud catalog,
2. Look for Natural Language Understanding, click on the tile
3. Set the name of the service, keep United Kingdom as region
4. Click Create
5. Copy the Credentials username and password on your clipboard.
6. Go back to your Node-Red application
7. Doubleclick on NLU Pre Proc node

8. Fill the username and password with the values of your Tone Analyser service
9. Click Done, then Deploy
10. On the Main stream, select the link between Input and Pre Processing nodes

11. Delete it
Page 38 of 42

12. Create a link between Input and link nodes

That the way to add NLU in pre-processing of the user input

Now your new component is ready to use.

Page 39 of 42

21. Test it
You are using input not manage by Watson conversation. Right now, you are just
going to review the information provided by NLU and which could be leverage during
a conversation.
1. On the Chatbot form enter the following sentence:
London could be the place to be
In the debug panel, you should get the entity location London and the language en
(English):

Page 40 of 42

2. On the Chatbot form enter the following sentence:
Londres est le lieu à la mode
In the debug panel, you should get the entity location Londres and the language fr
(French):

Page 41 of 42

3. On the Chatbot form enter the following sentence:
München ist eine modische Stadt
In the debug panel, you should get the entity location München (Munich) and the
language de (german):

It is an illustration of the capabilities , in pre-processing of WCS, to :
•

determine the language and then call the right workspace,

•

extract information which could be useful to manage the conversation.

Page 42 of 42



Source Exif Data:
File Type                       : PDF
File Type Extension             : pdf
MIME Type                       : application/pdf
PDF Version                     : 1.7
Linearized                      : No
Page Count                      : 42
Language                        : en-US
Tagged PDF                      : Yes
XMP Toolkit                     : 3.1-701
Title                           : 
Creator                         : ADMINIBM
Creator Tool                    : Microsoft Word
Create Date                     : 2018:11:15 05:08:54+00:00
Modify Date                     : 2018:11:15 05:08:54+00:00
Document ID                     : uuid:DC54F30B-6DEA-4B70-AC4B-D17295E052A7
Instance ID                     : uuid:DC54F30B-6DEA-4B70-AC4B-D17295E052A7
Author                          : ADMINIBM
EXIF Metadata provided by EXIF.tools

Navigation menu