【问题标题】:Enable QnA Follow-Up Prompts on Virtual Assistant在虚拟助手上启用 QnA 跟进提示
【发布时间】:2019-08-30 12:44:00
【问题描述】:

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

后续提示在使用 QnA 机器人时有效,但在虚拟助手上无效

【问题讨论】:

  • 这个问题缺少一些东西 - 你能解决这个问题吗?
  • @DFBerry 您能否更明确地说明新 SO 用户缺少什么?
  • @samueljohnpaul 任何样本的 QnA 代码都应该能够被提取并用于不同的机器人。你能显示你的代码不起作用吗?
  • 您可以参考此 C# .NET Core sample,它演示了提取 QnA Maker 后续(又名多轮)提示。这可能有助于确定您的 VA 代码中的某些内容是否不正确。

标签: c# botframework azure-language-understanding qnamaker


【解决方案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 Emulator 中进行测试,现在应该会按预期显示后续提示。

注意事项:

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

如果您使用其中一个 Bot 示例作为起点,请注意 appsettings.json 中的 QnAAuthKey 可能会被命名为 QnAEndpointKey

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

【讨论】:

  • 非常感谢!完美运行!
  • 这是一个很好的解决方案,但是如果使用 MS Teams 作为您的频道,提示将不起作用,因为 Teams 不支持 SuggestedActions,this post 说使用卡片代替提示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
相关资源
最近更新 更多