From e3ce245e4c81f13cf73e364aa05520add4514e16 Mon Sep 17 00:00:00 2001 From: MohamedAliBouhaouala Date: Mon, 28 Apr 2025 19:13:38 +0100 Subject: [PATCH] fix: add a fallback nlu penalty factor, handle edge cases and fix typos in log messages --- api/src/chat/services/block.service.ts | 13 ++++++++++++- .../migrations/1735836154221-v-2-2-0.migration.ts | 11 +++++++---- .../migrations/1745594957327-v-2-2-6.migration.ts | 11 +++++++---- api/src/utils/constants/nlp.ts | 9 +++++++++ 4 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 api/src/utils/constants/nlp.ts diff --git a/api/src/chat/services/block.service.ts b/api/src/chat/services/block.service.ts index 80240320..d023c241 100644 --- a/api/src/chat/services/block.service.ts +++ b/api/src/chat/services/block.service.ts @@ -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; } diff --git a/api/src/migration/migrations/1735836154221-v-2-2-0.migration.ts b/api/src/migration/migrations/1735836154221-v-2-2-0.migration.ts index c7753857..aebde14f 100644 --- a/api/src/migration/migrations/1735836154221-v-2-2-0.migration.ts +++ b/api/src/migration/migrations/1735836154221-v-2-2-0.migration.ts @@ -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, + ); } }; diff --git a/api/src/migration/migrations/1745594957327-v-2-2-6.migration.ts b/api/src/migration/migrations/1745594957327-v-2-2-6.migration.ts index a54e90bf..d348ac4d 100644 --- a/api/src/migration/migrations/1745594957327-v-2-2-6.migration.ts +++ b/api/src/migration/migrations/1745594957327-v-2-2-6.migration.ts @@ -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, + ); } }; diff --git a/api/src/utils/constants/nlp.ts b/api/src/utils/constants/nlp.ts new file mode 100644 index 00000000..fb281d5d --- /dev/null +++ b/api/src/utils/constants/nlp.ts @@ -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;