Chatbot — Dialogflow
--
Natural language processing(NLP) is one of the key area of machine learning/deep learning. Chatbot, is the most common and mature application of NLP in real life. It successfully reduce operating cost by replacing real human for interacting with customer to give them the information they want. Nowadays, there are different platforms providing ways of creating Chatbot easily through a predefined bot framework. In this article, we are going to show how to use Google Dialogflow to create a chatbot.
Dialogflow
Before building the bot, we will introduce the concepts of Dialogflow, and how Dialogflow utilise these concepts to help user to build the bot.
1. Intents
Intent is the most important concept of Dialogflow, it means one interaction of user input and bot response. For instance, you want to capture the end user’s intention to ask about weather, then you can create an intent in Dialogflow, provide samples how bot user will ask about the weather, and you can specify how the bot will response.
2. Entities
Entities means common object appear in different sentences, Dialogflow already provide a lot of common entities, such as colour, name, number etc. You certainly can set custom entities like different size of your product or different vegetable names.
3. Parameters
In order to answer bot user’s questions, we need to extract the important information/entities we mentioned above from the sentence as parameters. Then we can use the parameters to answer the questions or conduct complex operations to get the final answer for the bot user.
4. Fulfillments
Fulfillments allows you to connect to your server or use the Dialogflow serverless solution — GCP Cloud Functions to conduct complex operations like connecting to DB to get information etc. Without this, the chatbot cannot conduct any other operations but hardcoded answers.
5. Context
Most of the time, the bot needs to ask a lot of questions so that the answer can be made. In these cases, the bot have to memorise what conversations have happened so far.
Input context will make this intent happen only if the input context exist.
Output context will initiate the context after this intent.
Therefore, it is easy to see you can chain intents up by using contexts. For instance, if you want the second intent after and only after the first intent, then you can use context to achieve it.
Example
After understanding basic concepts we need to know for building a bot, now we will the ExampleBot step by step.
We will be building a bot to get user name, age and smoking status, then based on the information to get the proper product model for end user.
In this example, there will be
- Questions, use parameters to get user answer information
- Questions are dependent on the answer of user input, if user smoke, there will be one more question; if user does not smoke, there will not be any more questions.
- After getting all the information, we will use Cloud Function(you can build your own webhook server as well) to set the bot answer, if you know how to use this feature, within the function, you can basically do anything with programming you want, like connecting DB, calling APIs etc.
1. Welcome intent
This one is easy, just need to set up the training samples and set the bot answers.
2. Name intent
In name intent, we need to capture the name of the user in the samples, set the parameters to represent them.
Then we can set our bot answer with the parameter we get
Another important point is we need to set the context so that the name of user can be “cached” for the following conversations.
username has lifespan of 5, will be remember for the whole conversation. the second context is auto created by Dialogflow so that we can set the next question right after this conversation.
Therefore, you can set the context by yourself, or use the UI to let the Dialogflow create for you, they are functionally the same.
3. Smoke intent
In order to get the username, we will need to set the input context to be the username, the output context is created by UI, since we will have following questions.
we will get the user answer for their age
Save as a parameter
Then we need to ask another question for smoking status
4. Different smoking answer
It is very common the bot logic will have questions based on different user answers. We use UI feature to add different intents for user smoke or not.
- if user smoke, we will ask one more question about how many years of smoking
- if user does not smoke, we will end the conversation by giving the user the product model.
5. End
For user does not smoke or already gives the smoking year, we will then reach the end of conversation by giving the end user proper product model.
After getting user’s input of information, we will then use a “service” to calculate the proper model for end user. You can build your own webhook server, but in this example, we will serverless GCP Cloud Function to achieve it. You certainly can connect DB and call restful api within the cloud function, but in this example, we use a JS function to do the business logic.
Then our bot is ready to go!
Testing
It works!!!
Summary
We have seen that it is quite easy to use Dialogflow to make a chatbot. But there are a few things to pay attention to when it comes to real life production chatbot
- Training samples for production ideally have number over 50
- You can create your own webhook server with your preferred language
- Turn on small talk or create your own small talk function to make the bot feel more like a real person
- There are prebuilt agents for user to learn and take reference
Hope you find this article useful, thank you all.