From 2b2d508ec7a54aff90ea7f7cf0fe19a42faebd68 Mon Sep 17 00:00:00 2001 From: Mohamed Marrouchi Date: Wed, 14 May 2025 08:32:35 +0100 Subject: [PATCH] feat: use cache --- .../helpers/llm-nlu/index.helper.ts | 7 ++++--- api/src/nlp/services/nlp-entity.service.ts | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/api/src/extensions/helpers/llm-nlu/index.helper.ts b/api/src/extensions/helpers/llm-nlu/index.helper.ts index 625f11b1..5c7c9b2f 100644 --- a/api/src/extensions/helpers/llm-nlu/index.helper.ts +++ b/api/src/extensions/helpers/llm-nlu/index.helper.ts @@ -132,9 +132,10 @@ export default class LlmNluHelper // Perform slot filling in a deterministic way since // it's currently a challenging task for the LLMs. - const entities = await this.nlpEntityService.findAndPopulate({ - lookups: { $in: ['keywords', 'pattern'] }, - }); + const entities = await this.nlpEntityService.getNlpEntitiesByLookup([ + 'keywords', + 'pattern', + ]); const slotEntities = this.runDeterministicSlotFilling(text, entities); diff --git a/api/src/nlp/services/nlp-entity.service.ts b/api/src/nlp/services/nlp-entity.service.ts index 9e6025f2..0876f3c1 100644 --- a/api/src/nlp/services/nlp-entity.service.ts +++ b/api/src/nlp/services/nlp-entity.service.ts @@ -175,4 +175,23 @@ export class NlpEntityService extends BaseService< return acc; }, new Map()); } + + /** + * Retrieves all NLP entities that declare at least one of the specified + * lookup strategies. + * + * @async + * @param lookups - One or more lookup strategies to match + * against (e.g., {@link LookupStrategy.keywords}, {@link LookupStrategy.pattern}). + * An entity is included in the result if **any** of these strategies is found + * in its own `lookups` array. + * @returns A promise that resolves to the + * collection of matching entities. + */ + async getNlpEntitiesByLookup(lookups: Lookup[]): Promise { + const entities = [...(await this.getNlpMap()).values()]; + return entities.filter((e) => { + return lookups.filter((l) => e.lookups.includes(l)).length > 0; + }); + } }