From 339e8501a981b5a0d35a9ab7ad1c5d895227e838 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Tue, 31 Aug 2021 15:40:21 -0300 Subject: [PATCH] fix(basic.gblib): Fixes /answer bug. --- packages/core.gbapp/services/GBMinService.ts | 8 ++- packages/kb.gbapp/dialogs/AskDialog.ts | 68 +++++++++++--------- 2 files changed, 43 insertions(+), 33 deletions(-) diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index b0c549f6..3eafbf78 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -921,7 +921,7 @@ export class GBMinService { if (!min["conversationWelcomed"][step.context.activity.conversation.id]) { min["conversationWelcomed"][step.context.activity.conversation.id] = true; - + GBLog.info(`Auto start (web) dialog is now being called: ${startDialog} for ${min.instance.instanceId}...`); await GBVMService.callVM(startDialog.toLowerCase(), min, step, this.deployer); } @@ -1255,10 +1255,12 @@ export class GBMinService { user: user ? user.dataValues : null }; await CollectionUtil.asyncForEach(min.appPackages, async (e: IGBPackage) => { - nextDialog = await e.onExchangeData(min, 'handleAnswer', data); + if (!nextDialog) { + nextDialog = await e.onExchangeData(min, 'handleAnswer', data); + } }); data.step = null; - GBLog.info(`/answer being called from processMessageActivity.`); + GBLog.info(`/answer being called from processMessageActivity (nextDialog=${nextDialog}).`); await step.beginDialog(nextDialog ? nextDialog : '/answer', { data: data, query: text, diff --git a/packages/kb.gbapp/dialogs/AskDialog.ts b/packages/kb.gbapp/dialogs/AskDialog.ts index 08f32181..c6d3a400 100644 --- a/packages/kb.gbapp/dialogs/AskDialog.ts +++ b/packages/kb.gbapp/dialogs/AskDialog.ts @@ -248,43 +248,51 @@ export class AskDialog extends IGBDialog { } } - // Answers using Search or NLP responses. + // Try to answer by search. if (answer) { return await AskDialog.handleAnswer(service, min, step, answer); - } else if (!(await min.conversationalService.routeNLP(step, min, text))) { + } - if (process.env.GBMODELS_SERVER) { - text = await min.conversationalService.translate(min, text, 'en'); - let answered = false; + // Tries to answer by NLP. - const docs = await min.kbService['getDocs'](min.instance.instanceId); - - await CollectionUtil.asyncForEach(docs, async (doc: GuaribasAnswer) => { - - if (!answered) { - - const answerText = await min.kbService['readComprehension'](min.instance.instanceId, doc.content, text); - answered = true; - text = await min.conversationalService.translate(min, text, user.systemUser.locale - ? user.systemUser.locale - : min.core.getParam(min.instance, 'Locale', GBConfigService.get('LOCALE'))); - await min.conversationalService.sendText(min, step, answerText); - await min.conversationalService.sendEvent(min, step, 'stop', undefined); - } - - }); - return await step.replaceDialog('/ask', { isReturning: true }); - } - - - } else { - const message = min.core.getParam(min.instance, 'Not Found Message', - Messages[locale].did_not_find); - - await min.conversationalService.sendText(min, step, message); + if (await min.conversationalService.routeNLP(step, min, text)) { return await step.replaceDialog('/ask', { isReturning: true }); } + + // Tries to answer by Reading Comprehension. + + if (process.env.GBMODELS_SERVER) { + text = await min.conversationalService.translate(min, text, 'en'); + let answered = false; + + const docs = await min.kbService['getDocs'](min.instance.instanceId); + + await CollectionUtil.asyncForEach(docs, async (doc: GuaribasAnswer) => { + + if (!answered) { + + const answerText = await min.kbService['readComprehension'](min.instance.instanceId, doc.content, text); + answered = true; + text = await min.conversationalService.translate(min, text, user.systemUser.locale + ? user.systemUser.locale + : min.core.getParam(min.instance, 'Locale', GBConfigService.get('LOCALE'))); + await min.conversationalService.sendText(min, step, answerText); + await min.conversationalService.sendEvent(min, step, 'stop', undefined); + } + + }); + return await step.replaceDialog('/ask', { isReturning: true }); + } + + // Not found. + + const message = min.core.getParam(min.instance, 'Not Found Message', + Messages[locale].did_not_find); + + await min.conversationalService.sendText(min, step, message); + return await step.replaceDialog('/ask', { isReturning: true }); + } ]; }