Botmaker 解决 Qna Maker 跟进问题

Botmaker resolve Qnamaker Followup Questions

对于我当前的项目,我尝试使用仅上下文后续提示。

我现在的问题是,后续提示中的一些问题是相同的。

在 qna ui 中,上下文的分离工作正常,但是当用户从我在我的应用程序中实现的聊天中回答问题时,qna returns 错误的答案。

代码: Botbui旧版本:4.5.3 节点JS:12.10.0

onMessage(){.....

let results = qna.getAnswer(context);
if(results[0].context.prompts.length){ //  Answer with Follow-up
      return context.sendActivity(CardHelper.GetHeroCard(results[0].answer, results[0].context.prompts))
   }else{ // normal answer
      return context.sendActivity(results[0].answer)
   }
}

Sample Questions:

Expected Answer:
I want to learn programming => java => here is our guide
Real Answer:
I want to learn programming => java => that is the java test

Expected Answer:
I want to do a test => java => that is the java test
Real Answer:
I want to do a test => java => that is the java test

如何在代码中实现这些后续提示并且不丢失后续上下文?

首先要了解的是多轮对话和后续提示处于预览状态。这通常意味着人们应该期待错误和缺失的功能。在这种情况下,这意味着该功能不仅在 SDK 中缺失,它甚至在 API 参考 中也缺失。您可以在 Generate Answer reference 中看到对 generateAnswer 端点的调用在其主体中包含 context 属性,但该对象的类型未记录。它链接到响应中返回的 Context 对象类型,而不是您应该放入请求中的对象类型。

既然你的问题提到了 CardHelper.GetHeroCard,我想你已经熟悉 QnA Maker prompting sample. If you are by some chance not familiar with that sample, it is the ultimate source of truth when it comes to multi-turn conversations in QnA Maker. That sample contains the entire answer to your question so I'm unsure why you're not using it. However, you should have also seen what you need to do in the documentation you should be following:

A JSON request to return a non-initial answer and follow-up prompts

Fill the context object to include the previous context.

In the following JSON request, the current question is Use Windows Hello to sign in and the previous question was accounts and signing in.

{
  "question": "Use Windows Hello to sign in",
  "top": 10,
  "userId": "Default",
  "isTest": false,
  "qnaId": 17,
  "context": {
    "previousQnAId": 15,
    "previousUserQuery": "accounts and signing in"
  }
}

QnA Maker 不会自行保存任何状态,因此它取决于您的机器人为其提供上一回合的上下文。您的机器人没有这样做,这就是它无法正常工作的原因。以下是示例代码的简化版本,可帮助您了解需要执行的操作:

async testQnAMaker(turnContext) {
    var qna = new QnAMaker({
        knowledgeBaseId: '<GUID>',
        endpointKey: '<GUID>',
        host: 'https://<APPNAME>.azurewebsites.net/qnamaker'
    });

    var context = await this.qnaState.get(turnContext) || {
        PreviousQnaId: 0,
        PreviousUserQuery: null
    };

    // We're passing a context property into the QnAMakerOptions
    // even though it's not part of the interface yet
    var results = await qna.getAnswers(turnContext, { context });
    var firstResult = results[0];

    if (firstResult) {
        var answer = firstResult.answer;
        var resultContext = firstResult.context;
        var prompts = resultContext && resultContext.prompts;

        if (prompts && prompts.length) {
            await this.qnaState.set(turnContext, {
                PreviousQnaId: firstResult.id,
                PreviousUserQuery: turnContext.activity.text
            });

            answer = ChoiceFactory.forChannel(
                turnContext,
                prompts.map(prompt => prompt.displayText),
                answer);
        }

        await turnContext.sendActivity(answer);
    } else {
        await turnContext.sendActivity("I can't answer that");
    }
}

由于您使用的是当前处于预览状态的功能,因此在弄清楚如何使用它时,您需要发挥自己的聪明才智。我只是使用状态 属性 访问器来保存每个回合上一个问题的上下文,但您可能希望将其构建到对话框中并将每个用户查询保存在对话框状态中。关键是,如果您希望后续提示起作用,则必须保存 QnA Maker 状态。

编辑:事实证明,如果您将上下文放在操作本身中,那么有一种方法可以在没有机器人状态的情况下使用后续提示,但这只有在用户单击按钮而不是键入内容: