在Virtual Assistant上启用QnA后续提示

时间:2019-08-30 12:44:00

标签: c# botframework luis qnamaker

我目前在我的QnA对上有跟进提示,但是在本地运行机器人或使用网络聊天时,这些提示不会显示。有没有办法使用Virtual Assistant模板启用此功能?

使用QnA机器人但不能在Virtual Assistant上使用时,后续提示会起作用

1 个答案:

答案 0 :(得分:1)

Steven Kanberg发布的C#.NET Core示例是一个很好的资源。如果您喜欢教程样式指南,那么这可能会有所帮助:https://www.joji.me/en-us/blog/implement-follow-up-prompt-for-qna-bot/

其中概述的步骤是:

  1. 编辑您的Bots \ YourBotName.cs,添加以下用于支持后续提示的名称空间:
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Text;
  1. 添加以下类以匹配JSON响应形状:
class FollowUpCheckResult
 {
     [JsonProperty("answers")]
     public FollowUpCheckQnAAnswer[] Answers
     {
         get;
         set;
     }
 }

 class FollowUpCheckQnAAnswer
 {
     [JsonProperty("context")]
     public FollowUpCheckContext Context
     {
         get;
         set;
     }
 }

 class FollowUpCheckContext
 {
     [JsonProperty("prompts")]
     public FollowUpCheckPrompt[] Prompts
     {
         get;
         set;
     }
 }

 class FollowUpCheckPrompt
 {
     [JsonProperty("displayText")]
     public string DisplayText
     {
         get;
         set;
     }
 }
  1. 在qnaMaker.GetAnswersAsync成功并且有有效答案之后,执行附加的HTTP查询以检查后续提示:
// The actual call to the QnA Maker service.
 var response = await qnaMaker.GetAnswersAsync(turnContext);
 if (response != null && response.Length > 0)
 {
     // create http client to perform qna query
     var followUpCheckHttpClient = new HttpClient();

     // add QnAAuthKey to Authorization header
     followUpCheckHttpClient.DefaultRequestHeaders.Add("Authorization", _configuration["QnAAuthKey"]);

     // construct the qna query url
     var url = $"{GetHostname()}/knowledgebases/{_configuration["QnAKnowledgebaseId"]}/generateAnswer"; 

     // post query
     var checkFollowUpJsonResponse = await followUpCheckHttpClient.PostAsync(url, new StringContent("{\"question\":\"" + turnContext.Activity.Text + "\"}", Encoding.UTF8, "application/json")).Result.Content.ReadAsStringAsync();

     // parse result
     var followUpCheckResult = JsonConvert.DeserializeObject<FollowUpCheckResult>(checkFollowUpJsonResponse);

     // initialize reply message containing the default answer
     var reply = MessageFactory.Text(response[0].Answer);

     if (followUpCheckResult.Answers.Length > 0 && followUpCheckResult.Answers[0].Context.Prompts.Length > 0)
     {
         // if follow-up check contains valid answer and at least one prompt, add prompt text to SuggestedActions using CardAction one by one
         reply.SuggestedActions = new SuggestedActions();
         reply.SuggestedActions.Actions = new List<CardAction>();
         for (int i = 0; i < followUpCheckResult.Answers[0].Context.Prompts.Length; i++)
         {
             var promptText = followUpCheckResult.Answers[0].Context.Prompts[i].DisplayText;
             reply.SuggestedActions.Actions.Add(new CardAction() { Title = promptText, Type = ActionTypes.ImBack, Value = promptText });
         }
     }
     await turnContext.SendActivityAsync(reply, cancellationToken);
 }
 else
 {
     await turnContext.SendActivityAsync(MessageFactory.Text("No QnA Maker answers were found."), cancellationToken);
 }

  1. 在Bot Framework仿真器中对其进行测试,现在它应该显示预期的后续提示。

注意:

请确保创建IConfiguration _configuration属性,将IConfiguration配置传递到构造函数中,并使用适当的QnAKnowledgebaseId和QnAAuthKey更新appsettings.json。

如果您使用Bot样本之一作为起点,请注意,appsettings.json中的QnAAuthKey可能会改名为QnAEndpointKey

您还将需要一个GetHostName()函数,或仅将其替换为机器人的qna主机名的url。