当您对相同问题使用多个 KB 时,您可以通过向问题/答案集中添加不同的元数据来分隔答案。元数据只是名称/值对的集合。使用 QnAMakerDialog 时,您可以设置 filtering or boosting metadata,这将作为任何查询的一部分传递给 QnA Maker 服务。
您可以创建 QnA Maker 对话框并将单个名称/值元数据对添加到对话框上的 MetadataFilter 属性。这将导致对话框仅接收标记有相同名称/值元数据的答案。
消息控制器:
public class MessagesController : ApiController
{
internal static IDialog<object> MakeRoot()
{
var qnaDialog = new Dialogs.MyQnADialog
{
MetadataFilter = new List<Metadata>()
{
new Metadata()
{
Name = "knowledgeBase",
Value = "kb1"
}
}
};
return qnaDialog;
}
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
await Conversation.SendAsync(activity, MakeRoot);
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
}
同样,如果你想使用其他知识库,你可以传递其他元数据名称/值对。例如,{ Name="knowledgeBase",Value="kb2" }
QnAMAkerDialog:
[QnAMakerService("https://xxxx.azurewebsites.net/qnamaker/", "{EndpointKey_here}", "{KnowledgeBaseId_here}",1)]
public class MyQnADialog : QnAMakerDialog<object>
{
public override async Task NoMatchHandler(IDialogContext context, string originalQueryText)
{
await context.PostAsync($"Sorry, I couldn't find an answer for '{originalQueryText}'.");
context.Done(false);
}
public override async Task DefaultMatchHandler(IDialogContext context, string originalQueryText, QnAMakerResult result)
{
if (result.Answers.FirstOrDefault().Score > 80)
{
await context.PostAsync($"I found {result.Answers.Length} answer(s) that might help...{result.Answers.First().Answer}.");
}
else
{
await context.PostAsync($"Sorry, I couldn't find an answer for '{originalQueryText}'.");
}
context.Done(true);
}
}
示例:
元数据标记
当你测试它时,结果如下:
希望对你有帮助!!!