如何使多个具有相同问题但答案不同的常见问题(KB)?

时间:2019-05-13 09:03:28

标签: luis qnamaker

我无法保留聊天机器人对话的上下文(FAQ)。

我已根据此文档成功集成了LUIS + QnAmaker。 https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/tutorials/integrate-qnamaker-luis

我大约有3 KB,其中包含相同的问题,但答案不同。聊天机器人应该能够过滤到所需的常见问题,并且以下答案应来自用户选择的常见问题。当前,除非我这样回答我的问题,否则它仅返回答案的第一个KB: 我可以得到FAQ1的答案吗?要么 我可以找到FAQ2的答案吗?

希望我可以在这里获得社区的帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

当您使用多个具有相同问题的知识库时,可以通过向问题/答案集中添加不同的元数据来隔离答案。元数据只是名称/值对的集合。使用QnAMakerDialog时,您可以设置filtering or boosting metadata,它将作为任何查询的一部分传递给QnA Maker Service。

您可以创建QnA Maker对话框,并将单个名称/值元数据对添加到该对话框的MetadataFilter属性中。然后,这将导致对话框仅接收标有相同名称/值元数据的答案。

MessagesController:

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);
    }
}

示例: 元数据标记 enter image description here

测试时,结果如下:

  • 使用KnowledgeBase:kb1

enter image description here

  • 使用KnowledgeBase:kb2

enter image description here

希望这会有所帮助!