QnA制造商从多个kb中获得答案

时间:2019-12-27 11:15:09

标签: qnamaker

目前,我正在从QnA制造商的单个kb中获得响应。

当前代码。

  QnAMaker _qnaservice = new QnAMaker(new QnAMakerEndpoint
        {
            EndpointKey = _qnaconfig.AuthKey,
            Host = _qnaconfig.EndPoint,
            KnowledgeBaseId = "{KBID}"
        }, new QnAMakerOptions { Top = 3, StrictFilters = channelData.Filters?.ToArray() });
        var response = await _qnaservice.GetAnswersAsync(stepContext.Context);

是否可以从多个KB中获取结果?

1 个答案:

答案 0 :(得分:1)

那里的代码仅从KBID的一个kb中提取了前三个答案。您必须编写代码以显示三个答案,如果未设置scorethreshold,它将返回大约0.3或更高的答案。然后要在聊天中显示它们,您将必须基本上遍历它们。例如:

var options = new QnAMakerOptions { Top = 3, ScoreThreshold=0.0F };

var httpClient = _httpClientFactory.CreateClient();

var qnaMaker = new QnAMaker(new QnAMakerEndpoint
{
    KnowledgeBaseId = _configuration["QnAKnowledgebaseId"],
    EndpointKey = _configuration["QnAEndpointKey"],
    Host = _configuration["QnAEndpointHostName"]
},
options,
httpClient);


_logger.LogInformation("Calling QnA Maker");

// The actual call to the QnA Maker service.
var response = await qnaMaker.GetAnswersAsync(turnContext);
if (response != null && response.Length > 0)
{

    for (int i = 0; i < response.Length; i++)
    {
        await turnContext.SendActivityAsync(MessageFactory.Text(response[i].Answer), cancellationToken);
    }    
}

对于我的知识库,它显示:

Top Three answers displayed in chat

现在,如果要显示来自多个KB的前三个答案,则必须构造多个QnAMaker。

var qnaMaker = new QnAMaker(new QnAMakerEndpoint
{
    KnowledgeBaseId = _configuration["QnAKnowledgebaseId"],
    EndpointKey = _configuration["QnAEndpointKey"],
    Host = _configuration["QnAEndpointHostName"]
},
options,
httpClient);

var qnaMaker2 = new QnAMaker(new QnAMakerEndpoint
{
    KnowledgeBaseId = _configuration["QnAKnowledgebaseId2"],
    EndpointKey = _configuration["QnAEndpointKey"],
    Host = _configuration["QnAEndpointHostName"]
},
options,
httpClient);


//LATER IN CODE:
var response = await qnaMaker.GetAnswersAsync(turnContext);
var response2 = await qnaMaker2.GetAnswersAsync(turnContext);

if ((response != null && response.Length > 0) && (response2 != null && response2.Length > 0))
{
    await turnContext.SendActivityAsync(MessageFactory.Text("Answers from KB1:"), cancellationToken);
    for (int i = 0; i < response.Length; i++)
    {
        await turnContext.SendActivityAsync(MessageFactory.Text(response[i].Answer), cancellationToken);
    }
    await turnContext.SendActivityAsync(MessageFactory.Text("Answers from KB2:"), cancellationToken);
    for (int i = 0; i < response2.Length; i++)
    {
        await turnContext.SendActivityAsync(MessageFactory.Text(response2[i].Answer), cancellationToken);
    }
}
else if (response != null && response.Length > 0)
{
    await turnContext.SendActivityAsync(MessageFactory.Text("Answers from JUST KB1:"), cancellationToken);
    for (int i = 0; i < response.Length; i++)
    {
        await turnContext.SendActivityAsync(MessageFactory.Text(response[i].Answer), cancellationToken);
    }
}
else if (response2 != null && response2.Length > 0)
{
    await turnContext.SendActivityAsync(MessageFactory.Text("Answers from JUST KB2:"), cancellationToken);
    for (int i = 0; i < response2.Length; i++)
    {
        await turnContext.SendActivityAsync(MessageFactory.Text(response2[i].Answer), cancellationToken);
    }
}
else
{
    await turnContext.SendActivityAsync(MessageFactory.Text("No QnA Maker answers were found."), cancellationToken);
}

然后只需执行相同的长度检查,并为每个执行以下操作:

all the answers