diff --git a/cortex-js/src/infrastructure/commanders/engines.command.ts b/cortex-js/src/infrastructure/commanders/engines.command.ts index 0209d309a..9b0744562 100644 --- a/cortex-js/src/infrastructure/commanders/engines.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines.command.ts @@ -1,22 +1,56 @@ +import { invert } from 'lodash'; import { CommandRunner, SubCommand } from 'nest-commander'; import { SetCommandContext } from './decorators/CommandContext'; import { ContextService } from '@/infrastructure/services/context/context.service'; import { EnginesListCommand } from './engines/engines-list.command'; import { EnginesGetCommand } from './engines/engines-get.command'; import { EnginesInitCommand } from './engines/engines-init.command'; +import { ModuleRef } from '@nestjs/core'; +import { EngineNamesMap } from './types/engine.interface'; @SubCommand({ name: 'engines', subCommands: [EnginesListCommand, EnginesGetCommand, EnginesInitCommand], description: 'Get cortex engines', + arguments: ' [subcommand]', }) @SetCommandContext() export class EnginesCommand extends CommandRunner { - constructor(readonly contextService: ContextService) { + commandMap: { [key: string]: any } = { + list: EnginesListCommand, + get: EnginesGetCommand, + init: EnginesInitCommand, + }; + + constructor( + readonly contextService: ContextService, + private readonly moduleRef: ModuleRef, + ) { super(); } + async run(passedParam: string[]): Promise { + const [parameter, command] = passedParam; + if (command !== 'list' && !parameter) { + console.error('Engine name is required.'); + return; + } + + // Handle the commands accordingly + const commandClass = this.commandMap[command as string]; + if (!commandClass) { + this.command?.help(); + return; + } + const engine = invert(EngineNamesMap)[parameter] || parameter; + await this.runCommand(commandClass, [engine]); + } - async run(): Promise { - this.command?.help(); + private async runCommand(commandClass: any, params: string[] = []) { + const commandInstance = this.moduleRef.get(commandClass, { strict: false }); + if (commandInstance) { + await commandInstance.run(params); + } else { + console.error('Command not found.'); + } } } diff --git a/cortex-js/src/infrastructure/commanders/engines/engines-get.command.ts b/cortex-js/src/infrastructure/commanders/engines/engines-get.command.ts index 8eefc6f44..d72766dbe 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-get.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-get.command.ts @@ -2,11 +2,11 @@ import { CommandRunner, SubCommand } from 'nest-commander'; import { SetCommandContext } from '../decorators/CommandContext'; import { ContextService } from '@/infrastructure/services/context/context.service'; import { EnginesUsecases } from '@/usecases/engines/engines.usecase'; +import { EngineNamesMap, Engines } from '../types/engine.interface'; @SubCommand({ - name: 'get', + name: ' get', description: 'Get an engine', - arguments: '', argsDescription: { name: 'Engine name to get', }, @@ -21,6 +21,15 @@ export class EnginesGetCommand extends CommandRunner { } async run(passedParams: string[]): Promise { - return this.engineUsecases.getEngine(passedParams[0]).then(console.table); + return this.engineUsecases.getEngine(passedParams[0]).then((engine) => { + if (!engine) { + console.error('Engine not found.'); + } else { + console.table({ + ...engine, + name: EngineNamesMap[engine.name as Engines], + }); + } + }); } } diff --git a/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts b/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts index 2bbfe0a9f..7ed136c9e 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts @@ -7,9 +7,8 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; @SubCommand({ - name: 'init', + name: ' init', description: 'Setup engine', - arguments: '', argsDescription: { name: 'Engine name to setup', }, @@ -49,7 +48,7 @@ export class EnginesInitCommand extends CommandRunner { params, engine.includes('@') ? engine.split('@')[1] : 'latest', engine, - true + true, ) .then(() => console.log('Engine installed successfully!')) .catch((e) => diff --git a/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts b/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts index 4e931bfb7..fa49aa302 100644 --- a/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts +++ b/cortex-js/src/infrastructure/commanders/engines/engines-list.command.ts @@ -2,6 +2,7 @@ import { CommandRunner, SubCommand } from 'nest-commander'; import { SetCommandContext } from '../decorators/CommandContext'; import { ContextService } from '@/infrastructure/services/context/context.service'; import { EnginesUsecases } from '@/usecases/engines/engines.usecase'; +import { EngineNamesMap, Engines } from '../types/engine.interface'; @SubCommand({ name: 'list', @@ -17,6 +18,12 @@ export class EnginesListCommand extends CommandRunner { } async run(): Promise { - return this.enginesUsecases.getEngines().then(console.table); + return this.enginesUsecases.getEngines().then((engines) => { + const enginesTable = engines.map((engine) => ({ + ...engine, + name: EngineNamesMap[engine.name as Engines], + })); + console.table(enginesTable); + }); } } diff --git a/cortex-js/src/infrastructure/commanders/types/engine.interface.ts b/cortex-js/src/infrastructure/commanders/types/engine.interface.ts index 93101ad11..c66753523 100644 --- a/cortex-js/src/infrastructure/commanders/types/engine.interface.ts +++ b/cortex-js/src/infrastructure/commanders/types/engine.interface.ts @@ -9,3 +9,11 @@ export enum Engines { openai = 'openai', anthropic = 'anthropic', } + +export const EngineNamesMap: { + [key in string]: string; +} = { + [Engines.llamaCPP]: 'llamacpp', + [Engines.onnx]: 'onnx', + [Engines.tensorrtLLM]: 'tensorrt-llm', +}; diff --git a/cortex-js/src/infrastructure/dtos/engines/engines.dto.ts b/cortex-js/src/infrastructure/dtos/engines/engines.dto.ts index a86c2e309..b043b6c98 100644 --- a/cortex-js/src/infrastructure/dtos/engines/engines.dto.ts +++ b/cortex-js/src/infrastructure/dtos/engines/engines.dto.ts @@ -6,8 +6,7 @@ export class EngineDto implements Partial { @ApiProperty({ type: String, example: 'cortex.llamacpp', - description: - 'The name of the engine that you want to retrieve.', + description: 'The name of the engine that you want to retrieve.', }) @IsString() name: string; diff --git a/cortex-js/src/infrastructure/dtos/models/model.dto.ts b/cortex-js/src/infrastructure/dtos/models/model.dto.ts index 163ac8527..4054a8b14 100644 --- a/cortex-js/src/infrastructure/dtos/models/model.dto.ts +++ b/cortex-js/src/infrastructure/dtos/models/model.dto.ts @@ -28,7 +28,7 @@ export class ModelDto implements Partial { @ApiProperty({ type: [String], - example: ["End"], + example: ['End'], description: 'Defines specific tokens or phrases that signal the model to stop producing further output.', }) @@ -116,7 +116,8 @@ export class ModelDto implements Partial { @ApiProperty({ description: 'The prompt to use for internal configuration', - example: "You are an assistant with expert knowledge in {subject}. Please provide a detailed and accurate response to the following query: {query}. Ensure that your response is clear, concise, and informative." + example: + 'You are an assistant with expert knowledge in {subject}. Please provide a detailed and accurate response to the following query: {query}. Ensure that your response is clear, concise, and informative.', }) @IsOptional() @IsString()