我试图通过nock来模拟对LUIS的调用,nock使用的是僵尸生成器-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 maker的调用)。当我通过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}]});发布于 2020-01-22 20:32:06
问题是,您的{recognizerResult}是保存到const recognizerResult中的内容,而不是该API调用返回的内容。
需要进行大量的挖掘才能找到所有这些信息,但是V2 LUIS客户端会得到API响应,然后是recognizerResult。
您有几个“修复”选项:
node_modules\botbuilder-ai\src\luisRecognizerOptionsV2文件中的const result =行上设置一个断点,并获取luisResult。作为参考,您可以在测试中看到我们是如何做到这一点的:
您可以看到我们的nock() response.v2,它是.topScoringIntent,也就是它在寻找什么,这就是抛出错误的原因。
具体来说,模拟响应只需要v2/luisResults属性。换句话说,当使用luisRecognizer时,nock中的响应集需要
.reply(200,{ "query": "Sample query", "topScoringIntent": { "intent": "desiredIntent", "score":1}, "entities":[]});
如果查看上面链接的测试数据,实际响应中还有其他属性。但是,如果您只是试图让topIntent测试路由,那么这是最低要求的响应。如果您需要其他属性,您可以添加它们,例如,您可以在v2中添加所有内容,比如在这个文件或一些涉及更多的文件中,比如多个意图。
https://stackoverflow.com/questions/59866787
复制相似问题