left-icon

Skype Bots Succinctly®
by Ed Freitas

Previous
Chapter

of
A
A
A

CHAPTER 3

QnA Bots

QnA Bots


Quick intro

So far, we’ve explored how to create a basic Skype bot based on regular expressions, and then added natural language processing capabilities to it using LUIS—which really gave some nice superpowers to our bot and simplified the process of routing user requests.

But what if we want to build a bot based on a series of questions and answers? The QnA Maker Service from Microsoft—which, like LUIS, is part of the Cognitive Services suite, allows us to quickly build, train, and publish question-and-answer bots based on FAQ URLs or structured lists of questions and answers.

Just like LUIS, the QnA Maker Service exposes endpoints that can be consumed by a bot application built with the Bot Framework, such as our Skype bot.

Even with LUIS, making a fully fluid conversational bot is not easy. Creating a conversationally fluid bot would require entering a lot of intents, entities, and utterances, which would have to be followed up in the code by dialogs and specific FormFlow logic. This gradually builds up into complexity.

Although bot applications could be developed for any particular subject of interest—such as checking for availability of airline tickets, or an assistant for an online shop—many people tend to think of bots as automated answering agents. In essence, you ask a question and expect an answer.

The QnA Maker Service takes this process to the next level as it abstracts an extra layer of complexity from us by building on top of LUIS. This allows us to create bots based on the notion of taking a knowledge base and converting it into pairs of questions and answers from FAQ URLs or documents.

By being able to auto-extract question and answer (QnA) pairs and using LUIS to infer various utterances from those pairs, the QnA Maker Service makes it easy to create bots that act as answering agents.

This sounds very exciting—and as you’ll see shortly, it’s easier than you think.

Creating the knowledge base

The first step to creating a QnA bot is to create a knowledge base, which is nothing more than a list of questions and answers.

Creating a knowledge base is as simple as pointing the tool to an existing source and ingesting the QnA content.

QnA Maker is able to auto-extract QnA pairs from most FAQ URLs and documents. If it is not able to auto-extract, it has an option to manually add and edit QnA pairs. So, let’s give it a go.

Using your Microsoft account, go to the QnA Maker site and click Sign In.

QnA Maker Website

Figure 3-a: QnA Maker Website

Once you’ve provided your credentials, you might be asked to confirm if QnA Maker can access some of your Microsoft account profile details. When prompted, click Yes in order to continue.

Given that QnA Maker is provided under Cognitive Services Terms, the free preview provides at the time of writing up to 10 transactions per minute (10K transactions per month), so you might be asked to agree to the these service terms in order to continue.

Next, click the Create new service option. We’re going to create a bot that will answer questions related to Creative Commons; however, you may choose to use a totally different FAQ site or upload your own content.

When prompted, enter a name in the SERVICE NAME field and a URL in the FAQ URL(S) field.

A New QnA Service

Figure 3-b: A New QnA Service

There’s also an option to manually upload a document—if no URLs are provided—that must contain QnA pairs. At the time of writing, QnA can consume .tsv, .txt, .docx, and .pdf files.

What the QnA Maker Service will do is to crawl the content—either the provided URLs or document—creating a knowledge base that will serve as the brain for our bot. The information crawled can be edited in the following step.

In order to create the knowledge base, click Create. The QnA Maker crawler will index the information provided and almost immediately provide a list of the questions and answers it was able to identify, which should look similar to the following figure.

The QnA Service Results

Figure 3-c: The QnA Service Results

If you scroll down the results, you’ll see all the QnA pairs that the QnA Maker Service was able crawl, identify, and successfully parse in a relatively fast amount of time. It’s quite impressive.

Notice though how, in this example, the answers extracted contain some markup. Before we click Publish, it is possible to go through each of the answers and manually edit and remove these markup entries. However, that could be a long, tedious, and time-consuming process—especially if there are a lot of answers, as in this case.

So, what we’ll do is leave the content as it is and remove the markup in case we need to via code. Most conversational platforms are able to automatically parse markup, so that shouldn’t be an issue.

If there’s anything else that needs to be manually added, you can do it at this stage. You need to click on the Save and retrain button before publishing. Once you’ve clicked Publish, you’ll be presented with the following information.

The QnA Service Results Recap

Figure 3-d: The QnA Service Results Recap

In order to finalize the publication of these QnA pairs, click Publish. You’ll be presented with a screen similar to Figure 3-e.

The QnA Service Results Deployed

Figure 3-e: The QnA Service Results Deployed

We now have a QnA Service that we can use to create a Creative Commons bot.

We can also find this service under the My services option, as shown in the following figure.

List of QnA Services

Figure 3-f: List of QnA Services

QnA Maker Dialog

Now that we have a knowledge base ready that we can use to create our bot, we need to shift our attention to writing some code.

The QnA Maker Dialog is a library available on NuGet that acts as a wrapper around a QnA Maker Service endpoint. Instead of directly communicating with our QnA service through the published HTTP endpoint, it’s much easier to use the QnA Maker Dialog library.

Open Visual Studio and create a new bot application, using the previously installed Bot Template. Let’s name this new bot application CCBot (using the same steps described in Figure 1-a).

Once the project has been created, go to Solution Explorer and right-click References, and then select Manage NuGet Packages.

Once the NuGet Package Manager window is open, click Restore to restore all NuGet packages that are missing from the Visual Studio project.

The Restore Button in NuGet

Figure 3-g: The Restore Button in NuGet

Once all the missing packages have been restored, type QnA in the search box—you might need to open the NuGet Package Manager window again, as it is possible it might have closed itself when the missing packages were restored. Select QnAMakerDialog by Gary Pretty.

The QnAMakerDialog Package

Figure 3-h: The QnAMakerDialog Package

Once the package has been installed, build the project and add any missing references. For this example, we’ll be writing our code on the RootDialog.cs file.

So, let’s open up RootDialog.cs in order to make the necessary adjustments to use this library. Remove the code that comes out of the box and replace it with the following code.

Code Listing 3-a: The New RootDialog Class

using System;

using System.Threading.Tasks;

using Microsoft.Bot.Builder.Dialogs;

using QnAMakerDialog;

namespace CCBot.Dialogs

{

    [Serializable]

    [QnAMakerService("SUBSCRIPTION_KEY", "KNOWLEDGE_BASE_ID")]

    public class RootDialog : QnAMakerDialog<object>

    {

        public override async Task NoMatchHandler(

            IDialogContext context, string originalQueryText)

        {

            await context.PostAsync(

            $"Couldn't find an answer for '{originalQueryText}'.");

            context.Wait(MessageReceived);

        }

        [QnAMakerResponseHandler(50)]

        public async Task LowScoreHandler(IDialogContext context, string

            originalQueryText, QnAMakerResult result)

        {

            await context.PostAsync(

            $"Found an answer that could help...{result.Answer}.");

            context.Wait(MessageReceived);

        }

    }

}

Let’s try to understand what is going on here. This new RootDialog class now inherits from QnAMakerDialog<object>.

The QnA Maker Dialog library allows us to take incoming text messages from the bot, send them to the published QnA Maker Service, and send the answer sent back from the service to the bot as a reply. It basically acts as a wrapper around the QnA service endpoint and a bridge between the bot and the knowledge base.

When no matching answer in the knowledge base can be found, the NoMatchHandler method is executed. This can be overridden (as we’ve done in Code Listing 3-a) in order to send our own customized message.

Although the default implementation is already good enough for most developers, it is also possible to provide a slightly stronger way of doing things by defining a custom handler and decorating it with a QnAMakerResponseHandler attribute, indicating the maximum score to which the handler should respond.

Code Listing 3-a contains a custom handler that is executed when the confidence score is below 50—this is what the LowScoreHandler method does. We can add as many custom handlers as needed. Any scores above 50 will be handled the default way, and the appropriate response determined by the QnA service will be returned to the user.

That’s all there really is to this. We just need to do one thing—which is to add the subscription key and knowledge base identifier.

We can find both by clicking on the View Code link, as seen in Figure 3-f. Once you click View Code, you’ll see something like this.

The QnA HTTP Request Example

Figure 3-i: The QnA HTTP Request Example

The first box highlighted corresponds to the knowledge base identifier, and the second one to the subscription key. Copy them over to the previous code listing and replace each respective placeholder.

Let’s build our Visual Studio project, run it, and then open the emulator in order to test it out. The second question on the Creative Commons FAQ page is about copyright, as can be seen in the following figure.

The Creative Commons FAQ Page

Figure 3-j: The Creative Commons FAQ Page

Let’s type in a question that uses the word “copyright” (or the word itself) to see what result the QnA Maker Dialog returns as a response through the bot.

Testing the Bot Using the QnA Service

Figure 3-k: Testing the Bot Using the QnA Service

As we can see, the QnA Service was able to return the answer that actually corresponds to the question highlighted in Figure 3-j.

The Response for the Question Tested with the Bot

Figure 3-l: The Response for the Question Tested with the Bot

How cool is that? With barely any code at all, we managed to create a bot that takes data from the Creative Commons FAQ and create a knowledge base that can be queried through a conversation using a bot.

Say we want to know what Creative Common does with its money. Let’s try out this example to see if we get the correct result.

Another Bot Test using the QnA Service

Figure 3-m: Another Bot Test using the QnA Service

Let’s now look at the FAQ site and see if the bot’s response matches the answer on the website.

Figure 3-n: The Response for the Question Tested with the Bot

As we can see, the QnA Service has been able to return the correct answer, as it was able to infer that the question Where does the money go? is actually the same thing as What is the money used for and where does it go?—which itself is part of a bigger question.

Summary

We can start to appreciate that the QnA Service literally took LUIS a few steps forward and abstracted even more complexity for us, making it simple and easy to create a conversational agent based on a knowledge base—and with barely any code. In my view, this is quite an impressive feat.

Like with anything, there’s always the possibility to improve things and add extra logic, which can make this bot application even more robust and capable of handling more scenarios. However, this simple example suffices to demonstrate how far this technology has come, and how easy it is to create such a solution.

I have to finish by remarking that indeed Microsoft’s marketing slogan for the QnA Service is probably one of the most accurate ones I’ve ever read: “From FAQ to Bot in minutes.”

In the next chapter, we’ll explore how we can take this example to the next level by using Scorables for global message handling and interrupting dialogs when required. This is a great technique that can allow us to achieve a lot of flexibility when designing a conversational solution on Skype.

The full source code for this chapter can be found here.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.