1
votes

I have managed to create a KB and web app bot - which I believe is generated with git.

Before sending the default no result found message ("No QnAMaker answers found.") response, I would like to call a REST API.

protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
    {
        // get answer to user's question
        // if no answer found, call rest api
        await Dialog.RunAsync(turnContext, ConversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);
    }

In RootDialog.cs, it invokes

private async Task<DialogTurnResult> InitialStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        return await stepContext.BeginDialogAsync(nameof(QnAMakerDialog), null, cancellationToken);
    }

Once the stepContext.BeginDialogAsync is completed, it searches the QnA Maker for answer and send it to the user. Is there a way to get the answer and the score in this method?

I am not sure where should I implement it. Hope someone can guide me.

Thanks in advance.

1

1 Answers

0
votes

I'm working on nodejs but I think you can adapt this to .NET. You should be able to take any actions you want once you get your results back. I have an implementation like this. In my case I'm calling a second QnA Maker endpoint (I have one for business data and once for chit chat), but you could substitute that call with whatever you want.

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

const { MessageFactory } = require('botbuilder');
const { QnAServiceHelper } = require('../helpers/qnAServiceHelper');
const { CardHelper } = require('../helpers/cardHelper');

class QnADialog {

    async processAsync(oldState, activity){

        var menuText = `I'm sorry, I don't know how to help with that. Right now I am trained to help you with order management and product availability from Vista`;
        if (activity.channelId == 'msteams') {
            var defaultAnswer = CardHelper.GetMenuCardTeams(menuText);
        } else {
            var defaultAnswer = CardHelper.GetMenuCard(menuText);
        }
        var MINIMUM_SCORE = 50;
        
        var newState = null;
        var query = activity.text;
        var qnaResult = await QnAServiceHelper.queryQnAService(query, oldState);
        var qnaAnswer = qnaResult[0].answer;
        
        var prompts = null;
        if(qnaResult[0].context != null){
            prompts = qnaResult[0].context.prompts;
        }
        
        var outputActivity = null;
        if (prompts == null || prompts.length < 1) {

            // Apply confidence filter
            if (qnaResult[0].score > MINIMUM_SCORE) {
                outputActivity = MessageFactory.text(qnaAnswer);
            } else {
                // If low confidence, send to social talk
                var socialResult = await QnAServiceHelper.querySocialTalkService(query, oldState);
                if (socialResult[0].score > MINIMUM_SCORE) {
                    outputActivity = MessageFactory.text(socialResult[0].answer);
                } else {
                    // If low confidence for social talk, use default answer
                    //outputActivity = MessageFactory.text(defaultAnswer);
                    outputActivity = defaultAnswer;
                }
            }
        }
        else {
            var newState = {
                PreviousQnaId: qnaResult[0].id,
                PreviousUserQuery: query
            }

            outputActivity = CardHelper.GetHeroCard('', qnaAnswer, prompts);
        }
        
        return [newState, outputActivity , null];
    }  
}

module.exports.QnADialog = QnADialog;