Skip to content

Commit

Permalink
feat: change engine syntax (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
marknguyen1302 authored Jul 17, 2024
1 parent b562ecb commit 61f6c1b
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 14 deletions.
40 changes: 37 additions & 3 deletions cortex-js/src/infrastructure/commanders/engines.command.ts
Original file line number Diff line number Diff line change
@@ -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: '<command|parameter> [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<void> {
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<void> {
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.');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<name> get',
description: 'Get an engine',
arguments: '<name>',
argsDescription: {
name: 'Engine name to get',
},
Expand All @@ -21,6 +21,15 @@ export class EnginesGetCommand extends CommandRunner {
}

async run(passedParams: string[]): Promise<void> {
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],
});
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<name> init',
description: 'Setup engine',
arguments: '<name>',
argsDescription: {
name: 'Engine name to setup',
},
Expand Down Expand Up @@ -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) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -17,6 +18,12 @@ export class EnginesListCommand extends CommandRunner {
}

async run(): Promise<void> {
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);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
3 changes: 1 addition & 2 deletions cortex-js/src/infrastructure/dtos/engines/engines.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ export class EngineDto implements Partial<Extension> {
@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;
Expand Down
5 changes: 3 additions & 2 deletions cortex-js/src/infrastructure/dtos/models/model.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ModelDto implements Partial<Model> {

@ApiProperty({
type: [String],
example: ["End"],
example: ['End'],
description:
'Defines specific tokens or phrases that signal the model to stop producing further output.',
})
Expand Down Expand Up @@ -116,7 +116,8 @@ export class ModelDto implements Partial<Model> {

@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()
Expand Down

0 comments on commit 61f6c1b

Please sign in to comment.