From 5dcd36be98b98812b2790bc69754c84ffe89b627 Mon Sep 17 00:00:00 2001 From: MohamedAliBouhaouala Date: Tue, 6 May 2025 17:02:47 +0100 Subject: [PATCH] feat: set nlpEntity weights type to float --- api/src/chat/services/block.service.ts | 2 -- .../controllers/nlp-entity.controller.spec.ts | 2 +- api/src/nlp/dto/nlp-entity.dto.ts | 7 +++---- .../nlp/services/nlp-entity.service.spec.ts | 18 +----------------- api/src/nlp/services/nlp-entity.service.ts | 8 ++++---- frontend/public/locales/en/translation.json | 2 +- frontend/public/locales/fr/translation.json | 2 +- .../nlp/components/NlpEntityForm.tsx | 12 ++++++------ 8 files changed, 17 insertions(+), 36 deletions(-) diff --git a/api/src/chat/services/block.service.ts b/api/src/chat/services/block.service.ts index 930353db..d0afb2d5 100644 --- a/api/src/chat/services/block.service.ts +++ b/api/src/chat/services/block.service.ts @@ -384,8 +384,6 @@ export class BlockService extends BaseService< * * @param patterns - The NLP patterns associated with the block. * @param nlp - The parsed NLP entities from the user input. - * @param nlpCacheMap - A cache to reuse fetched entity metadata (e.g., weights and valid values). - * @param nlpPenaltyFactor - A multiplier applied to scores when the pattern match type is 'entity'. * @returns A numeric score representing how well the block matches the given NLP context. */ async calculateBlockScore( diff --git a/api/src/nlp/controllers/nlp-entity.controller.spec.ts b/api/src/nlp/controllers/nlp-entity.controller.spec.ts index 8eadf357..6a0189bb 100644 --- a/api/src/nlp/controllers/nlp-entity.controller.spec.ts +++ b/api/src/nlp/controllers/nlp-entity.controller.spec.ts @@ -311,7 +311,7 @@ describe('NlpEntityController', () => { doc: '', lookups: ['trait'], builtin: false, - weight: 4, + weight: 8, }; const originalEntity: NlpEntity | null = await nlpEntityService.findOne( buitInEntityId!, diff --git a/api/src/nlp/dto/nlp-entity.dto.ts b/api/src/nlp/dto/nlp-entity.dto.ts index 8c5c49e7..c5d0d58b 100644 --- a/api/src/nlp/dto/nlp-entity.dto.ts +++ b/api/src/nlp/dto/nlp-entity.dto.ts @@ -11,8 +11,8 @@ import { IsArray, IsBoolean, IsIn, - IsInt, IsNotEmpty, + IsNumber, IsOptional, IsString, Matches, @@ -53,11 +53,10 @@ export class NlpEntityCreateDto { @ApiPropertyOptional({ description: 'Nlp entity associated weight for next block triggering', type: Number, - minimum: 1, }) @IsOptional() - @Min(1, { message: 'Weight must be a positive integer' }) - @IsInt({ message: 'Weight must be an integer' }) + @Min(0.01, { message: 'Weight must be positive' }) + @IsNumber() weight?: number; } diff --git a/api/src/nlp/services/nlp-entity.service.spec.ts b/api/src/nlp/services/nlp-entity.service.spec.ts index 6e55f9ef..5b11e52a 100644 --- a/api/src/nlp/services/nlp-entity.service.spec.ts +++ b/api/src/nlp/services/nlp-entity.service.spec.ts @@ -170,28 +170,12 @@ describe('nlpEntityService', () => { expect(createdEntity.weight).toBe(1); }); - it('should throw an error if weight is less than 1', async () => { - const invalidWeight = 0; - - await expect( - nlpEntityService.updateWeight(createdEntity.id, invalidWeight), - ).rejects.toThrow('Weight must be a positive integer'); - }); - - it('should throw an error if weight is a decimal', async () => { - const invalidWeight = 2.5; - - await expect( - nlpEntityService.updateWeight(createdEntity.id, invalidWeight), - ).rejects.toThrow('Weight must be a positive integer'); - }); - it('should throw an error if weight is negative', async () => { const invalidWeight = -3; await expect( nlpEntityService.updateWeight(createdEntity.id, invalidWeight), - ).rejects.toThrow('Weight must be a positive integer'); + ).rejects.toThrow('Weight must be a positive number'); }); afterEach(async () => { diff --git a/api/src/nlp/services/nlp-entity.service.ts b/api/src/nlp/services/nlp-entity.service.ts index 09e233c1..907021b9 100644 --- a/api/src/nlp/services/nlp-entity.service.ts +++ b/api/src/nlp/services/nlp-entity.service.ts @@ -58,13 +58,13 @@ export class NlpEntityService extends BaseService< * This method is part of the NLP-based blocks prioritization strategy. * The weight influences the scoring of blocks when multiple blocks match a user's input. * @param id - The unique identifier of the entity to update. - * @param updatedWeight - The new weight to assign. Must be a positive integer. - * @throws Error if the weight is not a positive integer. + * @param updatedWeight - The new weight to assign. Must be a positive number. + * @throws Error if the weight is not a positive number. * @returns A promise that resolves to the updated entity. */ async updateWeight(id: string, updatedWeight: number): Promise { - if (!Number.isInteger(updatedWeight) || updatedWeight < 1) { - throw new Error('Weight must be a positive integer'); + if (updatedWeight < 0) { + throw new Error('Weight must be a positive number'); } return await this.repository.updateOne( diff --git a/frontend/public/locales/en/translation.json b/frontend/public/locales/en/translation.json index 1854a365..92273736 100644 --- a/frontend/public/locales/en/translation.json +++ b/frontend/public/locales/en/translation.json @@ -123,7 +123,7 @@ "video_error": "Video not found", "missing_fields_error": "Please make sure that all required fields are filled", "weight_required_error": "Weight is required or invalid", - "weight_positive_integer_error": "Weight must be a positive integer" + "weight_positive_number_error": "Weight must be a positive number" }, "menu": { "terms": "Terms of Use", diff --git a/frontend/public/locales/fr/translation.json b/frontend/public/locales/fr/translation.json index 8a80e491..15982b1d 100644 --- a/frontend/public/locales/fr/translation.json +++ b/frontend/public/locales/fr/translation.json @@ -122,7 +122,7 @@ "audio_error": "Audio introuvable", "video_error": "Vidéo introuvable", "missing_fields_error": "Veuillez vous assurer que tous les champs sont remplis correctement", - "weight_positive_integer_error": "Le poids doit être un nombre entier positif", + "weight_positive_number_error": "Le poids doit être un nombre positif", "weight_required_error": "Le poids est requis ou bien invalide" }, "menu": { diff --git a/frontend/src/components/nlp/components/NlpEntityForm.tsx b/frontend/src/components/nlp/components/NlpEntityForm.tsx index a5c3c270..88f8c079 100644 --- a/frontend/src/components/nlp/components/NlpEntityForm.tsx +++ b/frontend/src/components/nlp/components/NlpEntityForm.tsx @@ -141,18 +141,18 @@ export const NlpEntityVarForm: FC> = ({ valueAsNumber: true, required: t("message.weight_required_error"), min: { - value: 1, - message: t("message.weight_positive_integer_error"), + value: 0.01, + message: t("message.weight_positive_number_error"), }, validate: (value) => - value && Number.isInteger(value) && value! > 0 + value && value! > 0 ? true - : t("message.weight_positive_integer_error"), + : t("message.weight_positive_number_error"), })} type="number" inputProps={{ - min: 1, - step: 1, + min: 0.01, + step: 0.01, inputMode: "numeric", pattern: "[1-9][0-9]*", }}