feat: set nlpEntity weights type to float

This commit is contained in:
MohamedAliBouhaouala
2025-05-06 17:02:47 +01:00
committed by Mohamed Marrouchi
parent 003c6924f8
commit 5dcd36be98
8 changed files with 17 additions and 36 deletions

View File

@@ -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(

View File

@@ -311,7 +311,7 @@ describe('NlpEntityController', () => {
doc: '',
lookups: ['trait'],
builtin: false,
weight: 4,
weight: 8,
};
const originalEntity: NlpEntity | null = await nlpEntityService.findOne(
buitInEntityId!,

View File

@@ -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;
}

View File

@@ -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 () => {

View File

@@ -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<NlpEntity> {
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(