Search code examples
chatbotazure-qna-maker

QnA Maker: How to count a specific answer in a session?


I have QnA Maker chatbot. I want to do that: If bot gives the DefaultNoAnswer 3 times in a session, I want to show different DefaultNoAnswer. How can I count the DefaultNoAnswers in QnAMakerBaseDialog ?

ex: Client: asdaaasd

Bot: Sorry, Could you phrase your question differently?

Client: dsjhdsgjdsa

Bot:Sorry, Could you phrase your question differently?

Client: aasdjhajds

Bot: Sorry, I couldn't get the question. Send an email for detailed information.


Solution

  • I find the best way to handle this is with a conversation state variable. I have my default message set up in my helper (i.e. I have a helper file that makes the call to QnA Maker, checks the confidence, and sends a default message in case of low confidence or no answer). If you are using a similar case, you can increment your state variable there. If you are using QnA Maker's default answer directly, you still need to do some check on every result before sending the response to user. I haven't used that method, but I would probably just check the result for the default answer and increment the variable accordingly.

    Here is a sample for the first case. I am assuming here that you are already familiar with managing user and conversation state.

                    var qnaResult = await QnAServiceHelper.queryQnaService(query, oldState);
                    if (qnaResult[0].score > MINIMUM_SCORE) {
                        const conversationData = await this.dialogState.get(step.context, {});
                        conversationData.defaultAnswerCounter = 0;
                        await this.conversationState.saveChanges(step.context); 
                        var outputActivity = MessageFactory.text(qnaResult[0].answer);
                    } else {
                        const conversationData = await this.dialogState.get(step.context, {});
                        conversationData.defaultAnswerCounter += 1;
                        if (conversationData.defaultAnswerCounter <= 2) {                    
                           var outputActivity = defaultAnswer;
                        } else {
                           var outputActivity = escalationAnswer;
                        }
                        await this.conversationState.saveChanges(step.context);
                    }