Using QnA Maker – Bot Service

[ad_1]


To add simple question and answer support to your bot, you can use the QnA Maker service.

One of the basic requirements in writing your own QnA Maker service is to seed it with questions and answers. In many cases, the questions and answers already exist in content like FAQs or other documentation. Other times you would like to customize your answers to questions in a more natural, conversational way.

Create a QnA Maker service

First create an account and sign in at QnA Maker. Then navigate to Create a knowledge base. Click Create a QnA service and follow the instructions for creating an Azure QnA service.

Image 1 for qna

After you connect your Azure QnA service, then you can add new QnA pairs. Click Create your KB. Enter your own question and answers or copy the following examples.

Image 2 for qna

Alternatively, you can choose Populate your KB and provide a file or URL. A sample source file for generating a simple QnA Maker service is here.

After adding new QnA pairs or populating your KB, click Save and train. Once you are completed, in the PUBLISH tab, click Publish.

To connect your QnA service to your bot, you will need the HTTP request string containing the knowledge base ID and QnA Maker subscription key. Copy the example HTTP request from the publishing result.

Image 3 for qna

Installing Packages

Before we get coding, make sure you have the packages necessary for QnA Maker.

Add a reference to v4 prerelease version of the following NuGet packages:

  • Microsoft.Bot.Builder.Ai.QnA

Using QnA Maker

QnA Maker is first added as middleware. Then we can use the results within our bot logic.

Update the ConfigureServices method in your Startup.cs file to add a QnAMakerMiddleware object. You can configure your bot to check your knowledge base for every message received from a user, by simply adding it to your bot’s middleware stack.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Bot.Builder.Ai.Qna;
using Microsoft.Bot.Builder.BotFramework;
using Microsoft.Bot.Builder.Core.Extensions;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;

public void ConfigureServices(IServiceCollection services)

    services.AddSingleton(_ => Configuration);
    services.AddBot<AiBot>(options =>
    
        options.CredentialProvider = new ConfigurationCredentialProvider(Configuration);

        var endpoint = new QnAMakerEndpoint
            
                knowledgebaseId = "YOUR-KB-ID",
                // Get the Host from the HTTP request example at https://www.qnamaker.ai
                // For GA services: https://<Service-Name>.azurewebsites.net/qnamaker
                // For Preview services: https://westus.api.cognitive.microsoft.com/qnamaker/v2.0
                Host = "YOUR-HTTP-REQUEST-HOST",
                EndpointKey = "YOUR-QNA-MAKER-SUBSCRIPTION-KEY"
            ;
        options.Middleware.Add(new QnAMakerMiddleware(new QnAMakerEndpoint(endpoint)));
    );

Edit code in the EchoBot.cs file, so that OnTurn sends a fallback message in the case that the QnA Maker middleware didn’t send a response to the user’s question:

using System.Threading.Tasks;
using Microsoft.Bot;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;

namespace Bot_Builder_Echo_Bot_QnA
{
    public class EchoBot : IBot
        
        public async Task OnTurn(ITurnContext context)
        
            // This bot is only handling Messages
            if (context.Activity.Type == ActivityTypes.Message)
                         
                if (!context.Responded)
                
                    // QnA didn't send the user an answer
                    await context.SendActivity("Sorry, I couldn't find a good match in the KB.");

                
            
        
    
}

See the QnA Maker sample for a sample bot.

Ask your bot questions to see the replies from your QnA Maker service.

Image 4 for qna

Next steps

QnA Maker can be combined with other Cognitive Services, to make your bot even more powerful. The Dispatch tool provides a way to combine QnA with Language Understanding (LUIS) in your bot.




[ad_2]

source_link
https://www.asp.net