Using the Dialog Service in IBM Watson—the Fox, Goose, and Bag of Beans Chatbot

by Stas TurloAugust 2, 2016
In this blog post, we demonstrate how to create a sample chatbot on the Watson platform to solve a classic river crossing puzzle.

chatbots-with-ibm-watson-dialog

Dialog is an IBM Watson service that enables you to automate branching conversations. Below, we show how to create dialogs for virtual agent applications with this service. As an example, we design a sample chatbot for solving the Fox, goose, and bag of beans puzzle.

 

Prerequisites

To get started, you need:

  • An IBM Bluemix account
  • The Cloud Foundry CLI

 

How it works

IBM Watson Dialog manages conversations—dialogs—between virtual agents and users through an application programming interface (API). In addition to the automation of branching conversations between a user and your application, the service can also track and store user profile information.

 

Designing a dialog

In our example, we will create a dialog to solve a classic river crossing puzzle—a fox, a goose, and a bag of beans:

Once upon a time, a farmer went to a market and purchased a fox, a goose, and a bag of beans. On his way home, the farmer came to the bank of a river and rented a boat. But in crossing the river by boat, the farmer could carry only himself and a single one of his purchases—the fox, the goose, or the bag of beans.

If left together, the fox would eat the goose, or the goose would eat the beans.

The farmer’s challenge was to carry himself and his purchases to the far bank of the river, leaving each purchase intact.

The current state can be viewed as the positions of the players. For example, the puzzle begins in the state when all players are on the same side of the river (let’s say on the left side). So, the initial state can be represented by (mFGB|), where m is the man, F is the fox, G is the goose, and B is the bag of beans. If the man crosses with the fox, then the state becomes (GB|mF) with the goose eating the beans on the left bank and the man and fox on the right bank.

Since we have four players, each with two possible states, there are 16 possible states in total:

(mFGB|)(FGB|m)
(mFG|B)(FG|mB)
(mFB|G)(FB|mG)
(mF|GB)(F|mGB)
(mGB|F)(GB|mF)
(mG|FB)(G|mFB)
(mB|FG)(B|mFG)
(m|FGB)(|mFGB)

Blue means the goose ate the beans, and Red means the fox ate the goose. We can consolidate these states to make a total of 12 states:

(mFGB|)(F|mGB)
(mFG|B)(G|mFB)
(mFB|G)(B|mFG)
(mGB|F)(|mFGB)
(mG|FB)(Fox eats Goose)
(FB|mG)(Goose eats Grain)

In the given state space, there is a distinct process that changes from one state to another: crossing the river on the boat. Either it is the man alone or with one other item. Knowing this, we can arrange the states into something called a state diagram showing the possible transitions:

creating-bots-state-diagram

To represent a state in our dialog file, we will use the output element in conjunction with the folder element to group valid inputs and the goto element for state transition. To store the count of crossings, we will also use variables and actions. Each state looks like this:

 <output id="FB_MG_OUTPUT">
    <prompt selectionType="SEQUENTIAL">
        <item>Fox and bag of beans on left bank, you and goose right.</item>
    </prompt>
    <getUserInput id="FB_MG_INPUT">
        <search ref="FB_MG"/>
        <default>
            <output>
                <prompt selectionType="RANDOM">
                    <item>I did not quite get that.</item>
                    <item>I didnt grasp what you said.</item>
                    <item>I'm not following you at the moment.</item>
                </prompt>
            </output>
        </default>
    </getUserInput>
</output>

....
<folder id="FB_MG">
  <input>
    <grammar>
      <item>Return</item>
      <item>return</item>
    </grammar>
    <action varName="Moves" operator="INCREMENT_BY">1</action>
    <goto ref="MFB_G_OUTPUT" />
  </input>
  <input>
    <grammar>
      <item>Return with goose</item>
      <item>return with goose</item>
    </grammar>
    <goto ref="begin" />
  </input>
</folder>

For more details, see the element reference documentation.

The rest of our example is trivial: a simple web application gets user inputs, passes it to a dialog service, and shows responses from the services. Here is how the bot works:

ibm-watson-dialog-bots

 

Trying it yourself

The source code for the article is available in our GitHub repository. You can use this link to deploy a demo application to your Bluemix account or follow the steps below to deploy it manually:

  1. Clone the repository: git clone https://github.com/Altoros/watson-dialog-demo

  2. Go to the repository directory: cd watson-dialog-demo

  3. Create an application: cf push APP_NAME

  4. Create a Personality Insight service instance: cf create-service dialog standard dialog_svc

  5. Bind the service instance to your application: cf bind-service YOUR_APP_NAME dialog_svc

  6. Restage your application: cf restage APP_NAME

Alternatively, you can create and bind the service instance using the Bluemix dashboard UI.

ibm-watson-dialog-bluemix-catalog

The binding process is simple.

ibm-bluemix-ui

As you can see, with the Dialog service, it is really easy to automate branching conversations between a user and your application.

 

Further reading


This post was written by Stas Turlo with assistance from Victoria Fedzkovich and Alex Khizhniak.