From aaeada30bd85e00ff3818362c41850ad11d3a0ac Mon Sep 17 00:00:00 2001 From: Endurance Idehen Date: Tue, 2 Jan 2024 00:46:12 -0600 Subject: [PATCH] slackBolt update --- .../chat/slack/bolt/slackAppFactory.ts | 7 ++- .../chat/slack/bolt/slackBolt.module.ts | 12 +++-- .../logging/slack-logger-proxy.service.ts | 2 +- .../chain-builder/chain-builder.service.ts | 46 +++++++++++-------- 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/src/interfaces/chat/slack/bolt/slackAppFactory.ts b/src/interfaces/chat/slack/bolt/slackAppFactory.ts index 6afde71..e2a6ce3 100644 --- a/src/interfaces/chat/slack/bolt/slackAppFactory.ts +++ b/src/interfaces/chat/slack/bolt/slackAppFactory.ts @@ -1,17 +1,16 @@ import { App } from '@slack/bolt'; import { SlackConfigService } from '../config/slackConfig.service'; -import { SlackLoggerProxy } from '../logging/slack-logger-proxy.service'; +import { SlackLoggerAdapter } from '../logging/slack-logger-proxy.service'; import { Provider } from '@nestjs/common'; export const slackServiceFactory: Provider = { provide: App, - inject: [SlackConfigService, SlackLoggerProxy], - useFactory: async (scs: SlackConfigService, logger: SlackLoggerProxy) => { + inject: [SlackConfigService, SlackLoggerAdapter], + useFactory: async (scs: SlackConfigService, logger: SlackLoggerAdapter) => { const opts = await scs.createSlackOptions(); return new App({ ...opts, logger, - // logLevel: LogLevel.DEBUG, }); }, }; diff --git a/src/interfaces/chat/slack/bolt/slackBolt.module.ts b/src/interfaces/chat/slack/bolt/slackBolt.module.ts index edc56a4..986a431 100644 --- a/src/interfaces/chat/slack/bolt/slackBolt.module.ts +++ b/src/interfaces/chat/slack/bolt/slackBolt.module.ts @@ -1,19 +1,23 @@ import { slackServiceFactory } from './slackAppFactory'; -import { Module, OnModuleInit } from '@nestjs/common'; +import { Module, OnApplicationShutdown, OnModuleInit } from '@nestjs/common'; import { App } from '@slack/bolt'; import { SlackConfigService } from '../config/slackConfig.service'; import { GcpModule } from '../../../../shared/gcp/gcp.module'; -import { SlackLoggerProxy } from '../logging/slack-logger-proxy.service'; +import { SlackLoggerAdapter } from '../logging/slack-logger-proxy.service'; @Module({ imports: [GcpModule], - providers: [slackServiceFactory, SlackConfigService, SlackLoggerProxy], + providers: [slackServiceFactory, SlackConfigService, SlackLoggerAdapter], exports: [slackServiceFactory], }) -export class SlackBoltModule implements OnModuleInit { +export class SlackBoltModule implements OnModuleInit, OnApplicationShutdown { constructor(private readonly app: App) {} onModuleInit() { this.app.start(); } + + async onApplicationShutdown() { + await this.app.stop(); + } } diff --git a/src/interfaces/chat/slack/logging/slack-logger-proxy.service.ts b/src/interfaces/chat/slack/logging/slack-logger-proxy.service.ts index e98564f..b6ff9e8 100644 --- a/src/interfaces/chat/slack/logging/slack-logger-proxy.service.ts +++ b/src/interfaces/chat/slack/logging/slack-logger-proxy.service.ts @@ -2,7 +2,7 @@ import { Injectable, Logger, LogLevel as NestLogLevel } from '@nestjs/common'; import { LogLevel as SlackLogLevel } from '@slack/bolt'; @Injectable() -export class SlackLoggerProxy extends Logger { +export class SlackLoggerAdapter extends Logger { level: NestLogLevel = 'log'; constructor(name: string) { diff --git a/src/shared/ai/langchain/chain-builder/chain-builder.service.ts b/src/shared/ai/langchain/chain-builder/chain-builder.service.ts index 0e5ca65..2c5a7b7 100644 --- a/src/shared/ai/langchain/chain-builder/chain-builder.service.ts +++ b/src/shared/ai/langchain/chain-builder/chain-builder.service.ts @@ -6,27 +6,33 @@ import { ChatPromptTemplate } from 'langchain/prompts'; @Injectable() export class ChainBuilderService { private _model: OpenAI; - private promptTemplate: ChatPromptTemplate; constructor( private readonly _openAiSecrets: OpenAiSecretsService, private readonly _logger: Logger, ) {} - async createPrompt() { - if (!this.promptTemplate) { - this._logger.log('Creating prompt...'); - const promptStructure = [ - ['system', 'You were having a conversation with a human about {topic}'], - ['human', '{text}'], - ]; - // @ts-ignore - this.promptTemplate = ChatPromptTemplate.fromMessages(promptStructure); - } - return this.promptTemplate; + /** + * Creates a chain that may be invoked later. + * @param modelName + */ + async createChain(modelName?: string) { + const model = await this._createModel(modelName); + const promptTemplate = await this._createPrompt(); + const chain = promptTemplate.pipe(model); + return chain; } - async createModel(modelName?: string) { + /** + * Creates a model instance. + * + * The model instance is generated as a singleton. Subsequent calls reuse previously created model. + * + * @param {string} [modelName] - The name of the model. If not provided, the default model name 'gpt-4-1106-preview' will be used. + * @private + * @returns {Promise} - A Promise that resolves to the created model instance. + */ + private async _createModel(modelName?: string) { if (!this._model) { this._logger.log('Creating model...'); this._model = new OpenAI({ @@ -38,10 +44,14 @@ export class ChainBuilderService { return this._model; } - async createChain(modelName?: string) { - const model = await this.createModel(modelName); - const promptTemplate = await this.createPrompt(); - const chain = promptTemplate.pipe(model); - return chain; + private async _createPrompt() { + const promptStructure = [ + ['system', 'You were having a conversation with a human about {topic}'], + ['human', '{text}'], + ]; + // @ts-ignore + const template = ChatPromptTemplate.fromMessages(promptStructure); + this._logger.log('Creating prompt...', promptStructure); + return template; } }