在虚拟助手上启用 QnA 跟进提示

Enable QnA Follow-Up Prompts on Virtual Assistant

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

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

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。

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

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