Using the Dialog Service in IBM Watson—the Fox, Goose, and Bag of Beans Chatbot
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.
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:
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:
|(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:
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:
Trying it yourself
Clone the repository:
git clone https://github.com/Altoros/watson-dialog-demo
Go to the repository directory:
Create an application:
cf push APP_NAME
Create a Personality Insight service instance:
cf create-service dialog standard dialog_svc
Bind the service instance to your application:
cf bind-service YOUR_APP_NAME dialog_svc
Restage your application:
cf restage APP_NAME
Alternatively, you can create and bind the service instance using the Bluemix dashboard UI.
The binding process is simple.
As you can see, with the Dialog service, it is really easy to automate branching conversations between a user and your application.
- Analyzing Text with the AlchemyLanguage Service in IBM Watson
- Using the Personality Insights Service in IBM Watson
- IoT Platform on Bluemix: Experimenting with IBM Watson and Gobot