{"id":215,"date":"2017-11-12T02:57:28","date_gmt":"2017-11-12T02:57:28","guid":{"rendered":"http:\/\/freedville.com\/blog\/?p=215"},"modified":"2019-04-12T14:06:10","modified_gmt":"2019-04-12T14:06:10","slug":"introduction-to-chat-bots-with-watson-conversation-service","status":"publish","type":"post","link":"http:\/\/freedville.com\/blog\/2017\/11\/12\/introduction-to-chat-bots-with-watson-conversation-service\/","title":{"rendered":"Introduction to chat bots with Watson Conversation Service"},"content":{"rendered":"<p><strong>Introduction<\/strong><\/p>\n<p>Chat bots are a powerful way to put a natural language interface in front of your applications and services, to help users easily and naturally get what they need.&nbsp; In this post I will walk through a sample Watson Conversation Service application and call out places where you will want to extend in the future.&nbsp; This is intended as a &#8220;my first chatbot primer&#8221; that prepares you for even more exciting applications.<\/p>\n<p><strong>Setting up the example<\/strong><\/p>\n<p>Let us pretend we are building a chatbot for a small retail operation in your town.&nbsp; As chatbot novices, we want to support questions on two topics: the hours and locations of our stores.&nbsp; We have two stores (one on Elm and one on Maple), and when possible we want to provide store-specific responses.&nbsp; Thus our chatbot needs to understand two dimensions of a user question, 1) is it an &#8220;hours&#8221; or &#8220;location&#8221; question, and 2) is the question about a specific store.<\/p>\n<p><strong>Step 0: Create an IBM Cloud account and Watson Conversation tile<\/strong><\/p>\n<p>Setting up an <a href=\"https:\/\/console.bluemix.net\/registration\/\">IBM Cloud account<\/a>, adding Watson Conversation from the catalog, and creating the conversation workspace is well covered by other tutorials, I will omit it from my example and instead link to a nice <a href=\"https:\/\/www.ibm.com\/blogs\/watson\/2016\/12\/build-chat-bot\/\">Watson Conversation tutorial<\/a>.<\/p>\n<figure id=\"attachment_233\" aria-describedby=\"caption-attachment-233\" style=\"width: 700px\" class=\"wp-caption alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-233\" src=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/watson-conversation-tile-700x298.png\" alt=\"\" width=\"700\" height=\"298\" srcset=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/watson-conversation-tile-700x298.png 700w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/watson-conversation-tile-300x128.png 300w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/watson-conversation-tile-768x327.png 768w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><figcaption id=\"caption-attachment-233\" class=\"wp-caption-text\">Creating a Watson Conversation instance<\/figcaption><\/figure>\n<p><strong>Step 1: Build the intents<\/strong><\/p>\n<p>Whenever the user sends input to our bot (in the parlance, an &#8220;utterance&#8221;), we want to understand the user means (the &#8220;intent&#8221;).&nbsp; I like this terminology, literally what is the&nbsp;<strong>intent<\/strong> of our user?&nbsp; In order to understand user intents, we need to train the system with examples of each of the intents we intend to handle.<\/p>\n<p>In the following diagram, you can see the two intents I created (#store_location and #store_hours), and the set of example questions for #store_hours.&nbsp; Note that the set is by no means exhaustive, this is a key feature of Watson technology, that the Conversation service can understand questions similar to (but not exactly matching) these questions.&nbsp; Note also the variation in questions, covering a reasonable subset of ways people ask about store hours.<\/p>\n<figure id=\"attachment_227\" aria-describedby=\"caption-attachment-227\" style=\"width: 700px\" class=\"wp-caption alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-227\" src=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-intents-hours-700x377.png\" alt=\"List of utterances supporting the store_hours intent\" width=\"700\" height=\"377\" srcset=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-intents-hours-700x377.png 700w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-intents-hours-300x161.png 300w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-intents-hours-768x413.png 768w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><figcaption id=\"caption-attachment-227\" class=\"wp-caption-text\">Our two intents with examples for store hour questions<\/figcaption><\/figure>\n<blockquote><p>Protip: Generally you should&nbsp;<strong>not<\/strong> invent sample questions, ideally you should get examples from real users in your system (from logs, mock interface, etc).&nbsp; Your training data needs to be representative of the data it will receive at runtime.<\/p><\/blockquote>\n<p>Watson Conversation service requires a minimum of five examples per intent.&nbsp; This is impressively low, remember&nbsp;<a href=\"http:\/\/freedville.com\/blog\/2016\/12\/15\/why-does-machine-learning-require-so-much-training-data\/\">machine learning requires a lot of data<\/a>, but five is enough to train a minimally testable model.&nbsp; (Ten examples per intent provides good results for your time investment &#8211; more examples are useful but provide diminishing returns.)&nbsp; After entering my training data I opened the testing interface, waited a few seconds for training to finish, and started testing my model.<\/p>\n<figure id=\"attachment_224\" aria-describedby=\"caption-attachment-224\" style=\"width: 700px\" class=\"wp-caption alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-224\" src=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-hours-intent-700x359.png\" alt=\"\" width=\"700\" height=\"359\" srcset=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-hours-intent-700x359.png 700w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-hours-intent-300x154.png 300w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-hours-intent-768x394.png 768w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><figcaption id=\"caption-attachment-224\" class=\"wp-caption-text\">Sample question and intent detection for #store_hours<\/figcaption><\/figure>\n<p>Note that the Conversation service correctly detects the intent of &#8220;What are your hours today?&#8221; as a &#8220;#store_hours&#8221; intent, even though it is not a direct match for any of the training example.<\/p>\n<figure id=\"attachment_229\" aria-describedby=\"caption-attachment-229\" style=\"width: 700px\" class=\"wp-caption alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-229\" src=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-intent-700x364.png\" alt=\"\" width=\"700\" height=\"364\" srcset=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-intent-700x364.png 700w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-intent-300x156.png 300w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-intent-768x399.png 768w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><figcaption id=\"caption-attachment-229\" class=\"wp-caption-text\">Sample location question and intent detection for #store_location<\/figcaption><\/figure>\n<p>Additionally, we detect &#8220;What are the directions to your store?&#8221; as a &#8220;#store_location&#8221; intent.&nbsp; Again, this question does not directly match any training data &#8211; Watson Conversation decided which intent the question was&nbsp;<strong>closest to<\/strong> based on its training.<\/p>\n<p><strong>Step 2: Detect entities<\/strong><\/p>\n<p>In the setup we noted two retail store locations &#8211; one on Elm and one on Maple.&nbsp; We&#8217;d like to be able to detect if users are referring to a specific store in their questions, so that we can give a specific response.&nbsp; We refer to these specifics as&nbsp;<strong>entities<\/strong>.&nbsp; Note that entities are orthogonal to intents, after all you could ask about hours or locations for Elm or Maple.&nbsp; When we detect an entity, we know we have a specific question, without one we assume a general question.<\/p>\n<p>In the next diagram, you see the defined entities for our example.&nbsp; We declare one type of entity (@street) with two possible variations: elm and maple.&nbsp; We also note a few equivalence patterns &#8211; the phrases &#8220;elm street&#8221;, &#8220;elm drive&#8221;, and &#8220;elm st&#8221; are all &#8220;informationally equivalent&#8221; to &#8220;elm&#8221;.<\/p>\n<figure id=\"attachment_219\" aria-describedby=\"caption-attachment-219\" style=\"width: 700px\" class=\"wp-caption alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-219 size-large\" src=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-entities-700x228.png\" alt=\"\" width=\"700\" height=\"228\" srcset=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-entities-700x228.png 700w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-entities-300x98.png 300w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-entities-768x250.png 768w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><figcaption id=\"caption-attachment-219\" class=\"wp-caption-text\">Definition of entity for our system<\/figcaption><\/figure>\n<p>We can go directly to the testing interface, adding entities does not require lengthy retraining.&nbsp; As you can see in the diagram below, the system detects &#8220;How do I get to the Elm Street&#8221; store as both &#8220;#store_location&#8221; intent and the &#8220;@street:elm&#8221; entity.<\/p>\n<figure id=\"attachment_223\" aria-describedby=\"caption-attachment-223\" style=\"width: 700px\" class=\"wp-caption alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-223\" src=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-entity-700x225.png\" alt=\"\" width=\"700\" height=\"225\" srcset=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-entity-700x225.png 700w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-entity-300x97.png 300w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-entity-768x247.png 768w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><figcaption id=\"caption-attachment-223\" class=\"wp-caption-text\">Detecting entity and intent in sample question<\/figcaption><\/figure>\n<p>Our training data never included samples with Elm Street but our system was able to understand both dimensions of the user utterance.<\/p>\n<p><strong>Step 3: Build dialog flow<\/strong><\/p>\n<p>The last step in our example is to provide appropriate responses to user utterances.&nbsp; In our past few steps we only verified intent and entity detection.&nbsp; Let&#8217;s write up some more useful responses.<\/p>\n<p>On the &#8220;Dialog&#8221; tab we create a new response node below the default &#8220;Welcome&#8221; node.&nbsp; The diagram below shows our store_hours node.&nbsp; In this node we define three things: 1) When does this node fire (on the &#8220;#store_hours&#8221; intent), 2) What is the response (or set of responses), and 3) What should the bot do next.&nbsp; For simplicity, we ignore any entities on hours questions.<\/p>\n<figure id=\"attachment_222\" aria-describedby=\"caption-attachment-222\" style=\"width: 700px\" class=\"wp-caption alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-222 size-large\" src=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-hours-700x493.png\" alt=\"\" width=\"700\" height=\"493\" srcset=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-hours-700x493.png 700w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-hours-300x211.png 300w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-hours-768x541.png 768w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-hours.png 1620w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><figcaption id=\"caption-attachment-222\" class=\"wp-caption-text\">Defining the store_hours dialog node<\/figcaption><\/figure>\n<p>We are able to get more clever on our responses.&nbsp; For location questions, I defined three dialog nodes: one for Elm location questions, one for Maple location, and one for remaining location questions.&nbsp; Below is the definition of the store_location_elm node.&nbsp; The remaining dialog nodes look similar to the other diagrams.<\/p>\n<figure id=\"attachment_221\" aria-describedby=\"caption-attachment-221\" style=\"width: 700px\" class=\"wp-caption alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-221\" src=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-elm-700x490.png\" alt=\"\" width=\"700\" height=\"490\" srcset=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-elm-700x490.png 700w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-elm-300x210.png 300w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-elm-768x537.png 768w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-elm.png 1612w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><figcaption id=\"caption-attachment-221\" class=\"wp-caption-text\">Dialog node declaration for Elm location questions<\/figcaption><\/figure>\n<p>Now, the testing interface ties everything together.&nbsp; When it processes a user utterance, it displays information on which intent, entity, and dialog node fire.<\/p>\n<figure id=\"attachment_225\" aria-describedby=\"caption-attachment-225\" style=\"width: 700px\" class=\"wp-caption alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-225\" src=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-elm-conversation-node-700x261.png\" alt=\"\" width=\"700\" height=\"261\" srcset=\"http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-elm-conversation-node-700x261.png 700w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-elm-conversation-node-300x112.png 300w, http:\/\/freedville.com\/blog\/wp-content\/uploads\/2017\/11\/wcs-dialog-location-elm-conversation-node-768x286.png 768w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><figcaption id=\"caption-attachment-225\" class=\"wp-caption-text\">Testing interface with dialog response<\/figcaption><\/figure>\n<p>Note in this response, the system even highlights the dialog node that was selected, further helping you understand how the system is working.<\/p>\n<p><strong>What should we do next?<\/strong><\/p>\n<p>I kept this example very small intentionally.&nbsp; This is a great way to learn new technology, but leaves you short of a full-featured application.&nbsp; Here are some things I might consider doing next:<\/p>\n<ul>\n<li>Add new training data (what if your user question is just &#8220;store hours?&#8221;) matching real user input.<\/li>\n<li>Add new intents to the bot, covering more user utterances.<\/li>\n<li>Ask clarifying questions (&#8220;Which store are you asking about?&#8221;) when entities are not detected.<\/li>\n<li>Integrate with Watson Discovery.&nbsp; Conversation is good about handling common (&#8220;short tail&#8221;) user intents, Discovery is good at the rarer (&#8220;long tail&#8221;).&nbsp; (You can&#8217;t write specific dialog for every possible user intent!)<\/li>\n<li>Integrate with backend or third-party systems (show the store locations on a map?)<\/li>\n<\/ul>\n<p>Hopefully, this short example triggers your creativity on where to go next!&nbsp; Here are some additional examples of applications using Watson Conversation Service.<\/p>\n<ul>\n<li>An integration with Facebook Messenger:&nbsp;<a href=\"https:\/\/developer.ibm.com\/recipes\/tutorials\/how-to-build-an-enhanced-chatbot-with-watson-conversation\/\">https:\/\/developer.ibm.com\/recipes\/tutorials\/how-to-build-an-enhanced-chatbot-with-watson-conversation\/<\/a><\/li>\n<li>An integration with NodeRed:&nbsp;<a href=\"https:\/\/developer.ibm.com\/recipes\/tutorials\/how-to-create-a-watson-chatbot-on-nodered\/\">https:\/\/developer.ibm.com\/recipes\/tutorials\/how-to-create-a-watson-chatbot-on-nodered\/<\/a><\/li>\n<li>Using Watson Conversation in your Android mobile app:&nbsp;<a href=\"https:\/\/developer.ibm.com\/recipes\/tutorials\/making-an-android-mobile-app-that-uses-the-ibm-watson-conversation-service-as-a-chatbot\/\">https:\/\/developer.ibm.com\/recipes\/tutorials\/making-an-android-mobile-app-that-uses-the-ibm-watson-conversation-service-as-a-chatbot\/<\/a><\/li>\n<li>Integrating multiple services: Watson Conversation, NLU, and weather data:&nbsp;<a href=\"https:\/\/developer.ibm.com\/dwblog\/2017\/chatbot-watson-conversation-natural-language-understanding-nlu\/\">https:\/\/developer.ibm.com\/dwblog\/2017\/chatbot-watson-conversation-natural-language-understanding-nlu\/<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Chat bots are a powerful way to put a natural language interface in front of your applications and services, to help users easily and naturally get what they need.&nbsp; In this post I will&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[9,2],"_links":{"self":[{"href":"http:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/posts\/215"}],"collection":[{"href":"http:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/comments?post=215"}],"version-history":[{"count":11,"href":"http:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/posts\/215\/revisions"}],"predecessor-version":[{"id":411,"href":"http:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/posts\/215\/revisions\/411"}],"wp:attachment":[{"href":"http:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/media?parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/categories?post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/freedville.com\/blog\/wp-json\/wp\/v2\/tags?post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}