I’ll be speaking at Visual Studio Live!, San Diego, October 7-11 
 I’ll be presenting the following sessions:
 W11 - Building Business Applications Using Bots
 W19 - Using The Microsoft Cognitive Custom Vision Service
 As a speaker, I can offer $400 savings on the 4-day package.
 Register here: http://bit.ly/SDSPK27Home 

Using Azure QnA Maker (MS Bot Framework V4 Preview Edition)

Jul 15

Written by:
7/15/2018 6:18 PM  RssIcon

image

The Microsoft Bot Framework V4 allows you to create intelligent bots that interact naturally wherever your users are (text/SMS to Skype, Slack, Office 365 mail and other popular services). In preview now, the Bot Builder V4 Preview SDK offers new features and is extensible with a pluggable middleware model.

In this article we will build on the example started in the following article: Using LUIS -Language Understanding Intelligent Service (MS Bot Framework V4 Preview Edition). In this article we will cover integrating the QnA Maker service into your Bot. Using the QnA Maker in your Bot allows you to create chat bots that are able to understand and respond to queries by simply looking at a list of questions and answers that you provide.

Tutorial Series

Other articles in this series:

Requirements

 

What Is QnA Maker?

According to the Microsoft QnA Maker website, the QnA Maker allows you to: “Build, train and publish a simple question and answer bot based on FAQ URLs, structured documents, product manuals or editorial content in minutes”. This really speeds your Bot development because it leverages the abilities of LUIS (Language Understanding Intelligent Service) to understand human conversation, and combines it with a service that allows you to provide a list of questions and answers, or simply upload a product manual or provide a link to a website, and the questions and answers will be parsed for you automatically.

The end result is an incredible amount of functionality that you can add to your Bot easily.

 

Create The QnA Maker Service

image

To get started, go to: https://www.qnamaker.ai/, and sign in with your Azure account.

image

Select Create a knowledge base.

image

Select Create a QnA service.

Note: This will open a new web browser window.

image

Fill in the details to create the service and click the Create button.

image

Return to the https://www.qnamaker.ai/ site (the site should still be open on it’s original web browser tab/window).

Scroll down the page to the STEP 2 section.

Click the refresh this page link.

image

The page will query Azure to detect any newly added services.

image

You will now be able to select the service you just created.

image

Scroll down the page to the STEP 3 section.

Give the QnA database a name

image

Scroll down the page to the STEP 4 section.

Add a URL to a web page that contains FAQs (Frequently Asked Questions).

(or upload a file with question and answer pairs).

image

Scroll down the page to the STEP 5 section.

Click the Create your KB button.

image

The knowledgebase will be created.

image

The questions and answers detected on the page will be loaded.

You can update and edit them at this point.

For now, just click the Save and train button.

image

The knowledgebase will be updated.

image

Select PUBLISH.

image

Click the Publish button.

image

The knowledgebase will be published.

image

The connection settings will display.

Save them, you will need them later.

Note: You can always get back to these settings by selecting My knowledge bases from the main menu, then View Code for the service.

 

Update The Bot Code

image

Open the project created in the article: Using LUIS -Language Understanding Intelligent Service (MS Bot Framework V4 Preview Edition) in Visual Studio 2017 (or higher).

Right-click on the project node and select Manage NuGet Packages.

image

Select include prerelease.

Search for Microsoft.Bot.Builder.Ai.QnA.

Click on Microsoft.Bot.Builder.Ai.QnA, then click the Install button.

(accept any other screens that appear and fully install the package)

image

Open the EchoBot.cs file.

Add the following using statement to the top of the file:

 

using Microsoft.Bot.Builder.Ai.QnA;

 

Change the following code:

 

            // Normal process using Dialogs
            if (!context.Responded)
            {
                if (!userstate.AskedForUserName)
                {
                    // A dialog won't be started without calling the .begin command 
                    // passing the name of the dialog to be started
                    await DialogContext.Begin("greetings");
                    // Set AskedForUserName so we don't ask again
                    userstate.AskedForUserName = true;
                }
                else
                {
                    // Echo back to the user whatever they typed.
                    await context.SendActivity(
                        $"Hello {userstate.UserName} you said '{context.Activity.Text}'");
                }
            }

 

To (this adds code to call the QnA Service):

 

            // Normal process using Dialogs
            if (!context.Responded)
            {
                if (!userstate.AskedForUserName)
                {
                    // A dialog won't be started without calling the .begin command 
                    // passing the name of the dialog to be started
                    await DialogContext.Begin("greetings");
                    // Set AskedForUserName so we don't ask again
                    userstate.AskedForUserName = true;
                }
                else
                {
                    // **************************************
                    // Give QnA a chance to respond
                    var qEndpoint = new QnAMakerEndpoint()
                    {
                        Host = "** Your Host **",
                        EndpointKey = "** Your EndpointKey **",
                        KnowledgeBaseId = "** Your KnowledgeBaseId **"
                    };
                    var qOptions = new QnAMakerOptions()
                    {
                        ScoreThreshold = 0.4F,
                        Top = 1
                    };
                    var qnamaker = new QnAMaker(qEndpoint, qOptions);
                    QueryResult[] qResult = await qnamaker.GetAnswers(context.Activity.Text);
                    if (qResult.Length != 0)
                    {
                        await context.SendActivity(qResult[0].Answer);
                    }
                    // **************************************
                    else
                    {
                        // Echo back to the user whatever they typed.
                        await context.SendActivity(
                        $"Hello {userstate.UserName} you said '{context.Activity.Text}'");
                    }
                }
            }

 

 

Note:

  • Enter the Host you saved earlier where it indicates ** Your Host ** (it must be between the quotes)
  • Enter the EndpointKey you saved earlier where it indicates ** Your EndpointKey ** (it must be between the quotes)
  • Enter the KnowledgeBaseId you saved earlier where it indicates ** Your KnowledgeBaseId ** (it must be between the quotes)

Also, if you downloaded the code from the Download page on this site, ensure that you have LUIS keys for the LUIS sections in the code (See: Using LUIS -Language Understanding Intelligent Service (MS Bot Framework V4 Preview Edition)).

 

image

Run the project, return to the Bot Emulator, click the Start Over button, and chat with the updated Bot.

The Bot will behave as before, however, when asked a question that can be answered by the QnA service, it will provide the answer.

image

However, if the QnA service cannot provide an answer, it wont, and the normal code will run.

 

Special Thanks

A special thanks to Vishwac Sena Kannan, for assistance. Without his help this post would not be possible (however, any errors are my own).

 

Links

Azure Bot Service Documentation for the v4 SDK

www.qnamaker.ai

What is QnA Maker?

Create a QnA Bot with Azure Bot Service

QnA Maker Documentation

Integrate QnA Maker and LUIS to distribute your knowledge base

Choosing capacity for your QnA Maker deployment

 

Download

You can download the code from the Download page