Dedicated to the people I love and the God I trust.
—Navin Sabharwal
Dedicated to my family and friends.
—Amit Agrawal
This book provides clear guidance on how to use Google Dialogflow to develop a cognitive chatbot that simulates a conversation between the bot and the user. You will start by understanding the technology landscape and various use cases where Google Dialogflow can be used. Specifically, the first two chapters will take you through the basics of Google Dialogflow, use cases of Google Dialogflow, and various cognitive platforms/frameworks, before moving onto advanced concepts and hands-on examples of using Google Dialogflow. The next chapters provide integrations that enrich Google Dialogflow using sentiment analysis, a spellchecker, the Google knowledge base, weather APIs, etc. We conclude the book by providing you with a glimpse of what to expect in the future for Google Dialogflow.
To my family, Shweta and Soumil, for being always there by my side, for letting me sacrifice your time for my intellectual and spiritual pursuits, and for taking care of everything while I was immersed in authoring this book. This and other accomplishments of my life wouldn’t have been possible without your love and support. To my mom and my sister for the love and support as always; without your blessings, nothing is possible.
To my coauthor, Amit, thank you for the hard work and quick turnarounds to deliver this book. It was an enriching experience, and I look forward to work with you again soon.
To all my team members who have been a source of inspiration with their hard work, their ever-engaging technical conversations, and their technical depth. Your always-flowing ideas are a source of happiness and excitement every single day. Piyush Pandey, Sarvesh Pandey, Amit Agrawal, Vasand Kumar, Punith Krishnamurthy, Sandeep Sharma, Amit Dwivedi, Gauarv Bhardwaj, Nitin Narotra, Divjot, and Vivek: thank you for being there and making technology fun.
To Celestine and Aditee and the entire team at Apress for turning our ideas into reality. It has been an amazing experience authoring with you, and over the years, the speed of decision-making and the editorial support have been excellent.
To all my other coauthors, colleagues, managers, mentors, and guides, in this world of 7 billion, it was coincidence that brought us together, but it has been an enriching experience to be associated with you and learn from you. All ideas and paths are an assimilation of conversations that I have had and experiences I have shared. Thank you.
असतो मा साद गमय, तमसो मा ज्योतिर् गमय, मृत्योर मा अमृतम् गमय
Asato Ma Sad Gamaya, Tamaso Ma Jyotir Gamaya, Mrityor Ma Amritam Gamaya
Lead us from ignorance to truth, lead us from darkness to light, lead us from Illusion to Reality
has 20+ years of industry experience and is an innovator, thought leader, patent holder, and author in the areas of cloud computing, artificial intelligence and machine learning, public clouds, DevOps, AIOPS, DevOps, infrastructure services, monitoring and management platforms, big data analytics, and software product development. Navin is responsible for DevOps, artificial intelligence, cloud lifecycle management, service management, monitoring and management, IT ops analytics, AIOPs and machine learning, automation, and operational efficiency of scaled delivery through lean ops, strategy, and delivery. He is reachable at[email protected] and https://www.linkedin.com/in/navinsabharwal .
is a principal data scientist and researcher delivering solutions in the field of AI and machine learning. He is responsible for designing end-to-end solutions and architecture for enterprise products. He is reachable at[email protected] and https://www.linkedin.com/in/amit-agrawal-30383425 .
is a cognitive solution architect. She is experienced in managing the delivery lifecycle of enterprise cloud and cognitive solutions. She has implemented and worked on chatbots using various cognitive engines such as Watson, LUIS, LEX, Dialogflow, etc. She has done conversational modeling using natural language processing as well as machine learning building on bots and worked in development and integration environments. Neha develops avenues to drive end-to-end solutions and leverage cognitive, artificial intelligence, and machine learning solutions. She is reachable at[email protected] and https://www.linkedin.com/in/neha-anand-05 .
The technologies being implemented today are no longer the stuff of science-fiction; virtual reality and much more is now possible.
Remember when messaging services really took off? The reason they were universally accepted in such a positive light was because they personalized the user-to-user experience. At the same time, messaging services fueled the general population’s interest in technology, which gradually opened the doors to new possibilities becoming palatable to a general user base.
One of those new possibilities is the cognitive assistant, or chatbot. Chatbots are the latest technology to be changing the way people communicate and do business.
By consuming auditory or textual input, filtering it to get to the intent of a query, and providing the closest solutions possible based on predefined organization data and rules, chatbots can help people with things ranging from online shopping to managing the IT infrastructure of enterprises. In addition, artificial intelligence and machine learning have been making the chatbot experience more and more human-like.
This book is a guide to building your own conversation system on the Google Dialogflow platform. You will learn to develop a chatbot for a complex use case in the travel industry in a step-by-step manner and integrate with other services such as sentiment analysis, weather forecasting, and personality analysis. In this chapter specifically, you’ll get an introduction to chatbots.
Let’s deep dive into the main topic of this book, cognitive virtual assistants. The term assistant can be defined as someone or something that can assist in performing a task or activity. Virtual means something that exists in the virtual world such as software. The term cognitive relates to human cognition: “the mental action or process of acquiring knowledge and understanding through thought, experience, and the senses.” It covers aspects of intellectual functions and processes such as attention, generation of knowledge, evaluation, reasoning, problem-solving, decision-making, comprehension, and formation of language.
In other words, cognition is a human ability that helps us to acquire knowledge, perform all our mental functions, gain knowledge from an environment, and create new knowledge from that.
Some cognitive virtual assistants have some of these capabilities, but they may not be as accurate as a human being today. However, there are certain areas in which they are better than humans as they don’t have the limitation of processing power. In addition, they have an ability to scale and communicate with millions of users simultaneously, which is not possible for a human being.
Let’s now define what a cognitive virtual assistant (CVA) is. A CVA is a software agent that performs tasks either for humans or for machines based on text, voice, and visual input. It has an ability to understand the input provided, interpret it, perform step-by-step operations, and probe a user for missing information. As a result, it can either provide some information or execute an intended task. CVAs have the capability to understand conversation in a natural language and maintain the context of conversation to achieve the end objective.
Some CVAs can be configured to process or interpret voice commands, images, and videos, and then perform an action accordingly. CVAs can respond in various formats depending on a mode of communication, i.e., text when the mode of communication is a messaging system, or voice to respond over voice channels. CVAs can also be embedded in devices such as speakers, navigation systems, and so on.
Nowadays, CVAs are available on multiple devices and interfaces and can be accessed via mobile handheld devices, laptops, and PCs. CVAs can also be integrated with various messaging platforms such as Facebook Messenger, Skype, Telegram, etc.
Virtual assistants can search various sources and retrieve information such as weather, stock updates, market information, prices, catalog information, interest rates, etc.
Virtual assistants can act as a search engine to find relevant documents from document repositories.
Virtual assistants can play videos or music from catalogs and subscription services such as Spotify, Netflix, Amazon Prime, etc.
Virtual assistants can act as IT service desk agents to resolve problems and issues with IT equipment.
Virtual assistants can conduct specialized services such as healthcare, legal, administrative, and finance functions.
Virtual assistants can act as embedded agents in devices such as speakers, televisions, and other equipment.
Virtual assistants can act as agents in vehicles to aid in navigation and operate entertainment devices like music players, etc.
The following are use cases for cognitive virtual assistants.
Help desks for IT services that use cognitive virtual assistants are a way to reduce the effort of IT help-desk teams. In today’s highly competitive environment, it is important to reduce the cost of delivering IT services while managing the efficiency of delivery.
Due to the 24/7 availability of CVAs and the ability to communicate with multiple users simultaneously, it is possible to respond to users whose requests have been waiting to be fulfilled. Users can also solve most of their issues or problems on their own through laptops, desktops, e-mails, and other enterprise applications via the self-service capabilities of CVAs.
Some mundane tasks such as resetting passwords and creating users can be easily achieved by CVAs, thus freeing up IT resources to work on more complex problems.
Some bots even come as ready-to-use IT use cases for faster adoption. One such bot framework is DRYiCE Lucy, which provides hundreds of out-of-the-box use cases for IT; see https://www.dryice.ai/.
There are lot of use cases that fall in the triage category. CVAs in this category can ask users about issues and then route them to the right information or to the right agent for a resolution to the problem.
Interactive voice response (IVR) systems are an example of triage CVAs where they can ask the user to select an option over a telephony network and either provide information based on the option selected or route the user to a human agent. The IVR system of a telecom company, a broadband company, or a bank is a good example of how triaging works.
Organizations can use CVA capabilities to enable IVR to perform more intelligent communication and thus elevate the user experience. In fact, intelligent CVAs can understand a user’s requirements and provide relevant information faster than IVR systems. Tasks such as retrieving account balances and security balances can be easily implemented by integrating CVAs with enterprise applications.
Lead generation CVAs are generally embedded within web sites. When a user visits any page of the organization’s web site, a pop-up window for a CVA opens and starts a conversation with the user. These days you will find that most web sites have started using CVAs in a lead generation function to enhance the user experience.
With the help of CVAs, a user can get to know the information quickly and doesn’t have to go through the entire web site navigating from page to page to find it.
In this use case, since a user is communicating with the CVA, they are an active user providing meaningful information through chat conversations that later can be used as a lead, for feedback, etc.
E-commerce virtual assistants are most common types of CVA, helping millions of consumers to browse and select relevant products or services according to their preferences, within an intuitive interface.
It is difficult for users to search for products and services out of the millions of products that are currently available. However, with the use of advanced machine learning capabilities, CVAs not only converse with a user but can search relevant information to make buying and selling online easier.
E-commerce CVAs can have capabilities such as search and ranking algorithms to promote some products over others per a customer’s requirements.
CVAs can be also be used to provide product offers to consumers, thus meeting a consumer’s expectations and leading them to buy a product. This isn’t the case if there is a static web site where the price of a product remains constant .
Google provides a set of enterprise-ready artificial intelligence (AI) services, applications, and tooling. It is a collection of cloud services running on the Google Cloud Platform. For example, Google uses the Natural Language API to identify insights from a large amount of unstructured text data. Google AutoML leverages the latest AI technologies to find an optimal model architecture for specific datasets to perform specific tasks. This section provides details of these AI services, which can be used for the development of various AI-based use cases.
The Google AI Platform provides various cognitive services to make the usage of AI platforms easy for application developers. Specifically, it provides various cloud-based services, many pretrained models, support for new model training, and enterprise support. Google’s AI Platform helps developers and data scientists to build machine learning products and move them from the proof of concept (PoC) stage to production. It mostly covers domains related to preprocessing, natural language, machine learning, and deep learning. These services are quite efficient and easy to implement in any environment.
The following are some of the products offered as part of the Google AI Platform.
Google Dialogflow is one of the services from the Google Compute Platform that makes it easy for developers to develop and integrate cognitive virtual agents with their applications. It uses natural language understanding and natural language processing capabilities to build complex use cases. Because of the rising demand of chatbots to improve customer support and experience, Google Dialogflow offers technologies to develop robust and intuitive bots that can be launched across multiple chat channels, for example, Google Assistant, Line, Hike, etc.
The Google Natural Language API provides many pretrained models to be used for variety of natural language tasks such as sentiment analysis, entity recognition, etc. The huge corpuses over which the models are trained provide high efficiency and ease of direct predictions without the need to train the model. If the domain knowledge exploited in the dataset is peculiar or rather idiosyncratic, one can train their own model, though. The Natural Language API comprises five services, described next.
This service helps in the linguistic analysis of any text by generating part of speech tags, dependency trees, etc.
Score: The score is on a scale of -1 to +1 where the negative scale shows negative emotion, the positive scale shows positive emotion, and 0 is neutral.
Magnitude: This is a value measuring the impact or strength of an emotion. If text is reinforcing an emotion, the score for magnitude will be high as compared to text with a plain visible sentiment and no reinforced emotion, although both might be equal scored.
This service helps identify important entities such as places, locations, etc., with a confidence score that tells you how relevant an entity is to the identified phrases from given text. A higher score speaks of a higher importance of an entity with respect to the text.
This service helps identify the emotions that might be associated with each identified entity to give a better picture and analysis of text.
This service helps classify the text into predefined categories and subcategories.
Cloud AutoML provides an end-to-end solution for applying machine learning to real-world problems. It allows developers to focus on tuning parameters specific to their dataset instead of developing workflows; hence, it is mostly concerned with data acquisition and prediction.
The Google Cloud ML Engine enables developers and data scientists to build, deploy, and run their own machine learning applications in a serverless environment. It supports popular machine learning/deep learning frameworks such as Tensorflow to train your own model. It also has built-in tools support for better visualization and understanding of models.
Google Tensorflow is the enterprise version of Tensorflow for developing large-scale machine learning or deep learning applications with long-term support. It helps data scientists to design their own machine learning pipeline for any custom requirement and deploy it using the Google Cloud ML Engine.
A labeled dataset is required to solve most real-world problems, but labeling datasets correctly can be tedious. Data Labeling is Google’s solution that helps you label a dataset with human labelers when you provide an annotation specification set and instructions. It uses state-of-art technologies such as proactive learning to capture label information from users and apply it to labeling an unlabeled dataset. The dataset you receive is labeled and can be easily used with the machine learning models it is required to train.
The AI Platform basically is the solution to ease the ML development. For machine learning development, a lot of steps are involved, from idea to deployment; the AI Platform has an integrated tool chain to help you with the steps. It helps you build portable ML pipelines to implement on any environment with minimal changes. It has incorporated technologies such as Tensorflow, and Kubeflow to develop and implement solutions on the Google Cloud Platform and deploy them across environments.
Have you ever been looking for a solution that can search for relevant candidate profiles for a particular job posting? Google’s Cloud Talent Solution will do this for you. It performs job search and candidate profile matching using natural language understanding and machine learning to understand candidate profiles and job postings to find relevant jobs for job seekers and relevant profiles for employers. These features have been released as an API, which can be leveraged to build a job search interface.
Google’s Cloud Translation service translates text between two languages. It leverages Google’s pretrained language translation model to provide results as per the best model. The Cloud Translation service is easy to integrate into applications, web sites, tools, etc. It is generally used to provide multilingual capabilities to any application where building the machine learning model from scratch is a time-consuming and resource-intensive activity.
Google Cloud offers two vision products: the Vision API and AutoML Vision. The Vision API provides pretrained machine learning models that automatically classify images with predefined labels, and AutoML Vision helps you to build your model with your own training labels. The cloud vision services provide solutions to current problems of using a computer with impaired vision, i.e., searching for products on an e-commerce web site and identifying emotions on human faces for emotion analysis. They also provide pretrained models to identify the faces of most popular celebrities.
The Google Cloud Platform offers two video AI products: the Video Intelligence API and the AutoML Intelligence API. The Video Intelligence API provides pretrained machine learning models that automatically derive insights from stored or streaming video and help you to build your own model to derive insights based on your labeled dataset. Google’s Video Intelligence API has been gaining popularity because of its applicability in different use cases such as video search engines, identification of a particular scene from video, etc.
Dialogflow is a natural language understanding platform from Google that helps developers to design conversational interfaces and integrate them into their applications. Dialogflow can be integrated with other cognitive services such as sentiment analysis, knowledge base services, etc. Dialogflow can also leverage cognitive, AI, and other services available from other cloud providers through API-based integration.
Google Dialogflow has built-in integrations available for common messaging platforms such as Facebook Messenger, Slack, etc. This enables developers to rapidly create chatbots that are integrated with popular messaging platforms.
In this chapter, we will discuss the key aspects to consider while designing cognitive chatbots using Google Dialogflow such as intents, entities, responses, parameters, etc.
In a chatbot system, a conversation is defined as communication between parties in which news or information is exchanged, questions are asked or answered, etc. We will start with an example of a sample conversation between users and demonstrate how a chatbot system recognizes the intent of the user.
User: Hi, I am looking for some travel options from Pittsburgh to Boston. Can you help?
Support Person: Sure! I will help you. How do you want to travel?
User: I want to travel via flight.
Support Person: When do you want to travel?
User: October 30, 2019
Support Person: OK. Which airline do you prefer?
User: I will go with American Airlines.
Source City: Pittsburgh
Destination City: Boston
Date: October 30, 2019
Departure Time: 10 a.m. EDT
Arrival Time: 11:30 a.m. EDT
User: Thanks! Please go ahead with the bookings.
Source City: Pittsburgh
Destination City: Boston
Date: October 30, 2019
Departure Time: 10 a.m. EDT
Arrival Time: 11:30 a.m. EDT
PNR Number: 12345671
Identify the intention of user (i.e., look for travel-related information).
Extract location entities from the user’s question (i.e., Pittsburgh and Boston). These are entities of type location.
Ask follow-up questions about the mode of travel.
Extract the entity for the type of travel mode (i.e., flight from user’s response).
Ask a follow-up question about the day of travel.
Extract a date from the user’s response.
Ask a follow-up question about the airline preference.
Extract the airline preference from the user’s response.
Use the intent and entities to get flight information by calling the third-party API.
Respond to the user with flight information and ask for confirmation.
Process the confirmation message from the user and call the third-party API to book the tickets.
Respond to the user with the booking information.
Though these activities look complicated and beyond the capabilities of automated systems, with the advanced conversational capabilities being made available by machine learning technologies, it is possible to create such complex use cases. In subsequent sections, we will take a step-by-step approach to creating a conversation system using the Google Dialogflow engine.
Let’s go through the steps for creating a conversation interface using Google Dialogflow. Let’s start by creating a Google Dialogflow account. Please note that you should have a Google account before proceeding with the following steps. If you do not have a Google account already, you can sign up for one using the details at https://accounts.google.com/SignUp. Once you have a Google account, you can proceed to the next steps.
Go to https://dialogflow.com/ and click the “sign for free” button.
The site will ask you to log in with a Google account.
Select a Google account for setting up a Dialogflow account. After selecting an account, the site will ask you for the password for the selected account.
After entering the password, you will be redirected to the confirmation screen where you need to accept the terms of service, as shown in Figure 2-1 .
Agreement confirmation
Google Dialogflow welcome page
The Dialogflow agent is a virtual agent that controls the end-user conversation. It implements natural language understanding technology to deduce the meaning of a human query. It converts natural language text or voice into a structured format that is used as input to an application, service, or third-party API.
An agent can also be defined as a collection or repository of use cases for a particular domain. Ideally, only one agent should be configured for use cases related to one domain. We need to create separate agents for use cases related to other domains. A domain in this context is an industry or vertical domain such as IT, healthcare, banking/financial services, etc.
An agent can also use the Google knowledge base if Dialogflow is not able to answer the user’s query. The knowledge base acts as a searchable document store that can search documents by automatically extracting the intent from a user conversation and matching it to a document in the knowledge base. This feature is in beta at the time of writing of this book. Google Dialogflow can be enriched by enabling other features such as sentiment analysis, spellcheckers, and knowledge bases using agent configurations.
An agent can also use either a rule-based system or an ML+rule-based system for intent recognition. For example, we can use a rule-based system if fewer variations are available and can use an ML system otherwise. A rule-based system does not need a large amount of data to train a machine learning model; it simply uses rules that we configure to find the intent from the conversation.
Now, we will proceed to create an agent and configure it for the travel domain.
Creating an agent
Use case design
Before creating the intents, let’s first discuss some of basic concepts related to Google Dialogflow.
A use case is a complete end-to-end conversation flow for a scenario. It includes all the possible paths that the system can take to provide a relevant response to the query asked by a user. A simple example is something like Tell me about travel options from Pittsburgh to Boston. The conversation system will recognize the intent, entities, and actions, and it may also trigger your webhook APIs to provide responses to your queries by integrating with third-party systems that may have this data. In Google Dialogflow, a single intent or collection of intents can be referred to as the use case.
An intent is the verb or action that is part of the conversation with the user. Dialogflow categorizes end-user inputs into the relevant intent. In each agent, a user can define multiple intents, with the combined intents controlling the flow of a conversation. For each intent, we can specify how to respond to the user based on the use case. As an example, an intent for a query from a user saying Tell me about travel options from Pittsburgh to Boston would be Travel since the user is looking for some information related to travel.
Use case design flow
Intents contain the information covered next.
For each intent, the training phrases are the set of sample utterances from the end users of our use case. Google Dialogflow uses these phrases to train built-in machine learning algorithms for intent classification. These examples should be captured from end users who are using the system or will use the system. The data from previous chats and paths that user queries took will come in handy to train Google Dialogflow. Chat logs, manual standard operating procedures, call logs, web logs, web form requests, e-mails, etc., will contain information on how the user conversation flow happens and what queries customers are asking related to this use case and domain. The training phrases are also called utterances. This is the end-user data that is used to train the machine learning models so that the model can then accurately determine the intent when a new question is asked by a user by matching it to the already configured training phrases. Readers should note that the training phrases or utterances are not matched one to one with the query that the user is asking, but these are used to train a model, and thus the agent will be able to correctly figure out the intent even if a user’s query is different than any of the training phrases configured in the system. Google Dialogflow uses machine learning and natural language understanding and processing models to figure out the intent. Thus, it is important for a cognitive virtual assistant developer to understand use cases, intents, training phrases, and entities in depth to be able to configure the use case correctly and provide the correct response to new queries.
When an intent is matched, the Action field value is defined by us, and this value is passed on to the fulfilment webhook requests or APIs. In other words, the action is used to trigger the logic in the workflow.
Parameters, entities, values, and responses are defined under an action, and they are used to extract the required information from the user to complete the workflow, as shown in Figure 2-6.
Action and parameter
Required: Select this only if it is a mandatory parameter for an intent.
Parameter Name: Set the name of the parameter. Example: source-city and destination-city.
Entity: Define the system or developer entity for this parameter. Example: @sys.geo-city.
Value: Define a variable where the parameter value will be stored. Example: $source-city and $destination-city.
Is List: Specify whether this parameter should contain multiple values or not.
Prompts: Regularly ask questions if relevant or required input was not provided. This field is used only if the Required field is selected. Example: Please Enter Source City.
Parameters
Google Dialogflow supports built-in response handlers where we can configure a response for an intent. So, whenever the end-user expression matches the relevant intent and entities, the corresponding response will be displayed to the user. We can define different types of responses other than just text formats such as image, audio, speech, etc. The user can use parameter reference variables in the response field to generate a dynamic response. We can even define multiple responses for an intent to make our agent more human-like, where the agent will choose one of the responses at random.
It’s nice you want to travel from $source-city to $destination-city.
Response
Have a safe trip, you want to travel from $source-city to $destination-city.
Intent matching flow
Entities are nouns that combine with the intent to identify a particular action. An entity also specifies how parameter values will be extracted from user expressions. In the previous example, the system entity (@sys.geo-city) is used to extract the parameter values (Pittsburgh and Boston). Each parameter has an entity type, which shows what type of data is to be extracted and stored in the parameter value.
System entities
Dialogflow provides a set of predefined entities to extract the information from end-user expressions. But for most use cases, it may require extra values that are not predefined by Dialogflow. We can extend system entities for this purpose. In the Training Phase section on the intent page, we can add values to a system entity by annotating a training phase.
Regexp entities
Variations
Batch Operations
Operation | Description |
---|---|
Copy | This is used to transfer the selected entities into the destination agent without removing any in the current agent. |
Move | This is used to transfer the selected entities into the destination agent and remove any from the current agent. |
Delete | This is used to delete the selected entities permanently from the agent. |
Batch operations
“Try it now” panel with correct response
We have explained the basic concepts of Google Dialogflow that are necessary for training a conversation system. Let’s now create a use case in a step-by-step manner.
We will start with the identification of a use case. For this example, we are continuing with the use case from the travel domain that we have used in the previous examples.
The chatbot system can be integrated with a travel web site where users can log in and start conversing with our chatbot system. As an example, the user wants to travel from one place to another. The user is looking for travel options such as train, flight, etc., and the cognitive bot will capture the intent of the user and decipher what it is.
You can download the agent zip file that has the use cases configured for the travel domain. Go to https://github.com/dialogflow34/Chat-Bot/tree/master/Chapter_2 to download the Create_First_Bot.zip file.
Once you have downloaded the agent, you can import it into Google Dialogflow.
Exporting and importing a zip file
Importing an intent
Intents page
Once we have identified a use case to work upon, we will move on to the intent, training phrases, entities, and response configuration.
The following sections explain how to work with intents.
Default intents
Default Welcome Intent : This intent is triggered when the user starts a conversation with the bot. The intent gives a response to the user with the information about what your agent does. This is the first response from the bot to the user.
Default Fallback Intent: This intent is triggered if no condition is matched in any of the intents in the hierarchy. If the workflow matches the condition of any other intent, this intent is not executed. Thus, if none of the intents is matched, the agent can respond by gracefully exiting the conversation, as shown in Figure 2-19.
Fallback intent
The user can add training phrases explicitly to the default fallback intent that will act as negative examples. In some situations, there may be similar sentences, but they may not match with the intent; these negative example variations will ensure that the intent is matched correctly and excludes the negative training variations.
Entity entries: Train, Flight
Entity entries: American Airlines, JetBlue
I want to book a flight from Chicago to San Francisco
I want to explore tourism options in Las Vegas
Help me with travel options from Las Vegas to Brooklyn
Travel options from Chicago to Boston
I want to travel from Los Angeles to San Francisco
Ideally, in a practical scenario, these phrases should be collected from end users to provide actual variations to the system for training.
Log in to the Google Dialogflow console.
Select the agent named Travel that we created earlier.
Creating an intent
Going to the training phrases
Adding training phrases
Add at least ten examples/variations or more as training phrases for an intent. The more variations, the better the training, and that results in better responses from the bot.
Two different intents shouldn’t have similar types of training phrases. This is required to avoid any kind of overlap during intent recognition.
Dialogflow provides limited types of system entities. To match any custom data, the user can create a developer entity such as Travel_option.
Go to the left menu of the agent and click the Entities tab. Then click CREATE ENTITY.
Fill in the entity name at the top of the page. This defines the type of information the user wants to extract from the query. Enter Travel_Option as the name here.
Click the first row and enter an entity value and synonym. Here, enter Flight with a synonym of Flight.
Like previously, fill in the next rows as per the requirements, as shown in Figure 2-23. Here, enter Train with a synonym of Train.
After entering these values, your screen will look like Figure 2-23.
The “Allow automated expansion” option is used to match the entity values that are explicitly defined in the developer entities. If the user mentions some value that is not included but similar to an entity, then this feature recognizes the value as a parameter in the entity.
Entity Creation
Now we will configure an action and parameters so that we can extract the relevant entities from the conversation and prompt the user appropriately.
Action and Parameters
S. No. | Required | Parameter Name | Entity | Value | Is List | Prompts Text |
---|---|---|---|---|---|---|
1 | Yes | source-city | @sys.geo-city | $source-city | No | Please enter source city. |
2 | Yes | destination-city | @sys.geo-city | $destination-city | No | Please enter destination city. |
3 | Yes | Travel_options | @Travel_option | $travel_option | No | That’s nice. Please tell me how you want to travel. |
4 | Yes | airlines | @Airlines | $airlines | No | Please mention airline. |
5 | Yes | date | @sys.date | $date | No | Please mention date. |
Required: Check this for all parameters. This restricts users to providing values for all the parameters.
Parameter Name: Provide the name of all parameters as mentioned in Table 2-2 in the column “Parameter Name.”
Entity: This defines which entity entries will be stored in a particular parameter. For our use case, define the entity as per Table 2-2.
Value: This is a variable that stores values of entities from a user’s query. For our use case, define variables as per Table 2-2 in the “Value” column.
Is List: Define whether a parameter can have multiple values. For our use case, each parameter will have only a single value. Hence, don’t select this option for any parameter.
Prompts: A text message will be prompted to the user if values of parameters are not provided by the user. Please note that this will be enabled if you select the Required option for a parameter.
Once configured, your system should look like Figure 2-24.
Defining a prompt message
Prompt configuration
For this use case, configure the prompt text for all the parameters as per the details shown in Table 2-2. The “Action and parameters” fields will look like Figure 2-26. After defining all the parameter prompts, click the Save button next to the intent name to save the changes to the “Action and parameters” configuration.
The required parameters ensure that the agent will ask the queries to probe the users for additional information until all the required information is captured.
“Action and parameters” fields
Now, we can configure the response by clicking the drop-down button next to the Response section for the intent Travel_info. Define the response message as follows:
Your $airlines flight ticket has been booked. Below is your itinerary details. Source City: $source-city Destination City: $destination-city Date: $date Departure Time: 10:00 AM EDT Arrival Time: 11:30 AM EDT PNR Number: 12345671
In the response message, parameter values are used to generate dynamic responses (i.e., $airlines, $source-city, and $destination-city).
Configuring a response
We have configured intents, entities, parameters, and responses for the Travel_info use case. Google Dialogflow automatically starts training the agent as soon as an intent and its complete details have been configured.
Testing in the “Try it now” panel
Test console
User Prompt - Airline
User Prompt - Date
Flight Booking Confirmation
Integrating with Web Demo
Web Demo configuration
In this chapter, we designed our first cognitive chatbot using Google Dialogflow. We also discussed basic concepts such as intents, entities, parameters, etc., with an example to give you a glimpse at the building blocks that are essential to creating conversation use cases in Google Dialogflow.
In previous chapter, we discussed how to create a simple chatbot. In this chapter, we will discuss advanced topics such as dialog context (input and output contexts), follow-up intents, multilingual bots, webhooks, and more. These advanced features will help us to configure complex use cases.
Recap of action and parameters
Now, let’s take a step forward and imagine a scenario where a user might ask further questions based on the response of the previous intent. So, when the agent responds with Are you looking for any other bookings? the user might say cab booking or accommodation to continue a conversation flow. We’ll create separate intents for these services, and the challenge is connecting these intents as part of the conversation flow with the first intent.
Dialogflow provides a feature called input/output context that controls the flow of conversation and branching. Contexts can be set for intents and can be reused further in other intents. Whenever an intent is created, we get two options: input and output contexts. For the first intent, we set the output context, and then every other intent that is to be linked to the first intent needs to have its input context defined to be the same as the output context of the first intent. This way, these two intents can be associated with each other. We can define multiple input and output contexts to configure more complex use cases.
Output context : Dialogflow activates the output context after the intent is matched.
Input context : The input context can be used to filter which intents are matched. An intent will be matched only if the specified input contexts are currently active. This means that the output context of the originating or previous intent is active.
Input and output contexts
As shown in Figure 3-2, if the user asks a travel-related query, it will trigger the Travel_info intent, and then its output context will be set to Travel. Here, the Cab and Accommodation intents are going to activate as their input contexts are defined as the Travel context. Now, if the user asks cab- or accommodation-related questions, the respective intent will be triggered based on the user query matching with the training phrases defined in the intents. But if a user’s query does not match any of the active intents, then it will go to the fallback intent.
You can download the agent that has the use case configured for the travel domain using the input and output contexts. Go to https://github.com/dialogflow34/Chat-Bot/tree/master/Chapter_3 to download the Travel_IO_context.zip file.
Import the downloaded agent into Google Dialogflow, as explained in the Chapter 2.
Output context, Travel_info
Intent: Travel_Cab
Entity type: Cab_Vendor
Entity entries: Uber
I want to book a cab from San Francisco airport to Parker guest house at 5pm
I want to travel to airport using Lyft
Show me some cab details
Cab services
Intent configuration, Travel_Cab
Defining an entity, Travel_Cab
Input and output contexts
“Action and parameters” page for Travel_Cab
Defining a response, Travel_Cab
I/O context use cases
I/O context use cases, continued
I/O context use cases, continued
In “Input and Output Contexts” section, we discussed how multiple intents can be combined to form a conversation flow using contexts. However, Dialogflow provides a feature known as follow-up intents. A follow-up intent is like a child intent of the associated parent intent. Since it will be follow-up intent, all input and output contexts will be set automatically. The follow-up intent will be triggered only after the conversation has already matched with the parent intent in a previous conversation.
Now, what’s the difference between the two if both of them are doing same thing?
Follow-up intent
As shown in the figure, if the user expression matches the training phrases of Intent A, then only their follow-up intents will be matched further, either Follow-up Intent 1 or Follow-up Intent 2. And their input contexts will be automatically set as the output context of the parent intent. Once the follow-up intent is matched, it will give a response.
You can download the agent that has the use case configured for travel using follow-up intents.
Go to https://github.com/dialogflow34/Chat-Bot/tree/master/Chapter_3 to download the Travel_Follow_up.zip file.
Import the downloaded agent into Google Dialogflow, as explained in Chapter 2.
Next, we will configure same intents as defined in earlier sections (i.e., Travel_info and Travel_Cab) and connect them using the follow-up intents feature of Dialogflow. Configure the intent Travel_info in the same manner as defined in Chapter 2.
Custom follow-up intents
Custom follow-up intent named Travel-cab
Follow-up intents, Travel_info
Follow-up intents, Travel-cab
Now, consider the scenario where the user is looking for either cab bookings or accommodations just after confirming the travel. In other words, either the cab booking intent or the accommodation intent should be triggered after the Travel_info intent.
Custom intent, Travel-accommodation
Follow-up intent, Travel-followup
Entity entries: Single, Double
Entity entries: [1-9]+\s∗nights\s∗[1-9]+\s∗days
Entity entries: Hilton, Hyatt, Embassy Suites, Sheratons
I need an accommodation.
I want to book a hotel.
What are the different hotels available?
I want to book single room in Hyatt hotel for 3 nights and 2 days.
Entity configuration, duration
Next, configure the mandatory parameters such as Room_Type, Duration, Hotel_name, from-date, and to-date in the section “Action and parameters” of the intent Travel-accommodation. The trickiest part here is how to showcase values that have been captured in the root intent as part of the Responses section of the current intent. We can refer those values with a combination of the output context of that intent and the variable name where the information has been stored.
Response
Travel use case
Cab Booking Confirmation
User Prompt - Room Type
Hotel Booking Confirmation
Dialogflow comes with a unique implementation to handle multiple intents, called intent priority. By default, all intents have a normal priority. If multiple intents have been recognized from a query, then we can set the priority, and the highest priority intent will be triggered.
For example, in the query I am traveling from the United States to the United Kingdom and I want to book a cab from the airport to the hotel. So, two intents (i.e., Travel_info and Travel_Cab) should be recognized, but the order of their execution matters. As per the defined use case, the Travel_info intent should be triggered first, and then Travel_Cab intent should be triggered. In this case, during configuration, the priority of the Travel_info intent should be set to higher than the Travel_Cab intent.
Intent list
Now, we will use the “Try it now” panel to see how our conversation system is responding with the default configuration (i.e., all intents at normal priority).
Handling multiple intents
We can solve this problem by increasing the priority of the intent Travel_info from Normal to Highest. So, whenever two intents are being recognized from a single user’s query and Travel_info is one of them, then it will always be triggered first.
Priority configuration, Travel_info
Handling multiple intents, Travel_info
Dialogflow’s natural language processing is based on machine learning. To improve the performance of the agent, the user can add explicit training data. It provides an interface for incorporating both external and internal customer interaction logs into an agent’s training phrases. You can use this feature to build a new Dialogflow agent using the logs of existing customer interactions and to improve the performance of a live Dialogflow agent using its own logs.
Agent training
Uploading a file
Agent training
Agent training
For each request, you can choose to perform an action to improve your agent.
Click the check mark icon at the top right.
The check mark will turn green; click the Approve button to update the intent, as shown in Figure 3-33.
Approving the intent
The user can also add the request to a different intent.
Intent list
Intent list
Approving the intent
Saving the intent
Go to the Google Dialogflow page.
Click the Settings button next to the agent in the left sidebar menu. This opens the agent’s settings page, as shown in Figure 3-38.
Agent settings
Here is a breakdown of the settings:
Description: Give a description of your agent.
Default Time Zone: Select the time zone of your agent.
Project ID: This is the project ID of the Google Cloud Platform project linked to the agent, as shown in Figure 3-39.
Service Account: This is the service account for authentication.
Google Project settings
Select the API version the for agent, which here is V2 API, as shown in Figure 3-40.
API Version setting
Turn the toggle button on/off to enable beta features for your agent.
Client access token: Generate a client access token.
Developer access token: Generate a new key pair with the Refresh button.
Log interactions to Dialogflow: Toggle this button to enable/disable logging of user queries.
Log interactions to Google Cloud: Toggle this button to enable/disable logging to Google Cloud. This option is not available if log interaction to Dialogflow is disabled.
Delete This Agent: This completely deletes the agent and cannot be undone, as shown in Figure 3-41. Remove all the users before deleting the agent.
Deleting an agent
The default language is set to English. You can add multiple languages.
Click “+Add locale” to add a location (if available).
Click Save after adding a language and locale, as shown in Figure 3-42.
Agent language
Hybrid (Rule-based and ML): This is for agents with a small number of examples/templates of intents.
ML only: This is for agents with a large number of examples of intents.
ML Settings options
Automatic Spell Correction: Enable this to correct the spelling in queries, as shown in Figure 3-44.
Automatic Training: Enable or disable automatic agent training each time the agent is modified, as shown in Figure 3-44.
Agent Validation: Enable this to automatically validate the agent after performing agent training, as shown in Figure 3-44.
Spell correction
Export as ZIP: Exports the agent as a zip file, as shown in Figure 3-45.
Restore from ZIP: Overwrites the current agent with the supplied zip file, as shown in Figure 3-45.
Import from ZIP: Adds intents and entities to the current agent from the supplied zip file. If any existing intents or entities have the same name as those in the zip file, they will be replaced.
Exporting and importing
Environments tab
Speech configuration
Enable Automatic Text to Speech: Toggle this button to enable the automatic conversion of text to speech for every conversation, as shown in Figure 3-48.
Output audio encoding: Select the encoding type (16-bit linear PCM, MP3, Ogg Opus).
Agent Language: Select the agent language.
Voice: Choose a voice synthesis model.
Speaking Rate: Adjust the voice speaking rate.
Pitch: Adjust the voice pitch.
Volume Gain: Adjust the audio volume gain.
Audio Effects Profile: Select the audio effects profiles that you want applied to the synthesized voice.
Experiment with Different Voice Setting: Type some text and hit Play to test different voice settings.
Agent voice configuration
Agent access configuration
Agent advanced features
Consider a scenario where you need to build a chatbot for an organization whose users are spread across different geographic locations and converse in different languages. How would you build a conversation system that can handle such diversity?
Build a base conversation system in the English language, in other words, with the intent and entity configuration in the English language and the responses defined in different languages. However, sometimes this solution may fail to capture some language-specific context that is essential for building good conversation systems.
Build a single conversation agent and define the configuration (i.e., intents, entities, parameters, and responses) specific to the language.
The preferred approach is to build an end-to-end system in multiple languages for higher accuracy. In this scenario, we need to provide training phrases for intent classification, entity variations, and parameter prompts if required in the target language. This is how Google Dialogflow implements a multilingual conversation system.
You can download the agent that has the use case configured for the travel domain in different languages.
Go to https://github.com/dialogflow34/Chat-Bot/tree/master/Chapter_3 to download the Travel_multilingual.zip file.
Import the downloaded agent into Google Dialogflow, as explained in Chapter 2.
Agent configuration
Language configuration
Multilingual chatbot, training phrases
Multilingual chatbot, “Action and parameters” page
Multilingual chatbot, response
Multilingual chatbot, Web Demo
Prebuilt agents are agents trained on common topics such as weather, traffic, unit conversion, and so on. These agents have a predefined set of intents along with training phrases, entities, and parameters. So, if you want to leverage use cases related to these common topics, then you can use them in a new agent or copy them into an existing agent.
As a continuation of our travel use case, if the user is also looking for the weather conditions of the location where he is planning to travel, then we can use prebuilt agents to configure this in our existing agent. We will start with the configuration of a prebuilt agent, which will create a new agent by default, and then we will move intents from this newly created agent to the existing one.
Prebuilt agents
Example intents of prebuilt Weather agent
New agent created, based on prebuilt Weather agent
“View all agents” link
List of agents
You can also see a list of preconfigured intents listed under the Weather agent by clicking the Intents tab in the left menu bar, as shown in Figure 3-62.
Go to the left menu bar and click the drop-down menu next to the agent name and select the Weather agent.
Weather agent’s Intents list
Weather intent configuration
The Weather agent populates some default parameters as part of the weather intent. But for now, we have removed all parameters as we just wanted to show the integration to our Travel agent. We will discuss the parameters for the weather intent in the next section.
Prebuilt agent, copying the intent
Copying the intent to the Travel agent
Intents list
Now, the weather intent has been added to our Travel agent. So, users can ask questions related to the weather conditions. Go to the “Try it out” panel and enter the query shown in Figure 3-67.
In Figure 3-67, our Travel agent is able to categorize the query (i.e., Show me weather) into the weather intent.
Trying out the weather intent
Up until now, we have integrated the weather intent and related entities from the prebuilt Weather agent to our Travel agent. Next, we will discuss how our webhook service can be used to integrate Dialogflow and the Weather API.
Integration of Weather API with Dialogflow
Fulfillment helps to integrate your service with the agent to populate dynamic responses to end users. For example, if you want to check the weather conditions in a city, then your chatbot response should be changed as per the weather conditions of the respective city.
Since Dialogflow can’t directly integrate with any third-party API, it is only possible via your service that facilitates communication between Dialogflow and third-party APIs. Webhook services perform this task. You can write webhook services in any programming language of your choice and release them as REST APIs. Please make sure your webhook service is publicly accessible and accepts only POST requests. You can deploy your webhook service either in the virtual machine or in compute functions in any cloud provider. You can download the sample code that has the use case configured for weather integration.
Go to https://github.com/dialogflow34/Chat-Bot/tree/master/Chapter_3 to download the Travel_Weather_code.txt file.
Sign-up page
Sign-up page filled in
E-mail verification page
As the next step, go to the API tab to check the freely available weather API document.
Check this API URL through a browser as well. It will show the weather forecast details, as shown in Figure 3-72.
In this case, we are using the URL to call the API with the city name: http://api.openweathermap.org/data/2.5/weather?q=DELHI&units=metric&APPID=19e8588cb3d7d0623e3a5a8ec529232f.
Showing API result in a browser
In this section, we will showcase the integration of the Weather API to Google Dialogflow.
Dialogflow calls the webhook service via fulfillment with the required input.
The webhook service processes an input request from Dialogflow and sends it to the Weather API.
The Weather API processes requests from the webhook and returns a response to the webhook service.
The webhook service converts a response from the Weather API to a format that Dialogflow can understand and send it to Dialogflow via fulfillment for further processing.
You can download the agent that has the use case configured for weather.
Go to https://github.com/dialogflow34/Chat-Bot/tree/master/Chapter_3 to download the Travel_weather.zip file.
Import the downloaded agent into Google Dialogflow, as explained in Chapter 2.
We will proceed with the weather intent that was configured in the previous section where we will integrate Dialogflow with the Weather API using fulfillment via a webhook service. For this book, we have created a webhook service in the Python programming language with a sample request and response JSON.
Configuring a parameter
Enabling the webhook call
Enabling fulfillment
Configuring fulfillment
Web Demo, weather intent identification
Web Demo, calling the Weather API
In this chapter, we discussed how to design a complex use case using the concepts of input and output contexts, follow-up intents, etc. This chapter also explained various use cases with multiple intents, enabling fulfillment, and using webhooks for integration with external APIs to further enrich the quality of the conversation in our agent. We also explained how prebuilt agents can be used to reduce the time required to configure an agent for out-of-the-box use cases.
In this chapter, we will describe some advanced use cases that leverage the capabilities of Google Dialogflow. We have already discussed fulfillment and using the webhook service to integrate with third-party APIs, so in this chapter we will focus on chatbot personality, sentiment analysis, events, and integration with third-party messengers such as Facebook. We will also look at the Google knowledge base and implement a document search feature in Google Dialogflow.
A chatbot’s personality can be defined as the human-like qualities it exhibits in its conversations with end users. When building a chatbot for an organization, the chatbot’s personality represents an important element; otherwise, the chatbot will converse in a robot-like fashion with no emotions. For personality, we can use traits such as being friendly, formal, helpful, or polite in conversations with end users.
There are various ways to implement chatbot personality. Some organizations build their own organization-specific personality, and others implement common personality questions.
What are you doing?
What is your name?
For this book, we have developed our own personality module in the Python language. For this exercise, you can go to https://github.com/dialogflow34/Chat-Bot/tree/master/Chapter_4 to download the code.
config: Contains all the configuration files for the code
data: Contains all the data files such as training data or model files
logs: Contains all the log files
models: Contains the Python code files for various models used in this code
utils: Contains the Python code files for various natural language processing (NLP) tasks
Please refer to the Python code file corpus.py inside the model package. This Python code file has a Python class that implements various functions to train and use a personality model for a user’s query, as discussed in this chapter.
In this function, we are loading training data from the MongoDB database and storing it in a pickle file that will be used to recommend a relevant answer for a user’s query.
customerName: This is used to distinguish between multiple implementations across customers. Possible values are HCL and IBM.
inputQuery: This is the user’s query (for example, What is your name?).
These NLP tasks and related code snippets are available in the Python code file nlpTask.py inside the utils package. The definitions of all of these functions are as follows:
This code snippet compares the processed form of the user’s query with questions available as the part of training data and then returns an answer to a similar question as a response to the query.
So far, we have discussed how to train the personality module and use the trained model to find an answer to a question. Next, we will see how this system can be released as a REST API that can be consumed by other applications and software.
We will expose the personality module as a local URL. Since Google Dialogflow requires the public URL of your webhook service, we have used Ngrok to generate a public URL corresponding to our local URL.
Let’s look at the steps to set up a personality webhook service on both Windows and Linux Server.
For Windows Server, Python 3.6.x and PIP need to be installed in your system before continuing.
Flask-RESTful is an extension of the microframework Flask for building REST APIs.
Windows command prompt showing installation of Flask-RESTful
This will install the package and its dependencies.
A RESTful API uses HTTP requests to get and post data.
Windows command prompt showing the deployment of the service
We can check this URL in a REST client (e.g. Postman) with some sample input JSON.
Since Google Dialogflow requires a public URL of your webhook service, we will use Ngrok to generate a public URL corresponding to the local URL that we configured earlier.
To expose a local HTTPS server, download Ngrok from https://ngrok.com/download.
The public URL is available only when the auth token is downloaded from https://dashboard.ngrok.com after signing up at https://ngrok.com/signup.
The auth token must be specified to Ngrok so that the client is tied to this account. Ngrok saves the auth token at ~/.ngrok2/ngrok.yml so that there is no need to repeat the previous steps.
Unzip the downloaded Ngrok folder and run the ngrok.exe application.
Copy the auth token from the user account to use in the command and run this command at the Ngrok terminal prompt, as shown in Figure 4-3:
Generating an auth token using Ngrok
Saving the auth token
Windows command prompt showing command to expose the HTTPS URL
Windows command prompt showing public URL
Now this REST API will be integrated with our webhook service, which integrates the chatbot personality module with Dialogflow.
This deployed API service can be used for a development environment.
For Linux, Python 3.6.x and PIP need to be installed in your system before continuing.
Linux shell showing installation of Flask-RESTful
This will install the package and its dependencies.
Create a PersonalityService.py file that will have the personality code that you downloaded from GitHub.
Linux shell showing deployement of service
We can check this URL on a REST client (e.g. Postman) with some sample input JSON that we are going to explain in this section.
To expose a local HTTPS server, download Ngrok from https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip for the Linux server.
The public URL is available only when the auth token is downloaded from https://dashboard.ngrok.com after signing up at https://ngrok.com/signup.
The auth token must be specified to Ngrok so that the client is tied to this account. Ngrok saves the auth token in ~/.ngrok2/ngrok.yml so that there is no need to repeat this step.
Unzip the downloaded Ngrok from a terminal with the following command, as shown in Figure 4-9:
Linux command for unzipping
Copy the auth token from the user account to the command and run this command at the Ngrok terminal prompt, as shown in Figure 4-10:
Linux command to save auth token
After the previous step, the auth token gets saved to the configuration file.
Linux shell showing command to expose the HTTPS URL
Public URL
For more details, please refer to the Ngrok documentation at https://ngrok.com/docs.
This deployed API service is feasible for a development environment.
In this section, we’ll see the sample request JSON that is supplied on the Dialogflow end and the response JSON that is defined inside the code for our personality webhook service.
Since our webhook service and chatbot personality API is ready, you can either download the agent file that has the personality use case by accessing the GitHub URL (https://github.com/dialogflow34/Chat-Bot/tree/master/Chapter_4) or configure the personality use case by following the steps explained in the “Personality Intent” section.
Import the downloaded agent into Google Dialogflow, as explained in Chapter 2, if you are working with downloaded agent file from GitHub.
Create an intent named Personality in our Travel agent.
Define these training phrases, as shown in Figure 4-13:
What is your name?
How are you?
What are you doing?
What is your favorite color?
Chatbot personality, intent configuration
Here, we have defined some personality questions in the Personality intent so that it can be passed on to our webhook service, which in turn makes a call to our chatbot personality service for an answer.
Option “Enable webhook call for this intent”
Integration with chatbot personality
So, our chatbot personality service via the webhook service using fulfillment has been integrated with Google Dialogflow.
Now, go to the left sidebar menu and click Integrations. Enable the toggle button for Web Demo and open the URL in a browser of your choice.
What are you doing?
What is your favorite color?
Web Demo, chatbot personality
The system responds with the relevant answer from our webhook service that is integrated with chatbot personality service.
Please note that we can integrate the personality webhook service into any personality service provided by other providers, but we need to ensure that the webhook service processes the input and output JSON in the same format as mentioned earlier.
Sentiment analysis can be defined as the process of identifying the user’s opinion from text. Sentiment analysis plays an important role in responding appropriately to the user. It classifies text into three categories (positive, negative, and neutral) based on the user’s opinion of their experience with the services offered. Once we have done that, we can tailor the responses of our chatbot to reflect the sentiment of the user. As an example, if the user is highly negative about something, we can begin by reassuring them or showing concern in our response.
Sentiment analysis in Enterprise Edition
Here, we are going to show a sample sentiment analysis system that returns the sentiment of the user (i.e., positive, negative, or neutral). This has been exposed as a REST API. You can download the sample sentiment analysis code that has the use case configured for sentiment analysis.
Go to https://github.com/dialogflow34/Chat-Bot/tree/master/Chapter_4 to download the Sentiment_Analysis_code.txt file.
For this book, we have used the Deeppavlov library for the implementation of the sentiment analysis system. This library implements the Google BERT model to train the sentiment analysis system. The Deeppavlov library uses training data in CSV format, where each data point is a pair of sentences and their corresponding opinions (i.e., positive or negative).
For inference, the system processes the user’s query and provides the sentiment (i.e., positive or negative) as a response to a query.
We have used the following sample request JSON that is supplied on Dialogflow’s end and the response JSON that is defined inside the code for our webhook service (REST API) for sentiment analysis.
Now, we will explain the complete codebase present in the downloaded file named Sentiment_Analysis_code.txt.
The value of one argument of this function (i.e., download) is False. This is required if we don’t want the model to be downloaded every time we make a function call to get an instance of the sentiment model.
You can also use natural language services from any of the cloud vendors such as Google’s Cloud Natural Language API (https://cloud.google.com/natural-language/docs/analyzing-sentiment) for sentiment analysis. Since Google Dialogflow requires a public URL of our webhook service, we have used Ngrok to generate a public URL corresponding to our local URL, as explained in the previous chatbot personality use case.
The sentiment analysis webhook service integrates with this system to get an opinion and send it to Dialogflow using fulfillment.
Now, to configure the agent, you can either download the agent file (Sentiment_analysis.zip) that has the sentiment analysis use case by accessing the GitHub URL (https://github.com/dialogflow34/Chat-Bot/tree/master/Chapter_4) or follow the next steps to configure the use case from scratch.
Import the downloaded agent into Google Dialogflow, as explained in Chapter 2, if you are working with downloaded agent file.
Now, let’s see the steps to create a sentiment intent.
Service was good.
I am not satisfied with your services.
I didn’t like the options provided by you.
In the left sidebar menu, click Fulfillment and then toggle the button next to Webhook.
Enter the webhook service URL for sentiment analysis and click the Save button, as shown in Figure 4-18.
Integration with sentiment analysis, webhook
So, our sample sentiment analysis system via the webhook service using fulfillment has been integrated with Google Dialogflow. Now, go to the left sidebar menu from the sentiment intent page and click Integrations. Enable the toggle button for Web Demo and open the URL in a browser of your choice.
Integration with sentiment analysis, Web Demo
Please note that we can integrate the sentiment analysis webhook service with any sentiment analysis service, but we need to ensure that the webhook service will process the input and output JSON in the same format as mentioned earlier.
In the previous section, we integrated our Travel agent with sentiment analysis to identify the sentiments of a user from the feedback text. Sentiment identification has another use case as well. Specifically, output from sentiment analysis can be used to transfer a conversation to a human agent if the customer is not satisfied with the responses of our agent.
It also offers flexibility to change responses, to call any third-party APIs, etc. All this can be accomplished using events in Dialogflow.
Events in Google Dialogflow are used to trigger an intent not from the end-user expressions but based on the feedback response. We can leverage the same feature for this use case to trigger an opinion-specific intent.
Now, we need to create two intents: one for positive opinions (Issue_resolved) and one for negative opinions (Transfer_to_agent) in our Travel agent.
The user will give feedback to the Travel agent, which will trigger the sentiment analysis intent and call the sentiment analysis webhook service. Then the webhook service processes the input request and calls the sentiment analysis system to extract an opinion from the feedback text, as discussed in previous section and as shown in Figure 4-20.
Now, based on the response from the sentiment analysis system that is either positive or negative, the webhook service will set an event. For example, if the response is positive, then it will set an event to issue-resolved; and for a negative response, it will set a transfer-to-agent event. Once an event has been set, the webhook service generates the JSON response and sends it to Dialogflow via fulfillment.
Event configuration, design flow
Create an intent with the name Issue_resolved. Then, go to the Events section from the Issue_resolved intent page.
Define the event name as issue-resolved, as shown in Figure 4-21.
Event configuration, issue-resolved
Event configuration, Issue-resolved (response configuration)
Event configuration, Transfer_to_agent
Event configuration, Transfer_to_agent (response configuration)
Here we will be using our existing sentiment analysis webhook service with a small change in the response JSON. Now, the webhook service will send information about events that will trigger an associated intent. The sample request and response JSON is as shown next. As you can see, the webhook service responds with the event name that will be used.
Now, go to the left sidebar menu from the agent page and click Integrations. Enable the toggle button for Web Demo and open the URL in a browser of your choice.
Event configuration, Web Demo
In this section, we will go through the steps to integrate Google Dialogflow with third-party messengers such as Facebook and Slack. This example will help the end user to converse with our agent via Facebook Messenger.
Log in to a Facebook account. Create one if doesn’t exists.
Creating a page
Facebook Conversation_Utility page
Facebook page for developers
Create a new app ID by providing a display name and contact e-mail. For this example, we’re using the following:
Display Name: Dialog_flow
Creating an app ID
Facebook app page
Click the Set Up button below the Messenger block to add the product.
Access Token
Generating a token for our page
Generated token
Google Dialogflow integrations
This will open a dialog where you can provide a value for the verify token. This can be any string (i.e., facebook-integration).
Copy the callback URL, which will be required for the Facebook configuration page.
Entering the verify token and copying the callback URL
Facebook webhook configuration
Paste the copied callback URL and verify the token name as specified during the integration configuration in Google Dialogflow.
Entering the callback URL
Adding subscriptions
Edit Page Subscriptions dialog
Add button
Adding a button to our page
Facebook page
Facebook Messenger integration in action
In this section, we will implement a document search engine feature in Google Dialogflow.
It is not feasible to define intents and their training phrases for all types of use cases. It works better if we want to perform a specific action, say, fetching weather information for a particular city, finding available hotel accommodations, etc. But for other use cases, say, places to visit anywhere in the United States, it is not possible to define intents for such types of queries. These types of query generally fall into the category of long-tail queries where the answers are within relevant documents.
Google Dialogflow can be integrated with any document search engine service provided by various NLP providers using fulfillment via the webhook service.
Google Dialogflow also has a built-in connector to build a knowledge base where we can upload documents either manually or by specifying a location in cloud storage where the documents exist. The knowledge connector handles the user requests with answers extracted from the uploaded documents. In Google Dialogflow, this is a beta feature, which means it can’t be used in a production system. By the time you are reading this book, however, it is possible that the system will be out of beta and ready for use.
In the next sections, we will focus on Google Dialogflow’s built-in knowledge connector to build a search engine and integrate it with our Travel agent. Since Google Dialogflow’s knowledge base is in beta, we need to enable beta features for our agent.
Go to the Dialogflow console and select the Travel agent.
Click the settings button next to the agent name, i.e., Travel. Go to the General tab and click the toggle button for “Enable beta features and APIs.” Click the Save button to save the changes.
Enabling beta features
To create a knowledge base, go to the left sidebar menu and click the Knowledge tab.
Configuring the knowledge bases
Follow these steps:
Click the CREATE KNOWLEDGE BASE button.
Fill in the knowledge base name (i.e., Travel-UseCase).
Click the SAVE button, as shown in Figure 4-46.
Naming the knowledge base
Enter Places to visit in UK for Document Name, set Knowledge Type to Extractive Question Answering, set the MIME type to application / pdf.
To upload the documents, choose any option: File on Cloud Storage, URL, or Upload file from your system. Here we will show how to upload a document from our system.
Click the SELECT FILE button and select the files from the system that you want upload to the knowledge base.
Click the CREATE button to finish the configuration, as shown in Figure 4-47.
Document upload
Document list
Knowledge base, response
Now, if the user asks a question for which no relevant intents exist in our agent, the chatbot will extract the relevant results from our knowledge base.
Web Demo, knowledge base
A spell correction feature plays an important role in a chatbot system. This enables a chatbot system to be smart enough to capture spelling mistakes in user queries and responses.
Google Dialogflow provides a built-in spell correction feature. Please note that if there are any spelling mistakes in entity values, they should be corrected by the entity fuzzy matching feature first, before passing the values on to spell correction.
We can also integrate our chatbot with a spelling correction service provided by any other NLP provider using fulfillment via a webhook service that integrates with the spell-check service.
We will start by demonstrating our existing Travel agent without the spell correction feature enabled, where the agent is not able to recognize spelling mistakes.
Chatbot without spell correction enabled
To enable spelling correction, click the settings button next to the agent name (i.e., Travel).
Enabling spelling correction
Chatbot with spelling correction enabled
Chatbot response, spell correction
In this chapter, we discussed the importance and implementation of chatbot personality. We looked at different scenarios for sentiment analysis such as transfer to a live agent. We also discussed the integration of Google Dialogflow with third-party messengers such as Facebook Messenger. Also, we implemented a document search feature using the Google Dialogflow knowledge base. Finally, we implementing spelling correction in our chatbot.
Enterprises working on chatbot systems have already started seeing benefits from their deployed cognitive virtual assistants. They are working to implement even more features to enrich conversation systems and improve the user experience.
Some new features such as designing conversations from existing chat logs were impossible just a few short years ago. Today, cloud vendors such as Google and IBM Watson have implemented capabilities in their chatbot systems to recognize intents automatically from existing chat logs; however, this requires chat logs to be in a specific format. In a practical scenario, it is highly unlikely that a chat log follows any specific format. Thus, this feature is still being perfected in the field of chatbot systems.
Another common request from organizations working on a chatbot system is that they want to avoid designing the same use cases across multiple engagements. How do they design one type of use case and then reuse it? Some organizations have been able to address this by designing templates corresponding to use cases and reusing them for other engagements. This also offers the capability to change responses according to the specifics of the new use case.
Increasing demand for chatbot development in various domains, such as e-commerce, healthcare, online booking services, etc., has encouraged new research into some exciting features of chatbot and conversation systems.
These features include generating responses in natural forms, designing an end-to-end conversation flow from an existing conversation’s data, generating a response from a bot based on a user’s personality, etc.
What: This means recognizing the intent and entities from the end-user expression.
Who: To whom should it be addressed? Is it a chatbot or user?
How: How should a response be generated? Either it will be a response or an answer from a document.
When: When should a reply be sent? In other words, it will be sent after 10 seconds, 20 seconds, 1 minutes, etc.
Nowadays, chatbots with the capability to generate natural language responses are expected to make conversations more intuitive or human-like. Several patents and research papers exist on this feature, but this is difficult to implement in practice because of the different types of data in existence such as text, audio, images, video, etc. It is also difficult because of a lack of sufficient training data for various domains such as healthcare, insurance, etc. One of the patents uses the concept of how an individual communicates with the chatbot system and learns structure from those conversations to generate responses. Therefore, you may see different response text for the same query from different users, but all responses will have the same meaning.
The current implementations of cognitive virtual chatbots (CVAs) lack the capability to generate an end-to-end conversation flow from chat logs and to automatically generate dialogue. This is the most important research area, and most of the big industry players are actively working on the topic. One of the patents in this area implements this by analyzing conversation flows to understand the meaning of each line of text and by connecting each distinct response from both sides to finally generate conversation flows. The same approach can be used in spoken dialogue systems to automatically generate dialogue.
In current CVA systems, goal-oriented dialogue agents are generally trained to complete a specific goal. For any other new task, you may not find a dataset consisting of human-to-chatbot interactions. In such cases, a dialogue developer will need to develop this from scratch for all possible interactions. This is a time-consuming process and requires continuous support from developers. This problem has been well studied by Google, and its AI track has introduced a new framework known as machine-to-machine (M2M) that automates the generation of an end-to-end dialogue agent for goal-oriented systems. As an example, CVA developers now can use an existing dialogue agent to generate a new dialogue agent for tasks such as booking an online appointment.
A lot of research in the AI field for chatbots is being led by various startups, and some of these good ideas have been acquired by industry majors. Deepmind, the creators of AlphaGo, was acquired by Google in January 2014, for $500 million. Some of its cutting-edge technologies have now been integrated into Google Assistant. Another example is that Siri was acquired by Apple in 2010 to enable virtual assistant capabilities in iPhones. Further examples include Wit.ai, which was acquired by Facebook, and Maluuba, which was acquired by Microsoft, to expand their AI ecosystems.
Research in the area of human conversations is an exciting and active field where every day new discoveries are being made and new technologies are being implemented to make cognitive virtual agents better. The day is not far that we will not be able to distinguish a CVA from a human being.