Search code examples
c#botframeworkazure-language-understandingazure-qna-maker

Integrating bot framework Luis with QnA as an Intent, then re-ask user after getting to the QnA


I am trying to make a FAQ intent that act as a QnA dialog, which should re-ask the user after getting into the intent.

Below is my code so in integrating the luis and QnA:

 [LuisIntent("FAQ")]
    public async Task FAQ(IDialogContext context, LuisResult result)
    {
        await context.PostAsync("FAQ");
        await context.Forward(new QnADialog(), ResumeAfterQnA, context.Activity, CancellationToken.None);

    }
    private async Task ResumeAfterQnA(IDialogContext context, IAwaitable<object> result)
    {
        await context.PostAsync("Back to Intent");
        context.Wait(MessageReceived);

    }

While in the QnA Dialog:

[Serializable]
[QnAMakerService("endpoint", "knowledge base id", "subscription key")]
public class QnADialog : QnAMakerDialog<object>
{
    public bool flag = false;

    public override async Task DefaultMatchHandler(IDialogContext context, string originalQueryText, QnAMakerResult result)
    {


        if (result.Answers.Length > 0 && result.Answers.FirstOrDefault().Score > 0.75 && flag)
        {
            await context.PostAsync(result.Answers.FirstOrDefault().Answer);
            await context.PostAsync("To continue using the FAQ please type another question, if not type no");
        }
        else if (originalQueryText.Contains("no"))
        {
            context.Done(true);
        }
        else
        {
            await base.DefaultMatchHandler(context, originalQueryText,result);
            flag = true;
        }
    }

}

The test result is the following: enter image description here i would like for the "No Good match found in KB" to not show after the welcome to the FAQ but struggling to do so, i already look at the documentation samples but there's isn't any similar samples with my problem.

Any help will be appreciated


Solution

  • i would like for the "No Good match found in KB" to not show after the welcome to the FAQ

    Based on your code and requirement, I modified the code in DefaultMatchHandler method, which work for me, you can refer to it.

    public override async Task DefaultMatchHandler(IDialogContext context, string originalQueryText, QnAMakerResult result)
    {
        if (result.Answers.Length > 0 && result.Answers.FirstOrDefault().Score > 0.75 && flag)
        {
            await context.PostAsync(result.Answers.FirstOrDefault().Answer);
            await context.PostAsync("To continue using the FAQ please type another question, if not type no");
        }
        else if (originalQueryText.Contains("no"))
        {
            context.Done(true);
        }
        else
        {
            //detect if originalQueryText contains "faq"
            if (!originalQueryText.ToLower().Contains("faq"))
            {
                await base.DefaultMatchHandler(context, originalQueryText, result);
            }
            flag = true;
        }
    }
    

    Test result:

    enter image description here