new(all): General Bots Reading Comprehension,.
This commit is contained in:
parent
b755a81725
commit
1d337cf24a
3 changed files with 107 additions and 8 deletions
|
@ -95,12 +95,15 @@ export class DialogKeywords {
|
|||
if (month.length < 2) { month = '0' + month; }
|
||||
if (day.length < 2) { day = '0' + day; }
|
||||
|
||||
const locale = step.context.activity.locale;
|
||||
switch (locale) {
|
||||
case 'pt-BR':
|
||||
const user = await this.min.userProfile.get(step.context, {});
|
||||
const sec = new SecService();
|
||||
const userFull = await sec.getUserFromSystemId(user.systemUser.userId);
|
||||
|
||||
switch (userFull.locale) {
|
||||
case 'pt':
|
||||
return [day, month, year].join('/');
|
||||
|
||||
case 'en-US':
|
||||
case 'en':
|
||||
return [month, day, year].join('/');
|
||||
|
||||
default:
|
||||
|
|
|
@ -48,6 +48,7 @@ import { CollectionUtil, AzureText } from 'pragmatismo-io-framework';
|
|||
import { GBVMService } from '../../basic.gblib/services/GBVMService';
|
||||
import { GBImporter } from '../../core.gbapp/services/GBImporterService';
|
||||
import { GBDeployer } from '../../core.gbapp/services/GBDeployer';
|
||||
import { GBConfigService } from '../../core.gbapp/services/GBConfigService';
|
||||
|
||||
/**
|
||||
* Dialog arguments.
|
||||
|
@ -249,6 +250,32 @@ export class AskDialog extends IGBDialog {
|
|||
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;
|
||||
|
||||
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<string>(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<string>(min.instance, 'Not Found Message',
|
||||
Messages[locale].did_not_find);
|
||||
|
||||
|
|
|
@ -63,6 +63,8 @@ import { GBDeployer } from '../../core.gbapp/services/GBDeployer';
|
|||
import { CSService } from '../../customer-satisfaction.gbapp/services/CSService';
|
||||
import { GuaribasAnswer, GuaribasQuestion, GuaribasSubject } from '../models';
|
||||
import { GBConfigService } from './../../core.gbapp/services/GBConfigService';
|
||||
const request = require('request-promise-native');
|
||||
const textract = require('textract');
|
||||
|
||||
/**
|
||||
* Result for quey on KB data.
|
||||
|
@ -183,6 +185,17 @@ export class KBService implements IGBKBService {
|
|||
return output;
|
||||
}
|
||||
|
||||
public async getDocs(instanceId: number) {
|
||||
|
||||
return await GuaribasAnswer.findAll({
|
||||
where: {
|
||||
instanceId: instanceId,
|
||||
format: '.docx'
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public async getAnswerByText(instanceId: number, text: string): Promise<any> {
|
||||
text = text.trim();
|
||||
|
||||
|
@ -540,6 +553,7 @@ export class KBService implements IGBKBService {
|
|||
}
|
||||
|
||||
public async importKbPackage(
|
||||
min: GBMinInstance,
|
||||
localPath: string,
|
||||
packageStorage: GuaribasPackage,
|
||||
instance: IGBInstance
|
||||
|
@ -558,11 +572,15 @@ export class KBService implements IGBKBService {
|
|||
|
||||
// Import remaining .md files in articles directory.
|
||||
|
||||
return await this.importRemainingArticles(localPath, instance, packageStorage.packageId);
|
||||
await this.importRemainingArticles(localPath, instance, packageStorage.packageId);
|
||||
|
||||
// Import docs files in .docx directory.
|
||||
|
||||
return await this.importDocs(min, localPath, instance, packageStorage.packageId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Import all .md files in artcles folder that has not been referenced by tabular files.
|
||||
* Import all .md files in articles folder that has not been referenced by tabular files.
|
||||
*/
|
||||
public async importRemainingArticles(localPath: string, instance: IGBInstance, packageId: number): Promise<any> {
|
||||
const files = await walkPromise(urlJoin(localPath, 'articles'));
|
||||
|
@ -588,6 +606,31 @@ export class KBService implements IGBKBService {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Import all .docx files in reading comprehension folder.
|
||||
*/
|
||||
public async importDocs(min: GBMinInstance, localPath: string, instance: IGBInstance, packageId: number): Promise<any> {
|
||||
const files = await walkPromise(urlJoin(localPath, 'docs'));
|
||||
|
||||
await CollectionUtil.asyncForEach(files, async file => {
|
||||
if (file !== null && file.name.endsWith('.docx')) {
|
||||
let content = await this.getTextFromWord(Path.join(file.root, file.name));
|
||||
|
||||
content = await min.conversationalService.translate(min, content, 'en');
|
||||
|
||||
if (content) {
|
||||
await GuaribasAnswer.create({
|
||||
instanceId: instance.instanceId,
|
||||
content: content,
|
||||
format: '.docx',
|
||||
media: file.name,
|
||||
packageId: packageId
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async importKbTabularDirectory(localPath: string, instance: IGBInstance, packageId: number): Promise<any> {
|
||||
const files = await walkPromise(localPath);
|
||||
|
||||
|
@ -650,7 +693,7 @@ export class KBService implements IGBKBService {
|
|||
const instance = await core.loadInstanceByBotId(min.botId);
|
||||
GBLog.info(`[GBDeployer] Importing: ${localPath}`);
|
||||
const p = await deployer.deployPackageToStorage(instance.instanceId, packageName);
|
||||
await this.importKbPackage(localPath, p, instance);
|
||||
await this.importKbPackage(min, localPath, p, instance);
|
||||
deployer.mountGBKBAssets(packageName, min.botId, localPath);
|
||||
|
||||
await deployer.rebuildIndex(instance, new AzureDeployerService(deployer).getKBSearchSchema(instance.searchIndex));
|
||||
|
@ -706,4 +749,30 @@ export class KBService implements IGBKBService {
|
|||
where: { instanceId: instance.instanceId, packageId: packageId }
|
||||
});
|
||||
}
|
||||
|
||||
public async readComprehension(instanceId: number, doc: string, question: string) {
|
||||
const options = {
|
||||
timeout: 60000 * 5,
|
||||
uri: `http://${process.env.GBMODELS_SERVER}/reading-comprehension`,
|
||||
form: { content: doc },
|
||||
qs: { question: question, key: process.env.GBMODELS_KEY }
|
||||
};
|
||||
|
||||
GBLog.info(`[General Bots Models]: ReadComprehension for ${question}.`);
|
||||
return await request.post(options);
|
||||
}
|
||||
|
||||
private async getTextFromWord(filename: string) {
|
||||
return new Promise<string>(async (resolve, reject) => {
|
||||
textract.fromFileWithPath(filename, { preserveLineBreaks: true }, (error, text) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(text);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue