Kailash Kumawat
Kailash Kumawat

Reputation: 55

How to add LUIS in a existing QnA bot?

I have a existing QnA bot (C#, SDK-v4) and now I wan to add LUIS to it without creating a new bot with LUIS template.

my QnABot.cs file -

public class QnABot : ActivityHandler
    {
        private readonly IConfiguration _configuration;
        private readonly ILogger<QnABot> _logger;
        private readonly IHttpClientFactory _httpClientFactory;


        public QnABot(IConfiguration configuration, ILogger<QnABot> logger, IHttpClientFactory httpClientFactory)
        {
            _configuration = configuration;
            _logger = logger;
            _httpClientFactory = httpClientFactory;
        }

        protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            var httpClient = _httpClientFactory.CreateClient();

            var qnaMaker = new QnAMaker(new QnAMakerEndpoint
            {
                KnowledgeBaseId = _configuration["QnAKnowledgebaseId"],
                EndpointKey = _configuration["QnAAuthKey"],
                Host = GetHostname()
            },
            null,
            httpClient);

            _logger.LogInformation("Calling QnA Maker");

            // The actual call to the QnA Maker service.
            var response = await qnaMaker.GetAnswersAsync(turnContext);
            if (response != null && response.Length > 0)
            {
                awaitturnContext.SendActivityAsync(
              MessageFactory.Text(response[0].Answer), cancellationToken);
            }
            else
            {
                await turnContext.SendActivityAsync(MessageFactory.Text("No QnA Maker answers were found."), cancellationToken);
            }
        }

        private string GetHostname()
        {
            var hostname = _configuration["QnAEndpointHostName"];
            if (!hostname.StartsWith("https://"))
            {
                hostname = string.Concat("https://", hostname);
            }

            if (!hostname.EndsWith("/qnamaker"))
            {
                hostname = string.Concat(hostname, "/qnamaker");
            }

            return hostname;
        }
    }

I know about dispatch tool which can a dispatch LUIS app with knowledge base but I am do not know how to handle Luis intents in this bot. How can I integrate LUIS in this bot?

Upvotes: 0

Views: 542

Answers (1)

Matt Stannett
Matt Stannett

Reputation: 2728

You can add LUIS to an existing QnA Bot but you'll essentially be copying a lot of code from this sample so it's almost quicker to just start from the sample and copy over any code you want to keep from your existing QnA Bot.

Your OnMessageActivity should go from looking like this where it calls a qnamaker client directly to looking like this where the user's input is passed to a LUIS dispatch app which determines what intent to route the user to.

The routing of the user is handled inside the [DispatchToTopIntent]https://github.com/microsoft/BotBuilder-Samples/blob/master/samples/csharp_dotnetcore/14.nlp-with-dispatch/Bots/DispatchBot.cs#L51) method, the strings in the case statement match to the intent names under your LUIS app in the portal.

Needless to say there will be some additional packages that you will need to include in your bot Microsoft.Bot.Builder.Ai.LUIS is one, and you will need to create the IBotServices interface and BotServices class in your project along with other changes.

The whole process is documented here.

Upvotes: 1

Related Questions