The art of Conversational Chat-bots

Jyotsna
Voice Tech Podcast
Published in
8 min readMay 10, 2020

Too tired of writing codes? Need a basic chatbot for your website? This is just the right article for you!

Before proceeding to development let me list out a few basic advantages of chatbot:

· 24-hour a day availability at reduced costs.

· Ease of use on the web and measurable performances.

· Accessibility to very large amounts of updated knowledge.

· Compatibility with all devices, including mobile, social networks, and even SMS

There are many examples that have become well-known successful use cases. For example, retailer H&M uses them to guide users through their purchase process on their website. In general, many support systems use chatbots to achieve operational efficiency, including answering common questions or helping users solve repetitive tasks.

There are many types of chatbots that can be found online. Some of them do not require programming skills, much less knowledge of machine learning or natural language processing. Examples of this kind of chatbots are Rasa, Octane Ai, Massively, or ManyChat.

There are also very common systems that use AIML (Artificial Intelligence Markup Language) to model smart conversational systems. Today, the most widely used chatbots are those made available by major vendors such as Google, AWS and Microsoft. We all know Alexa, Cortana, and Google Assistant. You should carefully consider their service offerings when you are choosing the technology stack for your chatbot. All three of these big vendors provide reliable and scalable cloud computing services that will help you to implement and customize your chatbot according to your needs. By now, most famous platforms to easily create text or voice-based bots are the following:

· Dialogflow (Google, formerly Api.ai)

· Azure Bot Service (Microsoft)

· Lex (AWS)

· Wit.ai (Facebook)

· Watson (IBM)

How to start with Dialogflow chatbot framework

Among all the services taken into consideration, Dialogflow is certainly one of the most impressive. The power of Google’s machine learning makes the difference: the natural language processing (NLP) engine is among the best on the market. As indeed its slogan recites: “Dialogflow is user-friendly, intuitive and makes sense”. It is also very easy to integrate with Google Cloud Speech-to-Text and third-party services such as Google Assistant, Amazon Alexa, and Facebook Messenger.

Now let me help you get familiar with DialogFlow, but first, a little about the project as I will be drawing inferences from it in the course of this article:

Creating your first chatbot — RepoFinder (Project Name) — RepoFinder helps you find open source development libraries from Github, based on your input.

You can get the full project with source code on Github. https://github.com/jyotsnatiwary/RepoFinder-Chatbot

Tools and technology used

Dialogflow: Bot framework to create intelligent chatbots, which you can later integrate with your apps.

Node.js: To define the fulfillment logic, which eventually processes the data.

Kommunicate: Chat interface to query the chatbot and display the resulting output.

Dialogflow is based on two main concepts: intent and context. The intent is to accurately identify the purpose of the sentence that the user has sent to the bot. On the other side, the context is used to give coherence and fluency to the discussion, preserving the key concepts that have already been used in the conversation.

Creating Agent

Sign up for Diaogflow and open your dashboard. The first step is creating an agent, which essentially is the bot you are building. You can create one through the console by following these instructions. In our case, we’ve named it RepoFinder.

Creating Intents

Next, you will have to create Intents. Intents basically help the bot perceive the user’s input and decide the subsequent action. Intents can be created both from the console or by calling APIs.

DialogFlow by default gives two intents: Default Fallback Intent, Default Welcome Intent.

More Information on creating Intents can be found here.

Creating Entities

Once Intents are created, you need to define entities. Entities are powerful tools used to extract parameter values from the user’s query. Any actionable data that you want to get from a user’s request, should have a corresponding entity.

Consider our agent RepoFinder. The user says “Tell me the best Chat SDK on GitHub” — this should tell the agent that the user needs some info on Chat SDK.

So how do you configure the agent to do it? Well, for each user expression mapped to an intent, the agent needs to figure out the respective input that the user wants info about. This, the agent does with the help of Entities.

So for each intent that you create, every user expression should contain a corresponding entity, which your bot agent needs to figure out. Now by default, the agent can’t do that — you need to train it to do so.

Training the Agent

Dialogflow provides a training tool that allows you to add annotated examples to relevant Intents in bulk. It helps to improve the classification accuracy of the agent. Here you will also receive a log of all the queries sent to your agent and what the agent responded in return.

This is very useful if you tell your agent something and it responds with an output you don’t like. It can also be helpful if you realize later on that you forgot a synonym of an entity and users are using that a lot, then you can go and tell your agent what to do in that case.

Actions

Now your chatbot is all set to function. Every time it receives a query, it will first capture the intent and then extract the entity. The next step is to generate a response, which the user is ideally seeking. This you can do by leveraging webhooks to fetch data from external sources (GitHub API server in our case). You can do this in the Fulfillment Section, by specifying the webhook URL.

The penultimate step is to tell the intent to use this webhook to respond with the data that was returned from it.

Build better voice apps. Get more articles & interviews from voice technology experts at voicetechpodcast.com

Integrate the chatbot into your website

There are two ways that you can integrate a Dialogflow chatbot into your website: using a widget or using Python.

1) Using a widget

The easiest way to integrate Dialogflow into an HTML page is to use the iframe. Select “Integrations” from the menu on the left and make sure that “Web Demo” is enabled. Simply copy and paste the HTML code to view the agent directly on your site.

[Integrate the chatbot in your website using the iframe]

2) Using Python

The following script allows you to call Dialogflow using Python 3. You can find the client on GitHub for free. The script initializes a client session that takes the intent as input and finally returns a response, the so-called “fulfillment”, and the corresponding confidence as a decimal value. The sentence for which we want to get an answer is saved in the variable named “text_to_be_analyzed”. Edit the script by adding your sentence. Using Python, it is easy to create more custom logic. For example, you can catch a particular intent and then trigger a custom action.

Install the following requirements:

dialogflow 0.5.1

google-api-core 1.4.1

import dialogflow

from google.api_core.exceptions import InvalidArgument

DIALOGFLOW_PROJECT_ID = ‘google-project-id’

DIALOGFLOW_LANGUAGE_CODE = ‘en-US’

GOOGLE_APPLICATION_CREDENTIALS = ‘1234567abcdef.json’

SESSION_ID = ‘current-user-id’

text_to_be_analyzed = “Hi! I’m David and I’d like to have some SDK libs, can you help me?”

session_client = dialogflow.SessionsClient()

session = session_client.session_path(DIALOGFLOW_PROJECT_ID, SESSION_ID)

text_input = dialogflow.types.TextInput(text=text_to_be_analyzed, language_code=DIALOGFLOW_LANGUAGE_CODE)

query_input = dialogflow.types.QueryInput(text=text_input)

try:

response = session_client.detect_intent(session=session, query_input=query_input)

except InvalidArgument:

raise

print(“Query text:”, response.query_result.query_text)

print(“Detected intent:”, response.query_result.intent.display_name)

print(“Detected intent confidence:”, response.query_result.intent_detection_confidence)

print(“Fulfillment text:”, response.query_result.fulfillment_text)

As you can see, the function requires a session_id. This is nothing but a value that allows us to recognize the session in which you are working. For this purpose, I suggest that you use the ID of the user to retrieve it easily.

Finally, in order for the Python code work properly, you will need a fresh token to call the artificial intelligence of our chatbot. In fact, the V2 (version 2) of the Dialogflow API relies on an authentication system based on a private key associated with the Google Cloud Platform Service Account, instead of the access tokens. Please follow the tutorial here to accomplish this step. Through this simple procedure, it will be possible to obtain a private key in the JSON format. Be sure to store the file in a safe place because if you lose the key, you will have to generate a new one by going through the whole procedure again.

The image below demonstrates the integration architecture and the sequence of information flow:

User comes to the website and asks for the required library.

The website uses Kommunicate APIs to send messages to the application server.

The application server sends the query to Dialogflow agent.

Agent uses machine learning algorithms to match the user’s requests to specific intents and uses entities to extract relevant data from them → thereby processes natural language to convert it to actionable data.

RepoFinder returns the actionable result to the application server

A part of Response of RepoFinder look like this:

{“parameters”: {“keyword.original”: “chat”,“keyword”: [“chat”]}}

Dialogflow agent’s response with all the details available here.

Application server calls Github search APIs/internal database to get the list of libraries related to the keyword.

The application server then sends the list of libraries back to User again by calling Applozic APIs.

That is pretty much all you need to do in order to build a simple bot in Dialogflow. Just play around with Intent, Entities, and Action to build your own flow and chatbot. We can’t deny the fact that chatbots are going to handle most of our customer interactions; starting now would definitely set you on pace with your competitors.

If you’re just starting with Dialogflow and this post has piqued your interest, I highly recommend you starting with the “prebuild agents.” These are customizable agents specialized in different areas of knowledge that you can simply import into your chatbot. Then you can set up a webhook as described in this post and get the agent responding. All the intents and even entities of the agent are editable and ready to use. Feel free to add more functionalities directly from the Google Cloud Platform or enhance your algorithms with NLP.

Something just for you

--

--