如何基于 LUIS 问题创建响应? (天蓝色)

How to create response based on LUIS question? (Azure)

我对使用 Azure 的 LUIS 构建聊天机器人开发还比较陌生。我坚持使用已部署的 LUIS 和 QnA Maker 测试我的机器人。我基本上是在 LUIS 上提出问题,但我如何根据 LUIS 提出的问题获得答复?谢谢!

要调用 QNA maker,您可以查看以下代码:

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; }
    }

有关更多详细信息,您可以 refer this official document

希望对您有所帮助。

如果您有一个调用 QnAMaker 的聊天机器人,则使用 QnAMaker class 会稍微简单一些。以下是在 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);
    }
}

关于如何将 QnA 集成到机器人中有一个很好的示例 here, along with the official documentation here