feat: implement OnApplicationBootstrap for LlmNluHelper to build prompts during initialization

This commit is contained in:
medchedli 2025-05-27 18:14:44 +01:00
parent 988a56609a
commit e562739019

View File

@ -6,7 +6,11 @@
* 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). * 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).
*/ */
import { Injectable, OnModuleInit } from '@nestjs/common'; import {
Injectable,
OnApplicationBootstrap,
OnModuleInit,
} from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter'; import { OnEvent } from '@nestjs/event-emitter';
import Handlebars from 'handlebars'; import Handlebars from 'handlebars';
@ -24,7 +28,7 @@ import { LLM_NLU_HELPER_NAME } from './settings';
@Injectable() @Injectable()
export default class LlmNluHelper export default class LlmNluHelper
extends BaseNlpHelper<typeof LLM_NLU_HELPER_NAME> extends BaseNlpHelper<typeof LLM_NLU_HELPER_NAME>
implements OnModuleInit implements OnModuleInit, OnApplicationBootstrap
{ {
private languageClassifierPrompt: string; private languageClassifierPrompt: string;
@ -50,13 +54,17 @@ export default class LlmNluHelper
@OnEvent('hook:language:*') @OnEvent('hook:language:*')
@OnEvent('hook:llm_nlu_helper:language_classifier_prompt_template') @OnEvent('hook:llm_nlu_helper:language_classifier_prompt_template')
async buildLanguageClassifierPrompt() { async buildLanguageClassifierPrompt() {
const settings = await this.getSettings(); try {
if (settings) { const settings = await this.getSettings();
const languages = await this.languageService.findAll(); const languages = await this.languageService.findAll();
const delegate = Handlebars.compile( const delegate = Handlebars.compile(
settings.language_classifier_prompt_template, settings.language_classifier_prompt_template,
); );
this.languageClassifierPrompt = delegate({ languages }); this.languageClassifierPrompt = delegate({ languages });
} catch (error) {
this.logger.warn(
'Settings for LLM NLU helper not found or invalid, language classifier prompt will not be built.',
);
} }
} }
@ -64,8 +72,8 @@ export default class LlmNluHelper
@OnEvent('hook:nlpValue:*') @OnEvent('hook:nlpValue:*')
@OnEvent('hook:llm_nlu_helper:trait_classifier_prompt_template') @OnEvent('hook:llm_nlu_helper:trait_classifier_prompt_template')
async buildClassifiersPrompt() { async buildClassifiersPrompt() {
const settings = await this.getSettings(); try {
if (settings) { const settings = await this.getSettings();
const traitEntities = await this.nlpEntityService.findAndPopulate({ const traitEntities = await this.nlpEntityService.findAndPopulate({
lookups: 'trait', lookups: 'trait',
}); });
@ -75,14 +83,29 @@ export default class LlmNluHelper
entity, entity,
}), }),
})); }));
} catch (error) {
this.logger.warn(
'Settings for LLM NLU helper not found or invalid, trait classifier prompts will not be built.',
);
} }
} }
async onModuleInit() { async onModuleInit() {
super.onModuleInit(); super.onModuleInit();
// Add any additional initialization logic here if needed
}
await this.buildLanguageClassifierPrompt(); async onApplicationBootstrap() {
await this.buildClassifiersPrompt(); try {
this.logger.log('Initializing LLM NLU helper, building prompts...');
// Build prompts for language and trait classifiers
// This is done on application bootstrap to ensure that the settings are loaded
// and the prompts are built before any requests are made to the helper.
await this.buildLanguageClassifierPrompt();
await this.buildClassifiersPrompt();
} catch (error) {
this.logger.error('Unable to initialize LLM NLU helper', error);
}
} }
async predict(text: string): Promise<NLU.ParseEntities> { async predict(text: string): Promise<NLU.ParseEntities> {