Building an Ordering Application with Watson AI and PostgreSQL: Part I

Published

Do you want to leverage Compose, Twilio, and IBM Watson to provide customers with a real-time, interactive experience? We'll show you how by building an IBM Bluemix application that lets Watson interact with users via Twilio's SMS service then sends their data over to Compose PostgreSQL for storage.

If you want to take your business to the next level, Watson Conversation is a great tool to integrate into your system. It gives you the power of machine learning, while also providing customers with an interactive experience. In this article, we're using Watson Conversation to build a custom SMS ordering system using Twilio and Compose PostgreSQL. The company that we've made up is called ProLumber that wants an ordering system where customers can quickly create orders via SMS to be delivered to their job sites.

We'll use Watson Conversation to build the dialog so that it can interact with customers and gather the data we need to put into our Compose PostgreSQL database which will store the orders. To get the conversation from Watson Conversation to the customer's phone via SMS, we'll use Twilio, which provides the phone number for customers to use. Twilio will also provide Watson Conversation with the customers' SMS responses so that Watson can interact with them to understand what they want to order.

Our application will be built using NodeJS, IBM Watson Conversation, Twilio, and Compose PostgreSQL, which will be hosted on IBM Bluemix. The code for this application can be found on this Github repository.

This article is broken into two parts. We'll start off by setting up the Bluemix environment and setting up our Watson Conversation dialog that will run the conversation over SMS. In part two, we'll write the code that ties together Watson Conversation, Compose PostgreSQL, and Twilio, and we'll deploy the application to Bluemix to get it working. At the end, we'll have an application that looks like this:

To start, let's begin by creating our setting up our application and services together in Bluemix ...

Setting things up on Bluemix

First and foremost, you haven't set up an IBM Bluemix account, then jump on over to https://www.ibm.com/cloud-computing/bluemix and sign up. This is an important step since we'll be using Bluemix to host our application and to store the authentication credentials for our services in the Bluemix environment.

Once you've set up a Bluemix account, you'll want to click the Create app button. On the left-hand menu, click on Cloud Foundry Apps and select SDK for Node.js.

We'll name the application "ProLumber SMS Application". If you're following this tutorial, make sure that you name your application something different since every hostname on Bluemix must be unique.

After that, click Create. Now you'll be taken to your application's "Getting started" page where you will download and install the Bluemix command-line interface. You'll need that to log in and deploy the application to Bluemix. Once you've installed the Bluemix CLI, create a folder on your computer to store the code for your application. Then, follow the instructions on the overview page to connect and log in to Bluemix.

Clicking on the Overview button in the left-hand menu will give you an overview of your application's runtime, cost, activity, and a list of the other services that are connected to it.

The menu options that will be important for us are
Runtime and Connections. By clicking on Runtime, we can view the application's Environment variables which we'll need when using our authentication credentials. As we add services to the application, the credentials will be populated within VCAP_SERVICES and will be available or us to use when deploying to Bluemix.

The Connections menu will show us the Bluemix services that are connected to our application. But, since we don't have any services connected at the moment, the page will also be empty. We'll show you what it looks like after we start adding the services we'll tie into Bluemix. Let's set those up now ...

First, let's look at setting up IBM Watson Conversation. To do that, we'll click on the Connections menu option and click Connect new in the upper right-hand corner. This will take us to the IBM Bluemix catalog that will show us all of the services that we could attach to our application. Since we're interested in Watson Conversation, click on Watson in the left-hand menu and select Conversation.

This will take us to a setup page where we can rename the service. To the left, you'll see the Connect to menu, which by default is set to Leave unbound. We'll want to change that to bind the service to our application so select the name of the NodeJS application you just created.

You'll notice that once you've selected your application to bind to that the Credential name disappears. That's because the credentials are tied into your application's environment variables. Now, click Create at the bottom of the screen to create the Watson Conversation service.

Upon doing that, you'll be taken to the Connections view where you will see Watson Conversation as well as the Availability Monitoring Lite services added. The Availability Monitoring Lite is a free service that automatically comes with your application, but we won't be covering it here.

The next service we'll create is our Compose PostgreSQL service. You can select that from the Data & Analytics side menu button. Once you click that, you'll see a number of databases that are available to you, but we'll select Compose for PostgreSQL.

Again, you have the option of selecting a name for the service and whether to bind it to an existing application. We'll bind it to our application like we did with Watson Conversation.

Next, we'll add the Twilio App service. Again, just click on Connect new, then in the Bluemix catalog select Mobile under Apps, then Twilio.

This will take you to a page where you'll have to enter your Twilio account SID and Authentication Token. You'll need to set up a Twilio account if you don't have one to get your "Account SID" and "Auth Token". Bluemix doesn't provide you with credentials for Twilio as they do for our other services.

Now, going back to the Bluemix connections page, we'll see all of the services attached to that application. You can view the credentials of each application individually by selecting View credentials in each service.

But, since we've tied all of our services to the NodeJS application, we can view all of them by selecting Runtime from the left-hand menu and view all our credentials within VCAP_SERVICES under the Environment variables tab.

VCAP_SERVICES is a JSON document listing all of the credentials for our services that we attached to our application. You can download them, or add to them, but since we are using them within the Bluemix environment, we just have to point to them in our NodeJS application and Bluemix will automatically know how to get them. We'll show you a demonstration in the code we'll write for the application.

So, the setup for Bluemix is done. Let's set up the dialog we'll have with our customers using Watson Conversation ...

Watson Conversation

Using Watson Conversation is how we'll create the dialog for our order application. What the dialog does is leads our customers from one part of the conversation to the next based on their answers to Watson's replies. This back-and-forth dialog continues until it has been completed when the customer has finished making the order.

To set up Watson Conversation, select the Conversation service. This will take you to an overview page where you can click *Launch tool, where you'll be taken to Watson Conversation Workspaces. A workspace is essentially a repository for a conversation that stores intents, entities, and a dialog. We'll briefly touch on what all three are in a bit, but head on over to the Watson Conversation documentation if you'd like to know more.

For now, let's create a new workspace. The name we'll give it will be "ProLumber Ordering System". Once that's done, click Create and you'll have a blank conversation workspace.

At the top of the screen you have Intents, Entities, and Dialog.

Intents are groups of potential user inputs that you want Watson to recognize in order to choose the right response and direct conversation flow. Entities are terms that you want to clarify to Watson from a user's response. For example, when a user says "lumber" or "wood" we want Watson to be able to recognize these are synonyms for the same word, or that they relate to a particular intent in a certain context.

You can create your own intents and entities for your application. You can also import the intents and entities CSV files we've created for this application from our Github repository.

Once you've imported the intents and entities, or created your own, click on the Dialog menu at the top of the screen. Since we can't import dialogs, we'll have to create our own. Click Create new. You'll be given two default dialog boxes, "Welcome" and "Anything else", which you can open and modify.

"Welcome" is what a user will first encounter when interacting with Watson, while "Anything else" will be triggered if Watson doesn't understand the response. On the upper right-hand of the screen, there is a dialog icon that, when clicked, will open up a window that will allow you to test out your dialog. Notice that "Hello. How can I help you" is triggered when we open up the dialog.

To create the dialog, we simply add more dialog boxes and use the intents and entities we've imported to control the dialog flow. Our conversation comprises six dialog boxes.

Each of them takes the user from one part of the conversation to the other. The intent of the dialog is to ask customers for their customer ID, what kind of lumber they'd like, and the quantity that they want - here, we've limited customers to 10, 50, 100, and 150 boards.

The other important part for our application is the collection of user information. This is done via context variables. You can read more about them here. Essentially what they allow us to do is store information in a variable and pass that along to other parts of the conversation when it's called for. For example, if we want to store the customer's ID and use that in other parts of our conversation, we can do that using the context variable. However, for our application, we won't be passing information back and forth between dialogs too much. What we're concerned with is gathering information to send over to Compose PostgreSQL to be stored.

To set up context variables within Watson Conversation we'll have to click on a dialog box, in this case, the "Welcome" box. A window will open up and in the "Responses", click on the advanced feature button indicated by the three rows of dots icon.

We'll select JSON which will show us the JSON representation of our "Welcome" dialog box. Within the JSON dialog we'll only be given the "output" by default, so we can enter the context variables that we want manually and set them to any value. In this case, I've initially set them to null. You don't need to set up the context variables first, but I've done that strictly for convenience.

You may notice that our first dialog box is triggered with the "#greetings" intent. We don't want Watson to initiate the chat. Instead, we use the "#greetings" intent to tell Watson to wait for a greeting to start the conversation. Once the "#greetings" intent is triggered, the context variables will be stored and the response for the greeting will be "Hi. This is the ProLumber Delivery System. Please enter your account number to get started.", prompting the customer for their account number.

The customer account number will be stored in the background of our conversation when the next dialog box has been triggered by the customer sending a number and a phrase from the "#customer_id" intent. What Watson is looking for is something like "My account is 12345". The entity @sys-number, an entity that comes with Watson Conversation, will recognize that 12345 is a number, while the "customer_id" intent will understand the phrase "account is ...".

Now if we look at this dialog's JSON representation, we can see that we've stored the context variable accountID as the @sys-number 12345. In addition, I've used the @sys-number entity to tell the user that I recognize them as 12345 and prompt them for the next input.

For the next input, we'll say that we want some "wood" or "lumber". So the trigger states if the user asks for "wood" or "lumber", then ask if they want "oak" or "pine". So, here we're not leaving the response open ended, but we are trying to guide the user to a response that will trigger the next dialog. In this case, the user will say, "I want some oak".

When the user tells Watson that they want some "oak", the next dialog will trigger and the @kind_of_wood entity will populate the lumber_type context variable with "oak".

Prior to setting up the dialog, we imported intents and entities from a CSV file. If we look at the @kind_of_wood entity, we'll see that we've saved pine, oak, lumber, and wood.

That's so that when we use the entity, Watson will recognize the customer's input and display it using the entity variable. What we've also done is take the @kind_of_wood entity and tell Watson to store what the customer the kind of wood that the user states inside of the lumber_type context variable.

Once the user has given Watson the type of wood, they'll be prompted for the quantity they want. Like lumber_type, quantity is also stored in a context variable.

So, after telling Watson "150 boards", 150 will be recognized using the @lumber_num entity and we can use that to store the number in the quantity context variable. With the quantity stored, we can now use the context variables to send a response to the user using the stored context variables $accountId, $quantity, and $lumber_type.

With the conversation set up, all we have to do now is click on the Deploy menu button on the left-hand side to get the "Workspace ID", which we'll need when setting up the code for the application.

Next time ...

What we've accomplished in this article is setting up your Bluemix environment and creating a Watson Conversation dialog for customers to interact with when making orders. Next time, we'll start writing the code, deploy the application on Bluemix, and start making orders.


If you have any feedback about this or any other Compose article, drop the Compose Articles team a line at articles@compose.com. We're happy to hear from you.

attribution Alex Jones

Abdullah Alger
Abdullah Alger is a former University lecturer who likes to dig into code, show people how to use and abuse technology, talk about GIS, and fish when the conditions are right. Coffee is in his DNA. Love this article? Head over to Abdullah Alger’s author page to keep reading.

Conquer the Data Layer

Spend your time developing apps, not managing databases.