From 82ec9a11638c91d2d69422ed914b7ac8c80fb077 Mon Sep 17 00:00:00 2001 From: hexastack Date: Thu, 9 Jan 2025 12:24:15 +0100 Subject: [PATCH] fix: add default format implementation --- api/src/helper/lib/base-nlp-helper.ts | 48 +++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/api/src/helper/lib/base-nlp-helper.ts b/api/src/helper/lib/base-nlp-helper.ts index 57c2f6cd..d4b7470f 100644 --- a/api/src/helper/lib/base-nlp-helper.ts +++ b/api/src/helper/lib/base-nlp-helper.ts @@ -112,14 +112,58 @@ export default abstract class BaseNlpHelper< } /** - * Returns training dataset in NLP provider compatible format + * Returns training dataset in NLP provider compatible format. + * Can be overridden in child classes for custom formatting logic. * * @param samples - Sample to train * @param entities - All available entities * * @returns The formatted NLP training set */ - format?(samples: NlpSampleFull[], entities: NlpEntityFull[]): unknown; + async format(samples: NlpSampleFull[], entities: NlpEntityFull[]) { + const entityMap = NlpEntity.getEntityMap(entities); + const valueMap = NlpValue.getValueMap( + NlpValue.getValuesFromEntities(entities), + ); + const examples = samples + .filter((s) => s.entities.length > 0) + .map((s) => { + const intent = s.entities.find( + (e) => entityMap[e.entity].name === 'intent', + ); + if (!intent) { + throw new Error('Unable to find the `intent` nlp entity.'); + } + const sampleEntities = s.entities + .filter((e) => entityMap[e.entity].name !== 'intent') + .map((e) => { + const res = { + entity: entityMap[e.entity].name, + value: valueMap[e.value].value, + }; + if ('start' in e && 'end' in e) { + Object.assign(res, { + start: e.start, + end: e.end, + }); + } + return res; + }) + // TODO : place language at the same level as the intent + .concat({ + entity: 'language', + value: s.language.code, + }); + + return { + text: s.text, + intent: valueMap[intent.value].value, + entities: sampleEntities, + }; + }); + + return examples; + } /** * Perform training request