bot框架-使用源或多个知识库

时间:2018-07-31 23:10:06

标签: c# botframework bots azure-cognitive-services qnamaker

我正在使用c#中的bot框架开发QnA机器人,并且我有多个常见问题解答,这些问题具有相似甚至相同的答案。因此,我正在研究一种更好地隔离数据的方法,目前我看到两个选择:

  1. 使用相同的知识库并按来源隔离问题
  2. 使用多个知识库

第二种选择似乎可行,但需要更多的开发时间和成本来托管KB,因此我想尝试第一种选择。问题是,我找不到按来源过滤或隔离答案的任何方式。有没有人尝试使用它并获得成功?

1 个答案:

答案 0 :(得分:2)

  

问题是,我找不到按来源过滤或隔离答案的任何方式。

即使查询相同,知识库答案也可能因metadata tag而异。您可以通过在问题/答案集中添加不同的元数据来尝试隔离答案。

enter image description here

对于filter answers using metadata,您可以参考以下示例代码。

在MessagesController中:

Array ( [0] => Array ( [skillName] => JDK [comments] => [jobRating] => 2 [userRating] => 3 [skillGap] => -1 [hrRating] => 2 ) [1] => Array ( [skillName] => Java Servlets [comments] => [jobRating] => 4 [userRating] => 3 [skillGap] => 1 [hrRating] => 3 ) ) 

在QnAMakerDialog中:

[BotAuthentication]
public class MessagesController : ApiController
{
    internal static IDialog<object> MakeRoot()
    {
        var qnaDialog = new Dialogs.MyQnADialog
        {
            MetadataFilter = new List<Metadata>()
            {
                new Metadata()
                {
                    Name = "knowledgebase",
                    Value = "base1"

                    //Name = "knowledgebase",
                    //Value = "base2"
                }
            }
        };

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

    //......
    // other code logic
    //......
}

测试结果:

1)和元数据[Serializable] [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

2)和元数据knowledgebase:base1

enter image description here

注意: 我正在使用QnAMakerDialog -Version 3.1.2