【发布时间】: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