Blog: Build AI Virtual Assistant for Travel Portal Using DialogFlow
In this blog, we shall learn how to build an AI virtual assistant or a chatbot, with Google Dialogflow. Chatbots are used in applications such as e-commerce customer service, Internet gaming and much more. Firstly, you will be introduced to Google Dialogflow, a conversational interface for bots, devices, and applications. Next, you will learn the basic building blocks of Dialogflow such as agents, intents, entities, annotations, and fulfillment. I will be creating a chatbot for a travel agent.
What exactly is a chatbot?
A chatbot is an Artificial Intelligence (AI) software that can simulate a conversation (or chat) with a user in natural language through messaging applications, websites, mobile apps or through the telephone.
Why are chatbots important?
A chatbot is often described as one of the most advanced and promising expressions of interaction between humans and machines. However, from a technological point of view, a chatbot only represents the natural evolution of a Question Answering system leveraging Natural Language Processing (NLP). Formulating responses to questions in natural language is one of the most typical Examples of Natural Language Processing applied in various enterprises’ end-use applications.
How does a chatbot work?
As you can see in this graphic, a chatbot returns a response based on input from a user. This process may look simple; in practice, things are quite complex.
- User request analysis:
This is the first task that a chatbot performs. It analyzes the user’s request to identify the user intent and to extract relevant entities.
- Returning the response:
Once the user’s intent has been identified, the chatbot must provide the most appropriate response for the user’s request.
There are different chatbot platform tools like Dialogflow, Amazon Lex, IBM Watson, etc. We are creating a travel agent chatbot with Dialogflow which is owned by Google.
What is Dialogflow?
Dialogflow is an end-to-end, build-once deploy-everywhere development suite for creating conversational interfaces(chatbot) for websites & mobile applications, etc. It provides natural and rich conversational experiences.
Setting up Dialogflow account
Navigate to Go to console in the official website.
After navigating to console you will be prompted to sign in with Google, go ahead and sign-in.
After successfully signing in you can see a dashboard as shown below
In the above picture, you could see Create Agent tab. Agent is nothing but the bot that you would like to create. We are creating the chatbot for travel so let’s give our agent name as Travel (agent names are a personal choice) and click on CREATE button. After creating successfully you could see different tabs on the left side of the screen like
4. Integrations etc.
In Dialogflow, the typical flow of any conversation involves these steps:
- The user giving input.
- Dialogflow agent parsing that input.
- Agent returning a response to the user.
Identifying intents is crucial for any conversation, as we discussed earlier it is a part of the process.
Before going to create an Intent, let’s go to the Small Talk tab and Enable it. Because we know, when some users want to interact with our bot. They might want to exchange greetings. By default, Diagflow has this intent set already. All we need to do is enable it. When enabled, The bot will know when the user is greeting and would reply with the appropriate response.
Now Enable the Small Talk as indicated in the above image. You can also customize the responses in Small Talk Customization Progress section. Once you are done save the changes.
Intents in our agent map the input provided by the user.
In the above Intents screen, we can see a Default Welcome Intent and Default Fallback Intent.
In Default Welcome Intent, you can see multiple training phrases in Training phrases section. In Training phrases section you define examples of user utterances that could trigger the intent.
When user input matches anyone in the above expressions (training phrases) then the user receives the response that is defined in the Responses section as shown below.
In Default Fallback Intent you can see multiple responses have been defined. This Intent will be triggered when the user input doesn’t match to any Intent in the agent.
Now let’s go to the Intents tab and create a new Intent called Book a flight ticket by clicking CREATE INTENT button on top.
Step 1: Click on CREATE INTENT button.
Step 2: Give Intent name and SAVE.
After creating the Intent go to the Training phrases section and add a few expressions that would match the user input.
Now add the response in the Responses section when this Intent should be triggered.
We have added expressions (training phrases) and responses to our Book a flight ticket Intent. Once you are done save the changes.
So far, we created an Intent for booking a flight and now let’s see how it works. You can see an input as “Try it now” on the right side of the screen. We could test there.
When a user passes an input then the response will be received from the Intent that matches with the input. If there is no match found then the user receives a response from Default Fallback Intent.
You can see in the first image that the input matches to the Default Welcome Intent and received the response from that Intent. As same as in the second image if you see the input (user says) matches to the Book a flight ticket Intent. Where in the third image there is no match found to the input, so the response received from Default Fallback Intent.
The above figure shows how Dialogflow matches user input to intent and responds.
Actions and Parameters
Now let’s move further in this. Go to our Book a flight ticket Intent and add few more expressions like in the below image.
In newly added training phrases you can see highlighted words which are extracted as parameters at runtime. You can see the Action and parameters section that contains a table with parameters. Once you annotate your training phrases, the corresponding parameters automatically appear in this table.
In the above image, you can see geo-city and date entities are automatically annotated as @sys.geo-city and @sys.date.
You can make these parameters as required by just selecting them in the REQUIRED field as shown in the below image.
Once you select the parameter as required then you can see the PROMPTS field where you can define the prompts for that parameter.
Once you click on the “Define prompts…” of the parameter where you could define prompts that what should be asked when the user didn’t pass or mention that parameter.
Now let’s go to Responses section again. You could also specify that a parameter extracted from the user’s input be used in your response. First, let’s delete all the responses in this Intent (Book a flight ticket) and then add a new response as shown in the below image.
In the above image, there are $geo-city and $date which refers to the parameter value the user provides. If you see the above response, it is asking for confirmation. So, we need to create follow-up intents for this intent. After adding the response don’t forget to save it.
To create a follow-up intent for Book a flight ticket Intent follow these steps:
- Click on the Intents tab in the navigation.
- Hover your cursor over the Book a flight ticket Intent.
- Click the Add follow-up intent.
After clicking Add follow-up intent you shall get a drop down with few options.
Now click on yes, then a follow-up intent shall be created for Book a flight ticket Intent.
After creating the follow-up intent successfully, navigate to the Book a flight ticket Intent. You can see an output context has been created as Bookaflightticket-followup in the Contexts section.
When applied to an intent, an output context tells Dialogflow to activate a context if it’s not already active or to maintain the context after the intent is matched.
Read more info
- Multiple output contexts can be applied to an intent, allowing for finer intent matching control.
- You can adjust the lifespan of an output context to set the number of conversation turns the context is active for by clicking on the number.
Now let’s navigate to the Book a flight ticket — yes follow-up intent. In this intent, if you go to the Contexts section there is an input context has been created.
When applied to an intent, an input context tells Dialogflow to match the intent only if the user utterance is a close match and if the context is active.
Read more info
While intents are matched when a user replies with something similar to defined training phrases, contexts attached to a session can force an intent to be matched.
In the same follow-up, intent let’s go to the Training phrases section where you can see pre-defined expressions.
This Intent will be triggered after the response received from Book a flight ticket intent and when the user replies with any one of the expressions that match the above training phrases. We can also extract the parent intent’s parameters in the follow-up intent with context. Let’s go to the Responses section in this follow-up intent and response as follows
In the above image, there are #Bookaflightticket-followup.geo-city and #Bookaflightticket-followup.date which refers to the parameter value of geo-city and date in Book a flight ticket Intent (parent intent). After adding the response don’t forget to save it by clicking the SAVE button on top.
* Whenever you have done any changes in any intent you must save them. After clicking the SAVE button the intent will be saved and then Agent will get trained.
Now let’s test the Intents that have been created in a Travel agent. We can test on different platforms like Web Demo, Facebook Messenger, Slack, etc. We are going to test in the Web Demo.
- Navigate to Integrations tab on the left side.
- Enable Web Demo.
- Navigate to the link that is displayed after enabling the Web Demo.
After clicking the link you will be navigated to the new tab as shown below.
In the above image, you can see a chatbot on the right side with the Agent name that you created. You can test your intents here.
If you see the left image user says “Hello” and it matched to the Default Fallback Intent and received the response from that intent. Next in the right image user asks to book a flight ticket and it matches the Book a flight ticket Intent and received the Prompt that defined for geo-city parameter which is required.
Once the Intent receives all required parameters then it will send a response. In the above-left image, the user provides all required parameters which are valid. Where if you see the above-right image user passes wrong value instead of a city name so that Intent will resend the prompt until it receives the valid input.
In the left image the Intent asking for confirmation. If the user replies as yes or anything that matches to the Book a flight ticket — yes follow-up intent then the user receives a response from that intent or if the user replies as no or anything that matches to the Book a flight ticket — no follow-up intent then the user receives a response from that intent. If the user input doesn’t match to any follow-up intent then the user receives a response from Default Fallback Intent.
In the first image, the user replies as yes which matches Book a flight ticket — yes follow-up intent and received the response from it. In the second image user replies as yes which matches Book a flight ticket — no follow-up intent and received the response from it. In the third image, the user replies with some random input, so the response triggered by Default Fallback Intent.
That’s it folks, Thanks for the read!
Any queries, please drop them in the comment section.
This story is authored by Venu Vaka. Venu is a software engineer and machine learning enthusiast.