fix: add a fallback nlu penalty factor, handle edge cases and fix typos in log messages

This commit is contained in:
MohamedAliBouhaouala 2025-04-28 19:13:38 +01:00
parent 40c1349a3c
commit e3ce245e4c
4 changed files with 35 additions and 9 deletions

View File

@ -21,6 +21,7 @@ import { NlpEntityService } from '@/nlp/services/nlp-entity.service';
import { PluginService } from '@/plugins/plugins.service';
import { PluginType } from '@/plugins/types';
import { SettingService } from '@/setting/services/setting.service';
import { FALLBACK_DEFAULT_NLU_PENALTY_FACTOR } from '@/utils/constants/nlp';
import { BaseService } from '@/utils/generics/base-service';
import { getRandomElement } from '@/utils/helpers/safeRandom';
@ -489,8 +490,18 @@ export class BlockService extends BaseService<
const settings: Settings = await this.settingService.getSettings();
const nluPenaltyFactor =
settings.chatbot_settings.default_nlu_penalty_factor;
if (typeof nluPenaltyFactor !== 'number') {
this.logger.error(
'NLU Penalty Factor setting is missing or invalid. Using fallback...',
);
return FALLBACK_DEFAULT_NLU_PENALTY_FACTOR;
}
if (nluPenaltyFactor < 0 || nluPenaltyFactor > 1) {
this.logger.error('NLU Penalty Factor must be between 0 and 1');
this.logger.error(
'NLU Penalty Factor must be between 0 and 1. Using fallback...',
);
return FALLBACK_DEFAULT_NLU_PENALTY_FACTOR;
}
return nluPenaltyFactor;
}

View File

@ -798,9 +798,9 @@ const addDefaultStorageHelper = async ({ logger }: MigrationServices) => {
upsert: true,
},
);
logger.log('Successfuly added the default local storage helper setting');
logger.log('Successfully added the default local storage helper setting');
} catch (err) {
logger.error('Unable to add the default local storage helper setting');
logger.error('Unable to add the default local storage helper setting', err);
}
};
@ -811,9 +811,12 @@ const removeDefaultStorageHelper = async ({ logger }: MigrationServices) => {
group: 'chatbot_settings',
label: 'default_storage_helper',
});
logger.log('Successfuly removed the default local storage helper setting');
logger.log('Successfully removed the default local storage helper setting');
} catch (err) {
logger.error('Unable to remove the default local storage helper setting');
logger.error(
'Unable to remove the default local storage helper setting',
err,
);
}
};

View File

@ -37,9 +37,9 @@ const addDefaultNluPenaltyFactor = async ({ logger }: MigrationServices) => {
upsert: true,
},
);
logger.log('Successfuly added the default NLU penalty factor setting');
logger.log('Successfully added the default NLU penalty factor setting');
} catch (err) {
logger.error('Unable to add the default NLU penalty factor setting');
logger.error('Unable to add the default NLU penalty factor setting', err);
}
};
@ -50,9 +50,12 @@ const removeDefaultNluPenaltyFactor = async ({ logger }: MigrationServices) => {
group: 'chatbot_settings',
label: 'default_nlu_penalty_factor',
});
logger.log('Successfuly removed the default NLU penalty factor setting');
logger.log('Successfully removed the default NLU penalty factor setting');
} catch (err) {
logger.error('Unable to remove the default NLU penalty factor setting');
logger.error(
'Unable to remove the default NLU penalty factor setting',
err,
);
}
};

View File

@ -0,0 +1,9 @@
/*
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
export const FALLBACK_DEFAULT_NLU_PENALTY_FACTOR = 0.95;