In the previous article, we went through the process of creating and training the QnA Maker Service or, better said, a QnA Maker Knowledge Base. We also explored the ways to query the service REST APIs by using a simple PowerShell script, which can be very easily translated in a language of your choice. We are continuing now our journey to the integration of the created knowledge base into a Microsoft Teams channel. After all, we want the information to be easily accessible at any time, from any device. To do this, we will need to create a bot. This is what this article is all about.
Creating a Question and Answer Bot
Question and Answer Bot is nothing more or less than a predefined Web App Bot template which is available in your Azure portal. To create it, log in to the Azure portal and click Create a resource. In Azure Marketplace categories, click AI + Machine Learning and select Web App Bot. Alternatively, you can search for the Web App Bot term by using the Search the Marketplace search box.
Once you selected it, the New Web App Bot blade will appear. Give it a name, select the subscription and a resource group you want it to be deployed to and choose a location. I recommend the location to be the same as the location of the App Service your QnA Maker Service is running in. As the users will interact with the bot and, in the background, the bot with the REST APIs of the QnA Maker Service, you want to try to reduce the latency as much as possible. This is achieved by placing all the components geographically as near to each other as possible. Next, select your pricing tier, App Service plan and location and storage account.
Under Bot template, make sure that you select the Question and Answer template. You could create the bot with Basic template as well; however, the Question and Answer template contains the code and configuration options in place already, so that you just have to supply the endpoint, host name and the key, without the need to change anything in the code.
Next, you’ll have to set the Microsoft App ID and password. You can take one of two routes here – create it yourself in the Azure Active Directory or let the Azure provision them for your bot. For the sake of simplicity of the example, we will let Azure take care of it.
Click Create and wait for a couple of minutes for the deployment to finish. Once completed, go to the newly created resource.
Connecting Question and Answer Bot to a QnA Maker Service
As mentioned earlier, the selected template already provides the code which takes care of talking to the QnA Maker Service. If you are curious to see how it works, open the Web App Bot resource you just created and under Bot Management group, select Build and click Open online code editor.
Once the editor is opened, under WWWROOT\Dialogs select BasicQnAMakerDialog.cs. In the lines 28-30, we can see that the bot queries the App Service configuration for the information for connecting to the correct knowledge base.
I am not going to get into the code behind, at least not in this series. The reason behind showing this was just to point out that the required code is delivered with the template and to show you where to find the code, in case you decide to modify the functionality from whatever reason. For the latter, there are lot of development options, including source control integration and continuous integration / continuous deployment. It is an Azure App Service behind the scenes after all.
As everything is in place from the code perspective, we just have to provide the information about the knowledge base. This information can be configured in the Web App Bot resource, under Application Settings which can be found in the App Service Settings section of the configuration.
The following information is required:
- QnAKnowledgeBaseId – The ID of the knowledge base in a form of a GUID
- QnAEndpointHostName – QnA Maker Service host name
- QnAAuthKey – QnA Maker Service Authentication key
To obtain the required information, log in to the QnA Maker portal, click My knowledge bases and click View Code for the knowledge base you want to connect with your QnA bot.
You’ll be presented with the sample HTTP request, which contains the information.
Use the information to populate the values in the bot Application Settings and click Save to save the new configuration settings.
Test the bot
During the initial bot deployment, a Web Chat channel is automatically provisioned. Among other things, this gives us an option to validate the functionality of the bot. To test the bot, in the Bot Management group, click Test in Web Chat. The chat area will open within the blade and you can start asking questions. Assuming everything is configured properly, you will get the answers from the knowledge base.
With this, your bot is created, connected to the QnA Maker knowledge base and fully functional. In the next article, we will focus on the options for deploying the newly created bot to the Microsoft Teams.