From c365ff10837eccec41fec8b7fe1e74e59494d6ee Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 1 Aug 2024 14:22:12 +0700 Subject: [PATCH 1/2] feat: watch models and engines update for proper data retrieval --- .../infrastructure/controllers/chat.controller.ts | 2 +- .../extensions/extension.repository.ts | 12 +++++++++++- .../repositories/models/model.repository.ts | 14 ++++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/cortex-js/src/infrastructure/controllers/chat.controller.ts b/cortex-js/src/infrastructure/controllers/chat.controller.ts index 9c8da76a2..882ab8212 100644 --- a/cortex-js/src/infrastructure/controllers/chat.controller.ts +++ b/cortex-js/src/infrastructure/controllers/chat.controller.ts @@ -33,7 +33,7 @@ export class ChatController { @Body() createChatDto: CreateChatCompletionDto, @Res() res: Response, ) { - let { stream } = createChatDto; + const { stream } = createChatDto; this.chatService .inference(createChatDto, extractCommonHeaders(headers)) .then((response) => { diff --git a/cortex-js/src/infrastructure/repositories/extensions/extension.repository.ts b/cortex-js/src/infrastructure/repositories/extensions/extension.repository.ts index 0744876f3..464efcc44 100644 --- a/cortex-js/src/infrastructure/repositories/extensions/extension.repository.ts +++ b/cortex-js/src/infrastructure/repositories/extensions/extension.repository.ts @@ -4,7 +4,7 @@ import { Extension } from '@/domain/abstracts/extension.abstract'; import { readdir, lstat } from 'fs/promises'; import { join } from 'path'; import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; -import { existsSync } from 'fs'; +import { existsSync, mkdirSync, watch } from 'fs'; import { Engines } from '@/infrastructure/commanders/types/engine.interface'; import { OAIEngineExtension } from '@/domain/abstracts/oai.abstract'; import { HttpService } from '@nestjs/axios'; @@ -27,6 +27,16 @@ export class ExtensionRepositoryImpl implements ExtensionRepository { ) { this.loadCoreExtensions(); this.loadExternalExtensions(); + + // Watch engine folder only for now + fileService.getCortexCppEnginePath().then((path) => { + if (!existsSync(path)) mkdirSync(path); + watch(path, (eventType, filename) => { + this.extensions.clear(); + this.loadCoreExtensions(); + this.loadExternalExtensions(); + }); + }); } /** * Persist extension to the extensions map diff --git a/cortex-js/src/infrastructure/repositories/models/model.repository.ts b/cortex-js/src/infrastructure/repositories/models/model.repository.ts index ecbe9fb56..bd5e04de6 100644 --- a/cortex-js/src/infrastructure/repositories/models/model.repository.ts +++ b/cortex-js/src/infrastructure/repositories/models/model.repository.ts @@ -10,6 +10,7 @@ import { readdirSync, rmSync, writeFileSync, + watch, } from 'fs'; import { load, dump } from 'js-yaml'; import { isLocalModel, normalizeModelId } from '@/utils/normalize-model-id'; @@ -25,6 +26,12 @@ export class ModelRepositoryImpl implements ModelRepository { constructor(private readonly fileService: FileManagerService) { this.loadModels(); + fileService.getModelsPath().then((path) => { + if (!existsSync(path)) mkdirSync(path); + watch(path, (eventType, filename) => { + this.loadModels(true); + }); + }); } /** @@ -121,11 +128,14 @@ export class ModelRepositoryImpl implements ModelRepository { * This would load all the models from the models folder * @returns the list of models */ - private async loadModels(): Promise { - if (this.loaded) return Array.from(this.models.values()); + private async loadModels(forceReload: boolean = false): Promise { + if (this.loaded && !forceReload) return Array.from(this.models.values()); const modelsPath = process.env.EXTENSIONS_PATH ?? (await this.fileService.getModelsPath()); + this.models.clear(); + this.fileModel.clear(); + if (!existsSync(modelsPath)) return []; const modelFiles = readdirSync(modelsPath) From 86bb863e703ee1d43113c2035f04cca6727e8bba Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Thu, 1 Aug 2024 14:43:12 +0700 Subject: [PATCH 2/2] fix: add optional chain --- cortex-js/src/extensions/anthropic.engine.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cortex-js/src/extensions/anthropic.engine.ts b/cortex-js/src/extensions/anthropic.engine.ts index 392644cb0..6875d72b5 100644 --- a/cortex-js/src/extensions/anthropic.engine.ts +++ b/cortex-js/src/extensions/anthropic.engine.ts @@ -62,7 +62,7 @@ export default class AnthropicEngineExtension extends OAIEngineExtension { const system = data.messages.find((m: any) => m.role === 'system'); const messages = data.messages.filter((m: any) => m.role !== 'system'); return { - system: system.content, + system: system?.content ?? '', messages, ...pick(data, ['model', 'stream', 'max_tokens']), };