我对使用 Azure 的 LUIS 进行聊天机器人开发还比较陌生。我一直坚持使用已部署的 LUIS 和 QnA Maker 来测试我的机器人。我本质上是在 LUIS 上提出问题,但如何根据 LUIS 提出的问题获得答复?谢谢!
最佳答案
要调用 QNA 生成器,您可以查看以下代码:
调用 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);
}
用于调用 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
希望能有所帮助。
关于c# - 如何根据 LUIS 问题创建响应? ( azure ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59545559/