Understanding the Conversations
To build a ChatBot, there are two terms you should get familiar with:
1: Intent Based Conversations
2: Flow Based Conversations
Intent Based Conversations
In Intent Based Conversations the NLP (Natural Language Processing) algorithms make use of Intents and Entities to do a conversation. For example, “Book me a flight from LAX to JFK with United Air”. Here, “Book me a flight” is the intent and “United Air” is the entity. So basically, an Intent is what you want to do and Entity is to what thing you want to do your Intent. It recognizes the nouns and verbs in the user’s statement and then matching it to its own content, the bot can perform an action. DialogFlow API works on Intent Based Conversations.
Flow Based Conversations
Flow Based Conversations are the next level of NLP. This works extending the Intent Based algorithm by feeding upon multiple utterances or many different conversations between people and factoring in the appropriate state and context. We can model a conversation using a machine model which can enrich the nature of conversations between people and bots. Wit.ai has been working very impressively on flow based conversations.
In this tutorial we are going to create and train a bot on Intent Based Conversation. The intent and entity I am choosing for this Bot is bank account related. You may have a different choice.
The one thing we must keep in our mind while building a ChatBot is that it must behave like a real person. Let’s start by giving our bot a name — “Teddy”
Now, go to DialogFlow and click “GO TO CONSOLE” in the upper right corner.
Sign in with your google account and authorise DialogFlow to view and manage your data across Google Cloud Platform. After successful login you should be able to see the following page:
Now let’s create Teddy. In the left pane, click “Create Agent”. Here agent means our bot. Fill in the agent name “Teddy” and click on “Create” button to proceed.
Let’s see what we have now. You should see two intents available by default: “Default Fallback Intent” and “Default Welcome Intent”. Also, in the left pane, you should be able to see a list with several items. For now we’ll see Intents and Entities. Let me simplify what I said earlier, our intent is the action we want to perform and our entity is an object on which we’ll perform an action.
Create the Entities
Entities are the nouns which our bot can understand. Let’s create some nouns that we’ll use to communicate with Teddy. I am creating Teddy to reply to bank account related question and therefore I’ll use nouns listed below. You may have your own choice of nouns.
Now click the Entities tab and then click on the “CREATE ENTITY” button on top. Enter “Account” as entity name. Now we need to enter reference value in the list below. Enter “Account” as reference value and enter some of its synonyms like “Bank, Statement, Account Number etc.”. We need to write these as user may say words other than “Account” and we want them all to point towards our entity “Account”. More synonyms, more intelligent our bot will be. You may also create more entities and add synonyms to them. See this in the picture below.
Go ahead and save your entity and create a few more entities similar to how we created the Account entity. I have created two. See following two pictures.
Now that our entities are created, we’ll move forward and add some intents.
Create the Intents
Click on the Intents tab in the left pane and select “Default Welcome Intent”. As the name suggests this is a welcome intent. When our bot is ready, this intent will make Teddy greet us with messages like “Hi, Hello, Good Morning! etc.”. You should see a page like this:
In the same window, if you scroll down, you can see a section named “Responses”. These are the built in responses, this bot is supposed to give us when we say any greeting message. Let’s add one of our own “Howdy! What can I do for you?” This is how your window should look like:
Now, let us create an intent of our own. Click on Intent tab, then click “CREATE INTENT” button on top. Let’s name this intent “Account Information”. Expand the “Training phrases” tab in the middle and click “ADD TRAINING PHRASES”. Training Phrases is where we’ll add possible expressions what an user may ask. For example: “I have a question about my bank account” or “Show me my account information”.
Now let’s add above two expressions. Think a few of your own. In the following picture you can see how the training phrases look like:
Once you have entered an expression, you’ll see that our agent has recognized “@Account” entity in the statement. Similarly it will do for all the expressions. Add a few expressions of your own choice. By doing so you are making your bot more and more intelligent. An user will not ask just these questions. They may have many. By adding more variations, the agent can understand user better.
A fully functional bot is supposed to pull up the account information from the bank and show it to the user or may be find out nearest bank to user’s current location or pay bill etc. To do these we would need to call APIs and integrate the Webhook with Api.ai. We’ll do that in another part of this tutorial. For now, let’s create some fake information in the Response section.
Go ahead and expand the “Responses” tab, click “ADD RESPONSE” and add a few responses of your choice. Here’s what I have added:
Add Follow-up Intents
Let’s now add a few follow-up intents. Bring your cursor over the “Account Information” intent. You’ll see and option “Add follow-up intent”. Click on it and select Custom. A new intent should have generated with name “Account Information — custom”.
Why do we need a follow-up intent? As the name suggests, the bot should ask us a follow-up question. For example, if user asks “Show me my bill”, the bot should show the bill and ask if user wants to make a payment. Let’s now edit this intent. In below picture you can see what I have filled in:
If you added a training phrase with a number in it, you’ll notice that DialogFlow has a built in entity for numbers. Even if you entered a word representing a number, it will be able to handle it and understand its meaning.
Now, the next thing we want our bot to respond is with a confirmation that it is doing a task you have asked it to do. Example: “Calling your bank, Your account has been blocked, A payment of $100 is successfully made”. Go ahead and add these in the “Responses” tab and press “Save” button. See below picture of what I have added:
Now, while saving the intents you must have noticed a blue box appearing in the bottom right corner:
“Agent training started” and “Agent training completed”. So, what do they mean? While we were creating and saving our intents, we were basically training our bot to respond to our questions and these alerts are telling us that our bot is now getting smart to answer the questions that we just taught it.
Now, I’d like to bring your attention to the pane on the right. This is the testing pane. This is where you can test your questions and your bot’s replies. Go ahead and write a question in the box on top that says “Try it now”. You’ll see Teddy giving you a response. Checkout the pictures below:
If you press “DIAGNOSTIC INFO” button, you’ll be able to see the JSON response.
That’s it!! You created Teddy, your own bot.
Now the next thing we need to do is connect Dialogflow using the Api.ai sdk in our iOS app. We’ll do this in the next part of this tutorial which will be available soon. Hang on tight!!