Software Development

Chat Bot using Microsoft Bot Framework

Chatbot is a buzzword that has been in highlights for past few years. The use of chatbots has been significantly increasing. The use cases are numerous. So we decided to choose this topic for our tech session this afternoon.

We are going to cover below concepts in this blog post.

  1. What is Chat Bot?
  2. Concepts
  3. A “Hello World” Microsoft Bot Framework
  4. QnA Maker
  5. LUIS bot
  6. Dispatch tool

What is chat bot?

Chatbot is an AI based program that simulates human conversations. One can also call it digital assistant that understands human language. Bots interpret and process the user requests and give prompt relevant answers. Bots can go through voice as well as text and can be deployed across websites, applications and messaging channels such as Facebook Messenger, Twitter or WhatsApp.

In the scope of this blog, we will be writing about Text based chat bot. The response of the chatbot is very similar to human response. Basically, in other words, it’s a conversational UI which uses text, graphics (such as cards or images) and/or speech.

High level components of a ChatBot

Concepts

So, before creating the bots with Microsoft framework, it is very important to understand “HOW EXACTLY THE BOT WORKS”.

Every interaction between the user and the bot generates an activity. Few important terms and concepts used in Microsoft bot framework are given as below.

Activities flow of the bot.

Azure Bot Service :- It will facilitate the communication between the channel and Bot Application, so that every interaction between a user and a Bot will be treated as an activity. Therefore, activity can be of different types such as simple text, suggested action, attachments etc.

Channel :-It is a platform used to chat or interact with bot like Microsoft Teams, Skype or Facebook. Each channel can include the additional information in the activities which they send across.

Bot Application :- It is basically the Bot Code, which will send a response or process the activity which you get from the user.

Activities :- Arrive at the bot from the Bot Framework Service via an HTTP POST request. The bot responds to the inbound POST request with a 200 HTTP status code. Activities sent from the bot to the channel are sent on a separate HTTP POST to the Bot Framework Service. This, in turn, is acknowledged with a 200 HTTP status code.

Within the Bot Framework SDK, a turn consists of the user’s incoming activity to the bot and any activity the bot sends back to the user as an immediate response. 

The turn context object provides information about the activity such as the sender and receiver, the channel, and other data needed to process the activity. It also allows for the addition of information during the turn across various layers of the bot.

Activity Processing Stack Let’s drill into the previous diagram with a focus on the arrival of a message activity.

The adapter, an integrated component of the SDK, is the core of the SDK runtime. The activity is carried as JSON in the HTTP POST body. This JSON is deserialized to create the Activity object that is then handed over to the adapter with a call to process activity method. On receiving the activity, the adapter creates a turn context and calls the middleware.

Activity Handler: When the bot receives an activity, it passes it on to its activity handlers. Under the covers, there is one base handler called the turn handler. All activities get routed through there. That turn handler then calls the individual activity handler for whatever type of activity it received.

For example, if the bot receives a message activity, the turn handler would see that incoming activity and send it to the OnMessageActivityAsync activity handler. Likewise, your logic for handling members being added to the conversation will go in your OnMembersAddedAsync handler, which is called whenever a member is added to the conversation.

Middleware :- Middleware is much like any other messaging middleware, comprising a linear set of components that are all executed in order, giving each a chance to operate on the activity. The final stage of the middleware pipeline is a callback to the turn handler on the bot class the application has registered with the adapter’s process activity method. The turn handler is generally OnTurnAsync

Managing State :- State management is used to store and retrieve the current context of the conversation and user information. Bot has provided two classes to implement Bot State Management i.e Conversational State which keeps track of the conversation & User State which keeps track of the user information.

Dialogs :- Dialogs are structures in your bot that act like functions in your bot’s program; each dialog is designed to perform a specific task. You can specify the order of individual dialogs to guide the conversation, and invoke them in different ways – sometimes in response to a user, sometimes in response to some outside stimuli, or from other dialogs.

  • Adaptive dialogs use an event-driven model for conversations. They offer several built-in capabilities, including interruption handling, attaching a recognizer to each dialog, using the language generation system, and more. With adaptive dialogs, you can focus more on modeling the conversation and less on dialog mechanics.
  • Waterfall and prompt dialogs combine multiple steps, allowing your bot to follow a predefined sequence and pass information from one step to the next.  Prompts are a two-step dialog. First, the prompt asks for input; second, it returns the valid value or starts from the top with a reprompt.

A “Hello World” in Microsoft Bot Framework

Step 1:- Download & Install Bot Framework v4 SDK Templates for Visual Studio. Following is the link:-

https://marketplace.visualstudio.com/items?itemName=BotBuilder.botbuilderv4

Step 2:- Download & Install the Bot Emulator. Under given is the link:-

https://github.com/Microsoft/BotFramework-Emulator/releases/tag/v4.8.1

Step 3- Choose the EchoBot template, Rebuild the Project and Run it.

Step 4- Your Bot is ready! You can test your bot in the Bot Framework Emulator by connecting to #url#

Step 5- Open Bot Emulator. Paste the URL, leave the optional fields and Click Connect. That’s it. You can test your bot now :)]

Q n A Maker

One of the most common uses for bots is Answering questions. This can be explaining Standard Operating Procedures, contract terms, support queries, or anything else. Microsoft Azure uses QnAMaker.ai framework to help us achieve this.

QnAMaker.AI is a AI based model, where we can add our define the context of the problem. Design a context model, where a customer can enter through Natural Language. This context model can provide a lot of options to customers so we can narrow down into the problem.

A Sample question with multiple follow up steps.

The diagram above shows a question “My VPN is not working”. The middle column shows a sample question in human language that can be used to trigger this question. The right most column shows the answer that a customer will be shown for this question. The left most column, 10 additional follow up options that will be shown to customers, in addition to the answer.

Each follow up option is a question on its own, and can have further follow up options. The diagram below shows some follow up questions.

Each follow-up is an independent question.

LUIS bot

LUIS is another service that is provided by Microsoft Azure. LUIS is also known as Language Understanding service. The main intent of this service is to understand Natural Language (both voice and text) and convert it into an intent or Entities. I will just explain these 2 concepts quickly.

Intents :- Intent is usually an object that maps to the action. When a customer lands on the bot, we need to understand what is the intent of this interaction. E.g., “Can you please switch on the light” or “Please remove darkness from this room” both can be mapped to an intent “SwitchLight”.

Entities :- Entities is exactly what you think it is. A predefined structure with some fields. LUIS helps us to extract entities from Natural language statement. Example :- Can you please book a meeting for tomorrow at 12:00 noon with John Papa. In this statement, we not only see customer’s intent but we also pick up values that are predefined for this entity.

The main application of LUIS is to understand natural language out of more than 50 languages (yes Hindi and Punjabi are supported), and then provide Intent + Entities to our program.

Dispatch Tool

Dispatch is a tool to create and evaluate LUIS models used to dispatch intent across multiple bot modules such as LUIS models, QnA knowledge bases and others (added to dispatch as a file type).

Use the Dispatch model in cases when:

  1. Your bot consists of multiple modules and you need assistance in routing user’s utterances to these modules and evaluate the bot integration.
  2. Evaluate quality of intents classification of a single LUIS model.
  3. Create a text classification model from text files.

Summary

In this blog, we discussed what is a chat bot and what are the basic concepts of a chat bot. We also discussed some AI based tools that are available to add the much required AI functionality to our bot. We also learnt that how simple it is to enable different languages in our Bot.

So go ahead and Enhance your working style using the above information.

Leave a Reply