1
votes

I am relatively new to building with Azure's LUIS for chatbot development. I am stuck on testing my bot with a deployed LUIS and QnA Maker. I essentially developed questions on LUIS but how do I get the response based on the questions directed from LUIS? Thanks!

2
You can directly do it on QNA maker. Once you created your KB then from your source like https://YourSource.azurewebsites.net/qnamaker/knowledgebases/YourSubscription/generateAnswer. Let me know if you need some more details. You can also filter your user intent from LUIS.Md Farid Uddin Kiron
Thanks @MdFaridUddinKiron for the quick response, I'm not aware where I need to place that URL, but I will play around some more and let you know.Patty
I have shown you how would you implement QNA maker in details. Let me know if you feel any challenge to implement it. Thank you.Md Farid Uddin Kiron

2 Answers

0
votes

If you have a chatbot that is calling QnAMaker, a slightly easier method would be to use the QnAMaker class. Here's how you would do this in C#:

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["QnAEndpointKey"],
        Host = _configuration["QnAEndpointHostName"]
    },
    null,
    httpClient);

    _logger.LogInformation("Calling QnA Maker");

    var options = new QnAMakerOptions { Top = 1 };

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

There is a good example on how to integrate QnA into a bot here, along with the official documentation here.

0
votes

To Invoke QNA maker you could have a look on following code:

Invoke QNA Maker API:

//Take User Input And Validate param

            if (string.IsNullOrEmpty(objQnAMakerQuestion.question))
            {
                validationMessage = new OkObjectResult("Question is required!");
                return (IActionResult)validationMessage;
            }

            // Call QnA API             
            var jsonContent = JsonConvert.SerializeObject(objQnAMakerQuestion);
            var endpointKey = "YourSubscriptionKey";
            var qnaMakerURI = "https://YourSource.azurewebsites.net/qnamaker/knowledgebases/YourSubscription/generateAnswer";
            using (var client = new HttpClient())
            using (var request = new HttpRequestMessage())
            {
                request.Method = HttpMethod.Post;
                request.RequestUri = new Uri(qnaMakerURI);
                request.Content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
                request.Headers.Add("Authorization", "EndpointKey" + endpointKey);

                var response = await client.SendAsync(request);


                //Check status code and retrive response

                if (response.IsSuccessStatusCode)
                {

                    QnAMakerModelClass objQnAResponse = JsonConvert.DeserializeObject<QnAMakerModelClass>(await response.Content.ReadAsStringAsync());
                  //  var responseBody = await response.Content.ReadAsStringAsync();

                    foreach (var item in objQnAResponse.answers)
                    {
                        QnAMakerAnswer objAnswer = new QnAMakerAnswer();

                             objAnswer.answer = item.answer;
                             return new OkObjectResult(objAnswer);

                    }


                }
                else
                {
                    var result_string = await response.Content.ReadAsStringAsync();
                    return new OkObjectResult(result_string);
                }

Class Used To Invoke QNA Maker:

public class QnAMakerQuestion
    {
        public string question { get; set; }

    }

    public class QnAMakerAnswer
    {
        public string answer { get; set; }

    }
    public class Metadata
    {
        public string name { get; set; }
        public string value { get; set; }
    }

    public class Context
    {
        public bool isContextOnly { get; set; }
        public List<object> prompts { get; set; }
    }

    public class Answer
    {
        public List<string> questions { get; set; }
        public string answer { get; set; }
        public double score { get; set; }
        public int id { get; set; }
        public string source { get; set; }
        public List<Metadata> metadata { get; set; }
        public Context context { get; set; }
    }

    public class QnAMakerModelClass

    {
        public List<Answer> answers { get; set; }
        public object debugInfo { get; set; }
    }

For more details you could refer this official document

Hope it will help.