Chatbot — Dialogflow

Photo by Daniel Korpai on Unsplash

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.

User input samples for Dialogflow to train
Set response of the intent

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.

Set parameters we want to extract from user input samples

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.

Fulfillment of Dialogflow

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.

Context of conversations

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.

ExampleBot logic flow

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.

Training phrases
Default response

2. Name intent

Training phrases for name

In name intent, we need to capture the name of the user in the samples, set the parameters to represent them.

Name parameter

Then we can set our bot answer with the parameter we get

Answer with parameter

Another important point is we need to set the context so that the name of user can be “cached” for the following conversations.

Context

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.

Intent after intent

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

Context

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

Training phrases

Save as a parameter

Parameter

Then we need to ask another question for smoking status

Response

4. Different smoking answer

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
Ask one more question
Ask user smoking year
  • 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.

End of conversation

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.

Enable fulfillment for these 2 intents
Code your cloud function
Logic of getting the product model
Cloud Function will control the response of these 2 intents

Then our bot is ready to go!

Testing

Bot testing
Bot testing — smoke yes
Bot testing — smoke no

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
Additional features

Hope you find this article useful, thank you all.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

API Throttling and Rate Limiting

Python annotations: Here’s why you should use them NOW

The DevOps Tools List You Need To Know About

How to connect MySQL with Python?

firebase_messaging : Catch them all !

4 best practices for a successful Snowflake migration

Function Pipelines and Caesar’s Code in Elm

How We Changed Our Payment Service Provider Over an A/B Test via HAProxy

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
datalearningpr

datalearningpr

More from Medium

Named Entity Recognition: how to use it with LOKO AI

An example of NER with LOKO AI on a policy

How Does a Chatbot Learn?

image showing virtual chatbot

Building a simple chatbot

Create a question-answer service using GPT-3 and OpenAI