我如何使用 QnAMaker 为同一查询提供随机答案
How can i use QnAMaker to provide random answers to same query
我在想,因为机器人有一些通用的问题,比如你好吗?可能我有大约 10 个答案,我希望问答制作者随机选择,而不是每次都选择相同的答案。
或者还有诸如给我讲个故事之类的问题
some generic questions like how are you ? may be i have around 10 answers which i would like Q&A maker to choose randomly not every time same answer.
要达到这个要求,可以试试这个方法:
1) 添加一个QnA对并使用特殊字符(例如|
)拆分问题how are you?
的答案
2) 覆盖RespondFromQnAMakerResultAsync
方法,并在该方法中拆分响应并随机检索答案
protected override async Task RespondFromQnAMakerResultAsync(IDialogContext context, IMessageActivity message, QnAMakerResults result)
{
// This will only be called if Answers isn't empty
var response = result.Answers.First().Answer;
var answersforhowareyou = response.Split('|');
if (answersforhowareyou.Count() > 1)
{
Random rnd = new Random();
int index = rnd.Next(answersforhowareyou.Count());
response = answersforhowareyou[index];
}
await context.PostAsync(response);
}
测试结果:
我在想,因为机器人有一些通用的问题,比如你好吗?可能我有大约 10 个答案,我希望问答制作者随机选择,而不是每次都选择相同的答案。
或者还有诸如给我讲个故事之类的问题
some generic questions like how are you ? may be i have around 10 answers which i would like Q&A maker to choose randomly not every time same answer.
要达到这个要求,可以试试这个方法:
1) 添加一个QnA对并使用特殊字符(例如|
)拆分问题how are you?
2) 覆盖RespondFromQnAMakerResultAsync
方法,并在该方法中拆分响应并随机检索答案
protected override async Task RespondFromQnAMakerResultAsync(IDialogContext context, IMessageActivity message, QnAMakerResults result)
{
// This will only be called if Answers isn't empty
var response = result.Answers.First().Answer;
var answersforhowareyou = response.Split('|');
if (answersforhowareyou.Count() > 1)
{
Random rnd = new Random();
int index = rnd.Next(answersforhowareyou.Count());
response = answersforhowareyou[index];
}
await context.PostAsync(response);
}
测试结果: