Skip to content

Commit

Permalink
fix(core.gbapp): Start dialog done and log improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Dec 6, 2020
1 parent d04a480 commit 1ff2429
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 24 deletions.
11 changes: 7 additions & 4 deletions packages/core.gbapp/services/GBConversationalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ export class GBConversationalService {
try {
const saved = step.context.activity.text;
step.context.activity.text = text;
nlp = await model.recognize(step.context);
nlp = await model.recognize(step.context,{},{},{IncludeAllIntents:false, IncludeInstanceData: false, includeAPIResults: true});
step.context.activity.text = saved;
} catch (error) {
// tslint:disable:no-unsafe-any
Expand All @@ -498,12 +498,15 @@ export class GBConversationalService {
// tslint:enable:no-unsafe-any
}

const minBoot = GBServer.globals.minBoot as any;
let nlpActive = false;
let score = 0;
const instanceScore = min.core.getParam(min.instance, 'NLP Score',
min.instance.nlpScore ? min.instance.nlpScore : minBoot.instance.nlpScore);

Object.keys(nlp.intents).forEach(name => {
score = nlp.intents[name].score;
if (score > min.instance.nlpScore) {
if (score > instanceScore) {
nlpActive = true;
}
});
Expand All @@ -518,7 +521,7 @@ export class GBConversationalService {
}

GBLog.info(
`NLP called: ${intent}, entities: ${nlp.entities.length}, score: ${score} > required (nlpScore): ${min.instance.nlpScore}`
`NLP called: ${intent}, entities: ${nlp.entities.length}, score: ${score} > required (nlpScore): ${instanceScore}`
);

try {
Expand Down Expand Up @@ -548,7 +551,7 @@ export class GBConversationalService {
}

GBLog.info(
`NLP NOT called: score: ${score} > required (nlpScore): ${min.instance.nlpScore}`
`NLP NOT called: score: ${score} > required (nlpScore): ${instanceScore}`
);

return false;
Expand Down
2 changes: 1 addition & 1 deletion packages/core.gbapp/services/GBCoreService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ STORAGE_SYNC=true
return value ? value : defaultValue;
}
if (typeof defaultValue === 'number') {
return new Number(value ? defaultValue : defaultValue ? defaultValue : 0);
return new Number(value ? value : defaultValue ? defaultValue : 0);
}

if (instance['dataValues'] && !value) {
Expand Down
16 changes: 15 additions & 1 deletion packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ export class GBMinService {
// Get loaded user state
const step = await min.dialogs.createContext(context);
step.context.activity.locale = 'pt-BR';
let firstTime = false;

try {
const user = await min.userProfile.get(context, {});
Expand All @@ -622,6 +623,7 @@ export class GBMinService {

const sec = new SecService();
if (!user.loaded) {
firstTime = true;
await min.conversationalService.sendEvent(min, step, 'loadInstance', {
instanceId: instance.instanceId,
botId: instance.botId,
Expand Down Expand Up @@ -668,7 +670,19 @@ export class GBMinService {
await CollectionUtil.asyncForEach(appPackages, async e => {
await e.onNewSession(min, step);
});
await step.beginDialog('/');

const startDialog = min.core.getParam(min.instance, 'Start Dialog', null);
if (startDialog)
{
GBLog.info(`Auto start dialog is now being called: ${startDialog}...`);
await GBVMService.callVM(context.activity.text, min, step, this.deployer);
}
else
{
await step.beginDialog('/');

}

} else {
GBLog.info(`Member added to conversation: ${member.name}`);
}
Expand Down
17 changes: 13 additions & 4 deletions packages/core.gbapp/services/GBVMService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ export class GBVMService extends GBService {
const isIntentYes = (locale, utterance) => {
return utterance.toLowerCase().match(Messages[locale].affirmative_sentences);
}

let result = step.result;
if (step.activeDialog.state.options['boolean']) {

Expand All @@ -436,7 +436,7 @@ export class GBVMService extends GBService {

}
else if (step.activeDialog.state.options['email']) {
// e@e.com
// e@e.com
}
else if (step.activeDialog.state.options['number']) {
// MAX and MIN.
Expand All @@ -456,7 +456,7 @@ export class GBVMService extends GBService {
else if (step.activeDialog.state.options['zipcode']) {
// 12333-222
}
else if (step.activeDialog.state.options['menu']){
else if (step.activeDialog.state.options['menu']) {
// ['drums', 'guitar', 'bass']; kpmSearch
}

Expand Down Expand Up @@ -487,6 +487,15 @@ export class GBVMService extends GBService {

const mainMethod = text.toLowerCase();
sandbox[mainMethod].bind(sandbox);
return await sandbox[mainMethod](step);

let ret = null;
try {
ret = await sandbox[mainMethod](step);

} catch (error) {
GBLog.error(`BASIC ERROR: ${error.message} ${error.stack}`);
}

return ret;
}
}
9 changes: 5 additions & 4 deletions packages/kb.gbapp/dialogs/AskDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ export class AskDialog extends IGBDialog {

// Searches KB for the first time.

const searchScore = min.instance.searchScore ? min.instance.searchScore : minBoot.instance.searchScore;
const searchScore = min.core.getParam(min.instance, 'Search Score',
min.instance.searchScore ? min.instance.searchScore : minBoot.instance.searchScore);

user.lastQuestion = text;
await min.userProfile.set(step.context, user);

Expand All @@ -192,15 +194,14 @@ export class AskDialog extends IGBDialog {
answer = resultsA.answer;

// If this search was restricted to some subjects...
} else if (user.subjects) {
} else if (user.subjects && user.subjects.length > 0) {
// ...second time running Search, now with no filter.

const resultsB = await service.ask(min.instance, text, searchScore, undefined);

// If there is some result, answer immediately.

if (resultsB !== undefined && resultsB.answer !== undefined)
{
if (resultsB !== undefined && resultsB.answer !== undefined) {
// Saves some context info.

const user2 = await min.userProfile.get(step.context, {});
Expand Down
63 changes: 53 additions & 10 deletions packages/kb.gbapp/services/KBService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ export class KBService implements IGBKBService {
): Promise<KBServiceSearchResults> {
// Builds search query.



query = query.toLowerCase();
query = query.replace('?', ' ');
query = query.replace('!', ' ');
Expand All @@ -192,6 +194,8 @@ export class KBService implements IGBKBService {
}
}

let notSearched = true;

// tslint:disable:no-unsafe-any
if (instance.searchKey !== null && GBConfigService.get('STORAGE_DIALECT') === 'mssql') {
const client = new SearchService(instance.searchHost.split('.')[0], instance.searchKey);
Expand All @@ -205,22 +209,61 @@ export class KBService implements IGBKBService {

const values = results.result.value;

if (values && values.length > 0 && values[0]['@search.score'] >= searchScore) {
const value = await this.getAnswerById(instance.instanceId, values[0].answerId);
if (value !== null) {
return { answer: value, questionId: values[0].questionId };
let returnedScore = 0;

// Searches via Search (Azure Search).

if (values && values.length > 0) {
returnedScore = values[0]['@search.score'];
if (returnedScore >= searchScore) {
const value = await this.getAnswerById(instance.instanceId, values[0].answerId);
if (value !== null) {
GBLog.info(
`SEARCH WILL BE USED with score: ${returnedScore} > required (searchScore): ${searchScore}`
);
notSearched = false;
return { answer: value, questionId: values[0].questionId };
} else {
GBLog.info(
`SEARCH WILL NOT be used as answerId ${values[0].answerId} was not found in database,
returnedScore: ${returnedScore} < required (searchScore): ${searchScore}`
);

return { answer: undefined, questionId: 0 };
}
} else {
GBLog.info(
`SEARCH called but returned LOW level score,
returnedScore: ${returnedScore} < required (searchScore): ${searchScore}`
);

return { answer: undefined, questionId: 0 };
}
}
} else {
const data = await this.getAnswerByText(instance.instanceId, query);
if (data) {
return { answer: data.answer, questionId: data.question.questionId };
} else {
GBLog.info(
`SEARCH called but NO answer could be found (zero results).`
);
return { answer: undefined, questionId: 0 };
}
}
}

// DISABLED: Searches via Database "WHERE" command.

// if (notSearched) {
// const data = await this.getAnswerByText(instance.instanceId, query);
// if (data) {
// GBLog.info(
// `SEARCH called.`
// );
// return { answer: data.answer, questionId: data.question.questionId };
// } else {
// GBLog.info(`SEARCH NOT called getAnswerByText not found answers in database.`);
// return { answer: undefined, questionId: 0 };
// }
// }

// TODO: Add more sources....

}

public async getSubjectItems(instanceId: number, parentId: number): Promise<GuaribasSubject[]> {
Expand Down

0 comments on commit 1ff2429

Please sign in to comment.