机器人框架 - 使用源或多个知识库
bot framework - working with source or multiple knowledge base
我正在使用 c# 中的 bot 框架开发 QnA 机器人,我有多个常见问题解答,这些常见问题解答具有相似甚至相同的问题和不同的答案。所以我正在研究一种更好地隔离数据的方法,目前我看到两个选项:
- 使用相同的知识库并按来源分离问题
- 使用多个知识库
第二个选项似乎可行,但需要更多的开发时间和托管知识库的成本,因此我想尝试第一个选项。问题是,我没有找到任何一种方法来按来源过滤或分离答案。有没有人尝试过使用它并发现任何成功?
The problem is, I didn't find any kind of way to filter or segregate answers by source.
知识库答案可能因 metadata tag 而异,即使查询相同。您可以尝试通过向 question/answer 集添加不同的元数据来分离答案。
至filter answers using metadata,可以参考下面的示例代码
在 MessagesController 中:
[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
//......
}
在 QnAMakerDialog 中:
[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);
}
}
测试结果:
1) 带元数据 knowledgebase:base1
2) 带元数据 knowledgebase:base2
我正在使用 c# 中的 bot 框架开发 QnA 机器人,我有多个常见问题解答,这些常见问题解答具有相似甚至相同的问题和不同的答案。所以我正在研究一种更好地隔离数据的方法,目前我看到两个选项:
- 使用相同的知识库并按来源分离问题
- 使用多个知识库
第二个选项似乎可行,但需要更多的开发时间和托管知识库的成本,因此我想尝试第一个选项。问题是,我没有找到任何一种方法来按来源过滤或分离答案。有没有人尝试过使用它并发现任何成功?
The problem is, I didn't find any kind of way to filter or segregate answers by source.
知识库答案可能因 metadata tag 而异,即使查询相同。您可以尝试通过向 question/answer 集添加不同的元数据来分离答案。
至filter answers using metadata,可以参考下面的示例代码
在 MessagesController 中:
[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
//......
}
在 QnAMakerDialog 中:
[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);
}
}
测试结果:
1) 带元数据 knowledgebase:base1
2) 带元数据 knowledgebase:base2