Build your Covid19-Bot with LUIS, QnAmaker & Azure Bot Service

Zayed Rais
Chatbots Life
Published in
12 min readMay 6, 2020

--

Source

The sad reality is that covid19 is spreading quickly and will continue to spread for a while. But there is good thing, the recovery rate is higher than the previous Corona Virus.

In the era of AI world, technology play a crucial role to tackle down the covid19 pandemic.

Around 24 K new cases a day and it’s growing fast

We don’t know how many people will be affected by the virus, some are directly or indirectly affected by the pandemic situation. The economy is going to crash, which means the business will be affected. through business, common people are also affected.

Past one month onward, I’m thinking about to build a bot which will help to give the current status of confirmed cases. even it’ll suggest to you how to take precaution through your queries.

Today post i’ll tell you about, how to build your basic covid19-bot with the help of BotFramework, LUIS(NLP), QnAmaker(QnA), Azure Bot Service & Adaptive cards for interactive responsive to the user.

Prerequisites

Full code of the Covid19Bot project is available on GitHub

Step 1:- Download the .NET Core SDK and install in your system.

After the installed check, the version of dotnet-core, must be used version 3.1

# determine dotnet version
dotnet --version

Step 2: Build a LUIS Service

Language Understanding (LUIS) is a cloud-based API service that helps to identify the user’s conversational, natural language text to predict overall meaning, and pull out relevant, detailed information.

Here we are using a LUIS service in a Chatbot to identify the correct intent, which can give the right responses to the users.

  • In the pop-up dialog, enter the name Covid19Luis and keep the default culture, English. The other fields are optional, do not set them. Select Done.
  • Now go to the Build section, on the Intents page, select + Create to create a new intent. Enter the new intent name, Covid19India, then select Done.
  • The Covid19India intent is predicted when a user wants to know the India status of Covid19.

Add several example utterances to this intent that you expect a user to ask: below Covid19India few example utterances are:

Live status of covid19 India

Today status

Covid19 India

current status of covid19 India

India

  • As same as create few more intents, based on your application requirement. In this bot we need another intent is Covid19Global which predicted when a user wants to know the globally status of Covid19. Here also add utterances for the intent.
  • After adding the utterances of the intents, In the top right side of the LUIS website, select the Train button.
  • Now we can test the Luis app, select the Test Button

After a successful test the app, we have to select the publish button for a production uses.

LUIS AppId & AuthoringKey:-

After successfully published, now we have to get the LUIS appID & authoringKey. further, we’ll use these keys.

Go to Manage tab, on the left side, select the application information column and there you can find the application id.

Go to Manage tab, on the left side, select the Azure Resources column and there you can find the Primary key is the authoringkey.

Step 3: Build a QnAmaker service

QnA Maker is a cloud-based Natural Language Processing (NLP) service that easily creates a natural conversational layer over your data. It can be used to find the most appropriate answer for any given natural language input, from your custom knowledge base (KB) of information.

In our chatbot, we are using the QnAmaker service to response the right answer from the user’s question.

Create your first QnA Maker knowledge base

  1. Sign in to the QnAMaker.ai portal with your Azure credentials.
  2. In the QnA Maker portal, select Create a knowledge base.
  3. On the Create page, skip Step 1 if you already have your QnA Maker resource.
  • If you haven’t created the resource yet, select Create a QnA service. You are directed to the Azure portal to set up a QnA Maker service in your subscription.
  • When you are done creating the resource in the Azure portal, return to the QnA Maker portal, refresh the browser page, and continue to Step 2.

4. In Step 2, select your Active directory, subscription, service (resource), and the language for all knowledge bases created in the service.

5. In Step 3 & Step 4, configure the settings with the below screenshot:-

Here, we are using some basic Covid19 QnA from WHO site. if you have other sources of QnA, please checked the Enable multi-turn extraction column

https://www.who.int/news-room/q-a-detail/q-a-coronaviruses

and Select Professional on Chit-chat column.

6. In Step 5, Select Create your KB.

Save and train

In the upper right, select Save and train to save your edits and train QnA Maker.

Test the KB

In the QnA Maker portal, in the upper right, select Test. and enter your question to the QnAmaker, it’ll response the answer.

Publish the KB in production

In QnAmaker portal, select the Publish tab and click the publish button.

QnA KBid, Endpoint & EndpointKey

After successfully published the KB app.

As the below screenshot, you can find the KBid, Endpoint, Endpointkey

Create a Bot

In the QnA Maker portal, on the Publish page, select Create bot. This button appears only after you’ve published the knowledge base.

When you are clicking the Create Bot button, it’ll redirect into Azure Web App bot service page. Configure the Azure bot service

The bot and QnA Maker can share the web app service plan, but can’t share the web app. This means the app name for the bot must be different from the app name for the QnA Maker service.

Do Change

  • Change bot handle — if it is not unique.
  • Select SDK Language.

Don’t Change

  • Change the following settings in the Azure portal when creating the bot. They are pre-populated for your existing knowledge base:
  • QnA Auth Key
  • App service plan and location

Finally, click the Create button. after some time web bot is ready to use.

Now, select the Test in Web Chat tab, web chat screen will appear for the test. Enter the same query question as QnA app.

Step 4: Build a Bot logic with C#

When you are done to creating the QnAmaker Bot, Now we have to download the code for adding the LUIS feature to bot.

Go to your Bot service and select the download bot source code in Azure portal.

Before going into the code section, I wanna tell you how LUIS and QnAmaker are working in a single bot service. In Botframework is already available Dispatch tool to determine which LUIS model or QnAmaker knowledge base best matches the user input. The dispatch tool does this by creating a single LUIS app to route user input to the correct model. For more information about it, refer to the README.

Now, download code open with visual studio code or your editor

The project directory looks like as shown on the left image.

  • Delete the Dialog Folder from the project, here we are using the dispatch tool instead of dialog concept.

Add the libraries into the projectName.csproj file

  • Change the ‘TargetFramework’ from 2.1 into netcoreapp3.1, dispatch tool supports the dotnet core3.1 framework. and replaced the old ItemGroup with the below dependencies.
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.1" />
<PackageReference Include="Microsoft.Bot.Builder.AI.Luis" Version="4.8.0" />
<PackageReference Include="Microsoft.Bot.Builder.AI.QnA" Version="4.8.0" />
<PackageReference Include="Microsoft.Bot.Builder.Integration.AspNet.Core" Version="4.8.0" />
</ItemGroup>
  • Config dependency file looks like the below image.

when you are done with the above step. Then, open the project with a terminal and run the restore command

$ dotnet restore
  • Now, open the BotServices.cs file and replace with the below code.
  • In BotServices.cs, the information contained within configuration file appsettings.json is used to connect your dispatch bot to the Dispatch and SampleQnA services.
  • Same way do for the IBotServices.cs file
  • Do the same for the Startup.cs file
  • Create a new folder with name of cards on the root of the project
  • Create a new file with name of Covid19Status.json, inside the cards folder, It is used a template for the display the interactive response of the user.
  • We can create own template through the adaptive cards site https://adaptivecards.io/designer.

Create Adaptive cards:-

Adaptive Cards are platform-agnostic snippets of UI, authored in JSON, that apps and services can openly exchange. When delivered to a specific app, the JSON is transformed into native UI that automatically adapts to its surroundings.

Then copy the below code into the designer site.

You can see, how the template looks like

The same way you can create the GlobalStatus.json template file

After successfully done the template file, Add the below config code into the projectName.csproj file

<ItemGroup>
<Content Remove="Cards\Covid19Status.json" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Cards\Covid19Status.json" />
</ItemGroup>
<ItemGroup>
<Content Remove="Cards\GlobalStatus.json" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Cards\GlobalStatus.json" />
</ItemGroup>

Now, we have come to the core part of the code.

Create a new file inside the Bots folder, with the name of DispatchBot.cs

Copy the below code and paste into the DispatchBot.cs file.

Create a new file with the name Repo.cs and the below code on it.

This class file is used to initialize the required properties from the India API, which we are using to get the data in our Bot.

Create a new file with the name RepoWorld.cs and the below code on it.

Top Articles on How Businesses are using Bots:

1. The Future of Law, Lawyers and Law Professors… And the Exponential Growth of Disruptive Technology

2. Why Chatbots Are Key to the Future of Business Intelligence

3. Is Chatbot a synonym for great Customer Experience?

4. 50 of the best chatbot use cases

This class file is used to initialize the required properties from the Global API, which we are using to get the data in our Bot.

Explanation of DispatchBot.cs :-

OnMessageActivityAsync is called for each user input received. This module finds the top scoring user intent and passes that result on to DispatchToTopIntentAsync.

DispatchToTopIntentAsync, in turn, calls the appropriate app handler

  • ProcessSampleQnAAsync- for Covid19 FAQ.
  • ProcessCovid19LuisAsync - for Covid19 status queries.
  • ProcessCovid19LuisAsync the method has two intents, one is used to identify the India status intents and other is used to identify the global status intents.
  • When this module finds the top-scoring Covid19India intent and passes that result on to call the India API to get the current data. This data updated to the cards template (Covid19Status.json) and rendered the updated cards to the user responses.
  • As same process be done for the WorldCovid19intent.

Build a Dispatch Model:-

  • Create a new folder on the root of project with the name of CognitiveModels
  • Go to the CognitiveModels directory of your project. and run the below command. For more detail please check the dispatch tool
$ npm i -g npm
$ npm i -g botdispatch

Initializing dispatch

To initialize dispatch, run the below command. To replace the LuisAPIKey with your ApiKey. We have already built the LUIS app. In step 2, the primary key is LuisAPIKey.

$ dispatch init -n covid19Dispatch --luisAuthoringKey "LuisAPIKey" --luisAuthoringRegion westus

Adding source to dispatch

  • Here, we are using two sources, one is LUIS and another is QnAmaker service.
  • LuisAppId and LuisApiKey can be found in Step 2.
$ dispatch add -t luis -i "LuisAppID" -n "Covid19" -v 0.1 -k "LuisAPIKey" --intentName l_covid19

Add the source of QnAmaker into the Dispatch model

$ dispatch add -t qna -i "QnAKnowledgebaseId" -n "Covid19Who" -k "AzureCognitiveServiceKey" --intentName q_covid19
  • Check in step 3. QnA KbId is the QnAKnowledgebaseId.
  • The Key1 or Key2 is the AzureCognitiveServiceKey from the Azure portal of your Cognitive QnAmaker service.

Creating your dispatch model

To create, train and publish your new dispatch model:

$ dispatch create

After successfully, built the Dispatch model. you can find the .dispatch file inside the CognitiveModels folder.

Now, add each of the entities into appsetting.json of your project, add the values you recorded earlier in these instructions:

Step 5: Run & Test the Chatbot

Now, go the project root directory and run the project.

$ dotnet run
  • When the project is successfully run, and the browser will render the URL like as http://localhost:3978/
  • Open the Bot Framework Emulator App for testing your application. And open a bot with the URL “http://localhost:3978/api/messages/” and the Microsoft APP ID & Microsoft App Password.
  • Go to the App service of your Bot and select the configuration tab. There are available Microsoft App ID & Microsoft App Password in the application setting section.

The working Demo:

Step 6: Deploy the Bot on the Azure Web App Bot Service:-

$ dotnet publish --configuration Release
  • When you are run the above code, go to bin →Release →netcoreapp3.1 →Publish, copy all the files from the folder and paste into new creation folder of the desktop.
  • Now copy the cards folder from your project and paste the same folder, where the publish file be there.
  • Make a zip file for the publish folder (folder look like as the below image)
$ az login
$ az webapp deployment source config-zip --resource-group "your Resource group name" --name "covid19bots" --src "Path/publish.zip"

You can find the ‘Deployment endpoint responded with status 202’ message in your terminal. It’s mean your zip file is accepted by the deployment agent. After sometime you can find the message with the URL.

Step 7: Bot connect with Channel:-

Now, you can connect your bot with your desire channel.

Closing:

In a single post, Now you knew, how to create your covid19bot with the use of LUIS, QnAmaker and also learnt the Bot deployment. The same way, you can be applied to build the other Chatbot.

Covid19Bot GitHub Repository

Don’t forget to give us your 👏 !

--

--

Learn & share the Insight of new techie skills #ML #AI #Cloud #DataScience in real world.