简体   繁体   English

在Virtual Assistant上启用QnA后续提示

[英]Enable QnA Follow-Up Prompts on Virtual Assistant

I currently have follow-up prompts on my QnA pairs but those prompts does not show-up when running the bot locally or using the webchat. 我目前在QnA对上有跟进提示,但是在本地运行机器人或使用网络聊天时,这些提示不会显示。 Is there a way to get this enabled using the Virtual Assistant template? 有没有办法使用Virtual Assistant模板启用此功能?

The follow-up prompts works when using a QnA bot but not on Virtual Assistant 使用QnA漫游器但在Virtual Assistant上无法使用时,后续提示会起作用

The C# .NET Core sample posted by Steven Kanberg is a great resource. Steven Kanberg发布的C#.NET Core示例是一个很好的资源。 If you like tutorial style guides, this one may help : https://www.joji.me/en-us/blog/implement-follow-up-prompt-for-qna-bot/ . 如果您喜欢教程样式指南,那么这可能会有所帮助: https : //www.joji.me/zh-cn/blog/implement-follow-up-prompt-for-qna-bot/

The steps outlined there are : 概述的步骤有:

  1. Edit your Bots\\YourBotName.cs, add following namespaces we will use to support follow-up prompts: 编辑您的Bots \\ YourBotName.cs,添加以下名称空间,我们将使用这些名称空间来支持后续提示:
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Text;
  1. Add following classes to match the JSON response shape: 添加以下类以匹配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. After the qnaMaker.GetAnswersAsync succeeds and there is valid answer, perform an additional HTTP query to check the follow-up prompts: 在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. Test it in Bot Framework Emulator, and it should now display the follow-up prompts as expected. 在Bot Framework Emulator中对其进行测试,现在它应该显示预期的后续提示。

Notes: 笔记:

Be sure to create IConfiguration _configuration property, pass IConfiguration configuration into your constructor, and update your appsettings.json with the appropriate QnAKnowledgebaseId and QnAAuthKey. 确保创建IConfiguration _configuration属性,将IConfiguration配置传递到构造函数中,并使用适当的QnAKnowledgebaseId和QnAAuthKey更新appsettings.json。

If you used one of the Bot Samples as a starting point, note that QnAAuthKey in appsettings.json will probably be named QnAEndpointKey instead. 如果您使用Bot样本之一作为起点,请注意, QnAAuthKey中的QnAAuthKey可能会QnAEndpointKeyQnAEndpointKey

You will also need a GetHostName() function or just replace that with the url for your bot's qna hostname. 您还需要一个GetHostName()函数,或仅将其替换为机器人的qna主机名的url。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM