我目前在我的QnA对上有跟进提示,但是在本地运行机器人或使用网络聊天时,这些提示不会显示。有没有办法使用Virtual Assistant模板启用此功能?
使用QnA机器人但不能在Virtual Assistant上使用时,后续提示会起作用
答案 0 :(得分:1)
Steven Kanberg发布的C#.NET Core示例是一个很好的资源。如果您喜欢教程样式指南,那么这可能会有所帮助:https://www.joji.me/en-us/blog/implement-follow-up-prompt-for-qna-bot/。
其中概述的步骤是:
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Text;
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;
}
}
// 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);
}
注意:
请确保创建IConfiguration _configuration
属性,将IConfiguration配置传递到构造函数中,并使用适当的QnAKnowledgebaseId和QnAAuthKey更新appsettings.json。
如果您使用Bot样本之一作为起点,请注意,appsettings.json中的QnAAuthKey
可能会改名为QnAEndpointKey
。
您还将需要一个GetHostName()函数,或仅将其替换为机器人的qna主机名的url。