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

Chatbot with Multiple QnA Maker Knowledge Base and Intents in LUIS - sdk v4 C#


I am developing a bot which currently has one KB mapped to one Intent in LUIS through dispatch tool. I followed the this article to create the dispatch.

I want to create more KB's in the same QnA service and map them to multiple intents through creating a dispatch file. The Microsoft article that I followed uses Case switch statements to determine between multiple intents. This seems very code dependent to me.

The questions is: If I create multiple intents and KB's through dispatch file is there a way I can get rid of these case switch statements? Or I have to create multiple case switch statements for every Knowledge Base so the bot maps to the correct KB?

I have the following code:

 private async Task DispatchToTopIntentAsync(ITurnContext<IMessageActivity> 
 turnContext, string intent, RecognizerResult recognizerResult, 
 CancellationToken cancellationToken)
    {
        switch (intent)
        {
            case "l_HomeAutomation":
                await ProcessHomeAutomationAsync(turnContext, 
        recognizerResult.Properties["luisResult"] as LuisResult, 
     cancellationToken);
                break;
            case "l_Weather":
                await ProcessWeatherAsync(turnContext, recognizerResult.Properties["luisResult"] as LuisResult, cancellationToken);
                break;
            //case "q_sample-qna":
            case "q_CivicSampleFAQ":
                await ProcessSampleQnAAsync(turnContext, cancellationToken);
                break;
            default:
                _logger.LogInformation($"Dispatch unrecognized intent: {intent}.");
              // await turnContext.SendActivityAsync(MessageFactory.Text($"Dispatch unrecognized intent: {intent}."), cancellationToken);
                break;
        }
    }

Basically the case q_CivicSampleFAQ determines that the intent processed should get the reply from QnA Maker KB. If i have multipe KB's I can create multiple cases but this would restrict me to code change every time I have a new KB or even if the name of an old KB is changed. ANy ideas how to bypass this?


Solution

  • Microsoft offers a QnA Maker package which utilizes the Cognitive Services API, from there you can download and store a list of all KBs, their IDs, and their data.

    https://www.nuget.org/packages/Microsoft.Bot.Builder.AI.QnA/

    https://westus.dev.cognitive.microsoft.com/docs/services/5a93fcf85b4ccd136866eb37/operations/knowledgebases_getknowledgebasesforuser

    Beyond this, you'll have to create your own map of relation between the intents and the KBs you want to target. Once you have that done though, it'd be a simple matter of receiving an intent and dispatching to the appropriate KB based on your mapping.