【问题标题】:Mocking LUIS response with LuisRecognizer not working使用 LuisRecognizer 模拟 LUIS 响应不起作用
【发布时间】:2020-01-22 19:15:08
【问题描述】:

我正在尝试通过 nock 模拟对 LUIS 的调用,它使用来自 botbuilder-ai 的 LuisRecognizer。这是相关信息。

机器人本身正在调用 LUIS 并通过 const recognizerResult = await this.dispatchRecognizer.recognize(context); 获取结果。我抓到的实际结果如下:

{"text":"I want to look up my order","intents":{"viewOrder":{"score":0.996454835},"srStatus":{"score":0.0172454268},"expediteOrder":{"score":0.0108480565},"escalate":{"score":0.007967358},"qna":{"score":0.00694736559},"Utilities_Cancel":{"score":0.005627355},"manageProfile":{"score":0.004953466},"getPricing":{"score":0.001781322},"Utilities_Help":{"score":0.0007197641},"getAvailability":{"score":0.0005667514},"None":{"score":0.000321137835}},"entities":{"$instance":{}},"sentiment":{"label":"negative","score":0.171873689},"luisResult":{"query":"I want to look up my order","topScoringIntent":{"intent":"viewOrder","score":0.996454835},"intents":[{"intent":"viewOrder","score":0.996454835},{"intent":"srStatus","score":0.0172454268},{"intent":"expediteOrder","score":0.0108480565},{"intent":"escalate","score":0.007967358},{"intent":"qna","score":0.00694736559},{"intent":"Utilities.Cancel","score":0.005627355},{"intent":"manageProfile","score":0.004953466},{"intent":"getPricing","score":0.001781322},{"intent":"Utilities.Help","score":0.0007197641},{"intent":"getAvailability","score":0.0005667514},{"intent":"None","score":0.000321137835}],"entities":[],"sentimentAnalysis":{"label":"negative","score":0.171873689}}}

为简洁起见,我将在下面将其称为“recognizerResult”。我用nock成功拦截了我的测试文件中的API调用,配置如下:

nock('https://westus.api.cognitive.microsoft.com')
.post(/.*/)
.reply(200,{recognizerResult});

我尝试将 JSON 对象和字符串都返回,但我几乎可以肯定这需要是所示的 JSON 对象(我正在使用相同的方法模拟对 QnA 制造商的调用)。当我通过 mocha 运行此测试时,出现以下错误:

TypeError: Cannot read property 'replace' of undefined
    at LuisRecognizerV2.normalizeName (node_modules\botbuilder-ai\src\luisRecognizerOptionsV2.ts:96:21)
    at luisResult.intents.reduce (node_modules\botbuilder-ai\src\luisRecognizerOptionsV2.ts:104:31)
    at Array.reduce (<anonymous>)
    at LuisRecognizerV2.getIntents (node_modules\botbuilder-ai\src\luisRecognizerOptionsV2.ts:102:32)
    at LuisRecognizerV2.<anonymous> (node_modules\botbuilder-ai\src\luisRecognizerOptionsV2.ts:81:27)
    at Generator.next (<anonymous>)
    at fulfilled (node_modules\botbuilder-ai\lib\luisRecognizerOptionsV2.js:11:58)
    at process._tickCallback (internal/process/next_tick.js:68:7)

我查看了 luisRecognizerOptionsV2.ts 文件中的相关代码,但看不出哪里有问题。替换是规范化意图名称的一部分,用于将不支持的字符替换为“_”。该机器人在部署到 Azure(和本地)时正常运行,并且测试工作无需模拟调用。但是,我真的希望能够在不进行实际 LUIS 调用的情况下对此进行测试。任何想法为什么我会收到此错误以及如何解决?

作为参考,这是正在运行的 QnA Maker 的模拟,但请注意,我使用的是简单的 REST 调用而不是识别器。

nock('https://myqnaservicename.azurewebsites.net')
.post(/.*/)
.reply(200, {"answers": [{"questions": ["I need an unrecognized utterance for testing"], "answer": "I can hear you now!", "score": 28.48, "id": 1234}]});

【问题讨论】:

    标签: node.js botframework azure-language-understanding nock


    【解决方案1】:

    问题是您的 {recognizerResult} 保存到 const recognizerResult 的内容,但 不是该 API 调用返回的内容。

    需要大量挖掘才能找到所有内容,但 V2 LUIS 客户端会获取 API 响应,然后是 converts it into recognizerResult

    您有几个选项可以“修复”这个问题:

    1. node_modules\botbuilder-ai\src\luisRecognizerOptionsV2 文件的const result = 行上设置断点并抓取luisResult
    2. 使用 Fiddler 之类的东西来记录实际的 API 响应并使用它
    3. 手动编写

    作为参考,您可以在我们的测试中看到我们是如何做到这一点的:

    你可以看到我们的nock()returns response.v2,也就是does not contain .topScoringIntent,也就是what it's looking for,这就是抛出错误的原因。

    具体来说,模拟响应需要只是 v2/luisResults 属性。也就是说,在使用luisRecognizer时,nock中设置的响应需要是

    .reply(200,{ "query": "Sample query", "topScoringIntent": { "intent": "desiredIntent", "score":1}, "entities":[]});

    如果您查看上面链接的测试数据,实际响应中还有其他属性。但是,如果您只是试图让 topIntent 来测试路由,那么这是最低要求的响应。如果您需要其他属性,您可以添加它们,例如您可以在 v2 中添加所有内容,如 this file 或一些更复杂的文件,例如多个意图。

    【讨论】:

    • 我根据示例使用“v2”属性更新了响应,但我继续收到相同的错误。出于某种原因,我目前无法运行调试器(正在努力解决这个问题)。我也只是在 luisRecognizerOptionsV2 文件中放了一个 console.log 语句,但这也没有显示任何内容。我也没有在 Fiddler 中看到包。
    • @billoverton 有趣。介意分享你的代码吗?可能会让我更快/更容易地帮助调试。 vDASHmicricATmicrosoftDOTcom(用字符替换全大写)。可能需要更改post() 路径以匹配我们的路径?这会让我有点吃惊,但到目前为止,我还没有什么大惊小怪的。
    • 我明白了。我没有意识到我需要提供 v2 属性作为响应,而不是作为响应的一部分。其他结果由模块添加。换句话说,我在 nock 中的回复必须是 .reply(200,{ "query": "Sample query", "topScoringIntent": { "intent": "viewOrder", "score":0.991234567}, "entities":[]}); 我现在已经可以正常工作并会接受您的回答,但明确说明 nock 响应需要是什么样子可能会有所帮助。
    • @billoverton 我认为我明白你的意思。您能否编辑我的答案以更好地匹配所需内容?
    • 已添加。一旦出现,请查看并确保您同意我的发现。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多