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).
*/
import { Injectable, OnModuleInit } from '@nestjs/common';
import {
Injectable,
OnApplicationBootstrap,
OnModuleInit,
} from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import Handlebars from 'handlebars';
@ -24,7 +28,7 @@ import { LLM_NLU_HELPER_NAME } from './settings';
@Injectable()
export default class LlmNluHelper
extends BaseNlpHelper<typeof LLM_NLU_HELPER_NAME>
implements OnModuleInit
implements OnModuleInit, OnApplicationBootstrap
{
private languageClassifierPrompt: string;
@ -50,13 +54,17 @@ export default class LlmNluHelper
@OnEvent('hook:language:*')
@OnEvent('hook:llm_nlu_helper:language_classifier_prompt_template')
async buildLanguageClassifierPrompt() {
try {
const settings = await this.getSettings();
if (settings) {
const languages = await this.languageService.findAll();
const delegate = Handlebars.compile(
settings.language_classifier_prompt_template,
);
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:llm_nlu_helper:trait_classifier_prompt_template')
async buildClassifiersPrompt() {
try {
const settings = await this.getSettings();
if (settings) {
const traitEntities = await this.nlpEntityService.findAndPopulate({
lookups: 'trait',
});
@ -75,14 +83,29 @@ export default class LlmNluHelper
entity,
}),
}));
} catch (error) {
this.logger.warn(
'Settings for LLM NLU helper not found or invalid, trait classifier prompts will not be built.',
);
}
}
async onModuleInit() {
super.onModuleInit();
// Add any additional initialization logic here if needed
}
async onApplicationBootstrap() {
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> {