Skip to content

Commit e3ce245

Browse files
author
MohamedAliBouhaouala
committed
fix: add a fallback nlu penalty factor, handle edge cases and fix typos in log messages
1 parent 40c1349 commit e3ce245

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

api/src/chat/services/block.service.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { NlpEntityService } from '@/nlp/services/nlp-entity.service';
2121
import { PluginService } from '@/plugins/plugins.service';
2222
import { PluginType } from '@/plugins/types';
2323
import { SettingService } from '@/setting/services/setting.service';
24+
import { FALLBACK_DEFAULT_NLU_PENALTY_FACTOR } from '@/utils/constants/nlp';
2425
import { BaseService } from '@/utils/generics/base-service';
2526
import { getRandomElement } from '@/utils/helpers/safeRandom';
2627

@@ -489,8 +490,18 @@ export class BlockService extends BaseService<
489490
const settings: Settings = await this.settingService.getSettings();
490491
const nluPenaltyFactor =
491492
settings.chatbot_settings.default_nlu_penalty_factor;
493+
494+
if (typeof nluPenaltyFactor !== 'number') {
495+
this.logger.error(
496+
'NLU Penalty Factor setting is missing or invalid. Using fallback...',
497+
);
498+
return FALLBACK_DEFAULT_NLU_PENALTY_FACTOR;
499+
}
492500
if (nluPenaltyFactor < 0 || nluPenaltyFactor > 1) {
493-
this.logger.error('NLU Penalty Factor must be between 0 and 1');
501+
this.logger.error(
502+
'NLU Penalty Factor must be between 0 and 1. Using fallback...',
503+
);
504+
return FALLBACK_DEFAULT_NLU_PENALTY_FACTOR;
494505
}
495506
return nluPenaltyFactor;
496507
}

api/src/migration/migrations/1735836154221-v-2-2-0.migration.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -798,9 +798,9 @@ const addDefaultStorageHelper = async ({ logger }: MigrationServices) => {
798798
upsert: true,
799799
},
800800
);
801-
logger.log('Successfuly added the default local storage helper setting');
801+
logger.log('Successfully added the default local storage helper setting');
802802
} catch (err) {
803-
logger.error('Unable to add the default local storage helper setting');
803+
logger.error('Unable to add the default local storage helper setting', err);
804804
}
805805
};
806806

@@ -811,9 +811,12 @@ const removeDefaultStorageHelper = async ({ logger }: MigrationServices) => {
811811
group: 'chatbot_settings',
812812
label: 'default_storage_helper',
813813
});
814-
logger.log('Successfuly removed the default local storage helper setting');
814+
logger.log('Successfully removed the default local storage helper setting');
815815
} catch (err) {
816-
logger.error('Unable to remove the default local storage helper setting');
816+
logger.error(
817+
'Unable to remove the default local storage helper setting',
818+
err,
819+
);
817820
}
818821
};
819822

api/src/migration/migrations/1745594957327-v-2-2-6.migration.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ const addDefaultNluPenaltyFactor = async ({ logger }: MigrationServices) => {
3737
upsert: true,
3838
},
3939
);
40-
logger.log('Successfuly added the default NLU penalty factor setting');
40+
logger.log('Successfully added the default NLU penalty factor setting');
4141
} catch (err) {
42-
logger.error('Unable to add the default NLU penalty factor setting');
42+
logger.error('Unable to add the default NLU penalty factor setting', err);
4343
}
4444
};
4545

@@ -50,9 +50,12 @@ const removeDefaultNluPenaltyFactor = async ({ logger }: MigrationServices) => {
5050
group: 'chatbot_settings',
5151
label: 'default_nlu_penalty_factor',
5252
});
53-
logger.log('Successfuly removed the default NLU penalty factor setting');
53+
logger.log('Successfully removed the default NLU penalty factor setting');
5454
} catch (err) {
55-
logger.error('Unable to remove the default NLU penalty factor setting');
55+
logger.error(
56+
'Unable to remove the default NLU penalty factor setting',
57+
err,
58+
);
5659
}
5760
};
5861

api/src/utils/constants/nlp.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright © 2025 Hexastack. All rights reserved.
3+
*
4+
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
5+
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
6+
* 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).
7+
*/
8+
9+
export const FALLBACK_DEFAULT_NLU_PENALTY_FACTOR = 0.95;

0 commit comments

Comments
 (0)