首页 > 解决方案 > Azure Bot Framework - 无法访问的 QnA Maker 服务

问题描述

我们无法从 Bot Framework 调用 QnA Maker 知识库。代码编译得很好。机器人模拟器也会打开并运行。该代码基于以下 GitHub 存储库:

https://github.com/Microsoft/BotBuilder-Samples/tree/main/samples/csharp_dotnetcore/14.nlp-with-dispatch

在 repo 的代码中,有一个带有两个 LUIS 实例和一个 QnA Maker 实例的调度模型。我们能够成功地将调度代码更改为指向我们的三个 QnA Maker 实例。这在机器人模拟器中工作,我们能够在没有错误或异常发生的情况下进行通信。

但是 - 我们正在使用带有后续提示的 QnA Maker KB。尽管代码正在运行,但没有生成任何后续提示;只有答案被返回。我找到了以下博客文章,并尝试修改我们的一个函数,以便能够从 QnA Maker KB 中获取后续提示:

https://joji.me/en-us/blog/implement-follow-up-prompt-for-qna-bot/

这并没有完全破坏我们的代码。代码仍然可以编译和运行。我们可以成功地提出直接到其他 QnA Maker KB 的问题(返回答案,但没有后续提示),但是当我们提供将调度工具定向到我们修改的代码的输入(我们尝试返回的地方)时,我们会返回以下错误后续提示):

控制台错误消息:(注意:仅当我们在 Bot Emulator 中测试 bot 并提供将 Dispatch 定向到此 KB 的输入时才会发生此错误)

fail: Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter[0]
      [OnTurnError] unhandled error : Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.BotBuilderSamples.DispatchBot.ProcessfindPartQnAAsync(ITurnContext`1 turnContext, CancellationToken cancellationToken) in C:\Users\c50941\source\repos\AIM bot\BotBuilder-Samples\samples\csharp_dotnetcore\14.nlp-with-dispatch\Bots\DispatchBot.cs:line 155
   at Microsoft.BotBuilderSamples.DispatchBot.DispatchToTopIntentAsync(ITurnContext`1 turnContext, String intent, RecognizerResult recognizerResult, CancellationToken cancellationToken) in C:\Users\c50941\source\repos\AIM bot\BotBuilder-Samples\samples\csharp_dotnetcore\14.nlp-with-dispatch\Bots\DispatchBot.cs:line 109
   at Microsoft.BotBuilderSamples.DispatchBot.OnMessageActivityAsync(ITurnContext`1 turnContext, CancellationToken cancellationToken) in C:\Users\c50941\source\repos\AIM bot\BotBuilder-Samples\samples\csharp_dotnetcore\14.nlp-with-dispatch\Bots\DispatchBot.cs:line 88
   at Microsoft.Bot.Builder.ActivityHandler.OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken)
   at Microsoft.Bot.Builder.BotFrameworkAdapter.TenantIdWorkaroundForTeamsMiddleware.OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken)
   at Microsoft.Bot.Builder.MiddlewareSet.ReceiveActivityWithStatusAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken cancellationToken)
   at Microsoft.Bot.Builder.BotAdapter.RunPipelineAsync(ITurnContext turnContext, BotCallbackHandler callback, CancellationToken cancellationToken) 

QnAMaker Trace:(来自机器人模拟器)

Unable to find a QnA Maker service with Knowledge Base ID d1f*****-****-****-****-************. Please add a QnA Maker service to your bot.

根据对其他问题的研究,我们知道 appsettings.json 中没有正确的信息是很常见的。我们已经三次检查这在我们的最终是正确的。这也证明我们可以成功调用其他两个 Qna Maker KB,因为它们的凭据通过相同的方法和相同的文件存储。这是我们修改后的函数来调用特定的知识库,目的也是为了获得后续提示。根据控制台错误,这似乎是我们的错误所在。此外,这是我们修改的唯一函数,因为整个程序完全运行:(我们在输出日志告诉我们存在问题的行中注释了 ERROR)

private async Task ProcessfindPartQnAAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
    _logger.LogInformation("ProcessfindPartQnAAsync");

    var results = await _botServices.findPartQnA.GetAnswersAsync(turnContext);

    // The actual call to the QnA Maker service.
    var response = await _botServices.findPartQnA.GetAnswersAsync(turnContext);
    if (response != null && response.Length > 0)
    {
        // create http client to perform qna query
        var followUpCheckHttpClient = _httpClientFactory.CreateClient();

        // add QnAAuthKey to Authorization header
        followUpCheckHttpClient.DefaultRequestHeaders.Add("Authorization", _configuration["findPartQnAEndpointKey"]);



        // construct the qna query url
        var url = $"{_configuration["findPartQnAEndpointHostName"]}/knowledgebases/{_configuration["findPartQnAKnowledgebaseId"]}/generateAnswer";
                
        // post query
        // ***** ERROR *****
        var checkFollowUpJsonResponse = await followUpCheckHttpClient.PostAsync(url, new StringContent("{​​​​\"question\":\"" + turnContext.Activity.Text + "\"}​​​​&quot;, Encoding.UTF8, "application/json")).Result.Content.ReadAsStringAsync();

        // parse result
        var followUpCheckResult = JsonConvert.DeserializeObject<FollowUpCheckResult>(checkFollowUpJsonResponse);

        // initialize reply message containing the default answer
        var reply = MessageFactory.Text(response[0].Answer);

        // ***** ERROR *****
        if (followUpCheckResult.Answers.Length > 0 && followUpCheckResult.Answers[0].Context.Prompts.Length > 0)
        {
            // if follow-up check contains valid answer and at least one prompt, add prompt text to SuggestedActions using CardAction one by one
            reply.SuggestedActions = new SuggestedActions();
            reply.SuggestedActions.Actions = new List<CardAction>();
            for (int i = 0; i < followUpCheckResult.Answers[0].Context.Prompts.Length; i++)
            {
                var promptText = followUpCheckResult.Answers[0].Context.Prompts[i].DisplayText;
                reply.SuggestedActions.Actions.Add(new CardAction() { Title = promptText, Type = ActionTypes.ImBack, Value = promptText });
            }
        }
        await turnContext.SendActivityAsync(reply, cancellationToken);
    }
    else
    {
        await turnContext.SendActivityAsync(MessageFactory.Text("No QnA Maker answers were found."), cancellationToken);
    }
}

我们认为我们构建 URL 的方式一定有问题......尽管我们已经三重检查了所有信息是否正确,因为它在我们的 appsettings.json 中命名,并且我们拥有来自的正确凭据我们的 Azure 服务存储在那里。感谢您提供的任何帮助。

标签: c#botframework

解决方案


  1. 通过创建新的 QnA Maker 服务确保您的 QnA Maker 实例没有损坏
  2. 确保您不会每轮多次使用相同的话语调用相同的 QnA Maker 服务

推荐阅读