Skip to content

Commit

Permalink
Remove serve command (#896)
Browse files Browse the repository at this point in the history
  • Loading branch information
marknguyen1302 authored Jul 22, 2024
1 parent dc57195 commit 9857448
Show file tree
Hide file tree
Showing 38 changed files with 558 additions and 273 deletions.
3 changes: 0 additions & 3 deletions cortex-js/src/command.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ModelsModule } from './usecases/models/models.module';
import { DatabaseModule } from './infrastructure/database/database.module';
import { ConfigModule } from '@nestjs/config';
import { CortexModule } from './usecases/cortex/cortex.module';
import { ServeCommand } from './infrastructure/commanders/serve.command';
import { ModelsCommand } from './infrastructure/commanders/models.command';
import { ExtensionModule } from './infrastructure/repositories/extensions/extension.module';
import { HttpModule } from '@nestjs/axios';
Expand Down Expand Up @@ -45,7 +44,6 @@ import { EnginesListCommand } from './infrastructure/commanders/engines/engines-
import { EnginesGetCommand } from './infrastructure/commanders/engines/engines-get.command';
import { EnginesInitCommand } from './infrastructure/commanders/engines/engines-init.command';


@Module({
imports: [
ConfigModule.forRoot({
Expand Down Expand Up @@ -73,7 +71,6 @@ import { EnginesInitCommand } from './infrastructure/commanders/engines/engines-
providers: [
CortexCommand,
ModelsCommand,
ServeCommand,
ChatCommand,
PSCommand,
KillCommand,
Expand Down
2 changes: 1 addition & 1 deletion cortex-js/src/domain/abstracts/oai.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export abstract class OAIEngineExtension extends EngineExtension {
const additionalHeaders = _.omit(headers, [
'content-type',
'authorization',
'content-length'
'content-length',
]);
const response = await firstValueFrom(
this.httpService.post(this.apiUrl, payload, {
Expand Down
3 changes: 3 additions & 0 deletions cortex-js/src/domain/config/config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ export interface Config {
dataFolderPath: string;
cortexCppHost: string;
cortexCppPort: number;
// todo: will remove optional when all command request api server
apiServerPort?: number;
apiServerHost?: string;
}
29 changes: 29 additions & 0 deletions cortex-js/src/infrastructure/commanders/base.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { CommandRunner } from 'nest-commander';
import { Injectable } from '@nestjs/common';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import ora from 'ora';

@Injectable()
export abstract class BaseCommand extends CommandRunner {
constructor(readonly cortexUseCases: CortexUsecases) {
super();
}
protected abstract runCommand(
passedParam: string[],
options?: Record<string, any>,
): Promise<void>;

async run(
passedParam: string[],
options?: Record<string, any>,
): Promise<void> {
const checkingSpinner = ora('Checking API server online...').start();
const result = await this.cortexUseCases.isAPIServerOnline();
if (!result) {
checkingSpinner.fail('API server is offline');
process.exit(1);
}
checkingSpinner.succeed('API server is online');
await this.runCommand(passedParam, options);
}
}
13 changes: 9 additions & 4 deletions cortex-js/src/infrastructure/commanders/benchmark.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
BenchmarkConfig,
ParametersConfig,
} from './types/benchmark-config.interface';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseCommand } from './base.command';

@SubCommand({
name: 'benchmark',
Expand All @@ -14,12 +16,15 @@ import {
description:
'Benchmark and analyze the performance of a specific AI model using a variety of system resources',
})
export class BenchmarkCommand extends CommandRunner {
constructor(private readonly benchmarkUsecases: BenchmarkCliUsecases) {
super();
export class BenchmarkCommand extends BaseCommand {
constructor(
private readonly benchmarkUsecases: BenchmarkCliUsecases,
readonly cortexUsecases: CortexUsecases,
) {
super(cortexUsecases);
}

async run(
async runCommand(
passedParams: string[],
options?: Partial<BenchmarkConfig>,
): Promise<void> {
Expand Down
65 changes: 46 additions & 19 deletions cortex-js/src/infrastructure/commanders/chat.command.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import {
CommandRunner,
SubCommand,
Option,
InquirerService,
} from 'nest-commander';
import ora from 'ora';
import { existsSync } from 'fs';
import { SubCommand, Option, InquirerService } from 'nest-commander';
import { ChatCliUsecases } from './usecases/chat.cli.usecases';
import { exit } from 'node:process';
import { PSCliUsecases } from './usecases/ps.cli.usecases';
Expand All @@ -17,6 +12,14 @@ import {
TelemetrySource,
} from '@/domain/telemetry/telemetry.interface';
import { ContextService } from '../services/context/context.service';
import { BaseCommand } from './base.command';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { ModelsCliUsecases } from './usecases/models.cli.usecases';
import { Engines } from './types/engine.interface';
import { join } from 'path';
import { EnginesUsecases } from '@/usecases/engines/engines.usecase';
import { FileManagerService } from '../services/file-manager/file-manager.service';
import { isLocalModel } from '@/utils/normalize-model-id';

type ChatOptions = {
threadId?: string;
Expand All @@ -35,19 +38,26 @@ type ChatOptions = {
},
})
@SetCommandContext()
export class ChatCommand extends CommandRunner {
export class ChatCommand extends BaseCommand {
constructor(
private readonly inquirerService: InquirerService,
private readonly chatCliUsecases: ChatCliUsecases,
private readonly modelsUsecases: ModelsUsecases,
private readonly psCliUsecases: PSCliUsecases,
readonly contextService: ContextService,
private readonly telemetryUsecases: TelemetryUsecases,
readonly cortexUsecases: CortexUsecases,
readonly modelsCliUsecases: ModelsCliUsecases,
private readonly fileService: FileManagerService,
private readonly initUsecases: EnginesUsecases,
) {
super();
super(cortexUsecases);
}

async run(passedParams: string[], options: ChatOptions): Promise<void> {
async runCommand(
passedParams: string[],
options: ChatOptions,
): Promise<void> {
let modelId = passedParams[0];
// First attempt to get message from input or options
// Extract input from 1 to end of array
Expand All @@ -59,7 +69,7 @@ export class ChatCommand extends CommandRunner {
// first input might be message input
message = passedParams.length
? passedParams.join(' ')
: options.message ?? '';
: (options.message ?? '');
// If model ID is not provided, prompt user to select from running models
const models = await this.psCliUsecases.getModels();
if (models.length === 1) {
Expand All @@ -71,14 +81,20 @@ export class ChatCommand extends CommandRunner {
}
}

const existingModel = await this.modelsCliUsecases.getModel(modelId);
if (!existingModel || !isLocalModel(existingModel.files)) {
process.exit(1);
}

const engine = existingModel.engine || Engines.llamaCPP;
// Pull engine if not exist
if (
!existsSync(join(await this.fileService.getCortexCppEnginePath(), engine))
) {
await this.initUsecases.installEngine(undefined, 'latest', engine);
}

if (!message) options.attach = true;
const result = await this.chatCliUsecases.chat(
modelId,
options.threadId,
message, // Accept both message from inputs or arguments
options.attach,
false, // Do not stop cortex session or loaded model
);
this.telemetryUsecases.sendEvent(
[
{
Expand All @@ -88,7 +104,18 @@ export class ChatCommand extends CommandRunner {
],
TelemetrySource.CLI,
);
return result;
return this.cortexUsecases
.startCortex()
.then(() => this.modelsCliUsecases.startModel(modelId))
.then(() =>
this.chatCliUsecases.chat(
modelId,
options.threadId,
message, // Accept both message from inputs or arguments
options.attach,
false, // Do not stop cortex session or loaded model
),
);
}

modelInquiry = async (models: ModelStat[]) => {
Expand Down
15 changes: 10 additions & 5 deletions cortex-js/src/infrastructure/commanders/configs.command.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import { CommandRunner, SubCommand } from 'nest-commander';
import { SubCommand } from 'nest-commander';
import { SetCommandContext } from './decorators/CommandContext';
import { ContextService } from '@/infrastructure/services/context/context.service';
import { ConfigsGetCommand } from './configs/configs-get.command';
import { ConfigsListCommand } from './configs/configs-list.command';
import { ConfigsSetCommand } from './configs/configs-set.command';
import { BaseCommand } from './base.command';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';

@SubCommand({
name: 'configs',
description: 'Get cortex configurations',
subCommands: [ConfigsGetCommand, ConfigsListCommand, ConfigsSetCommand],
})
@SetCommandContext()
export class ConfigsCommand extends CommandRunner {
constructor(readonly contextService: ContextService) {
super();
export class ConfigsCommand extends BaseCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
) {
super(cortexUseCases);
}

async run(): Promise<void> {
async runCommand(): Promise<void> {
this.command?.help();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { CommandRunner, SubCommand } from 'nest-commander';
import { SetCommandContext } from '../decorators/CommandContext';
import { ContextService } from '@/infrastructure/services/context/context.service';
import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseCommand } from '../base.command';

@SubCommand({
name: 'get',
Expand All @@ -12,15 +14,16 @@ import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';
},
})
@SetCommandContext()
export class ConfigsGetCommand extends CommandRunner {
export class ConfigsGetCommand extends BaseCommand {
constructor(
private readonly configsUsecases: ConfigsUsecases,
readonly contextService: ContextService,
readonly cortexUsecases: CortexUsecases,
) {
super();
super(cortexUsecases);
}

async run(passedParams: string[]): Promise<void> {
async runCommand(passedParams: string[]): Promise<void> {
return this.configsUsecases
.getGroupConfigs(passedParams[0])
.then(console.table);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { CommandRunner, SubCommand } from 'nest-commander';
import { SubCommand } from 'nest-commander';
import { SetCommandContext } from '../decorators/CommandContext';
import { ContextService } from '@/infrastructure/services/context/context.service';
import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';
import { BaseCommand } from '../base.command';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';

@SubCommand({
name: 'list',
description: 'Get all cortex configurations',
})
@SetCommandContext()
export class ConfigsListCommand extends CommandRunner {
export class ConfigsListCommand extends BaseCommand {
constructor(
private readonly configsUsecases: ConfigsUsecases,
readonly contextService: ContextService,
readonly cortexUsecases: CortexUsecases,
) {
super();
super(cortexUsecases);
}

async run(): Promise<void> {
async runCommand(): Promise<void> {
return this.configsUsecases.getConfigs().then(console.table);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { CommandRunner, SubCommand, Option } from 'nest-commander';
import { SetCommandContext } from '../decorators/CommandContext';
import { ContextService } from '@/infrastructure/services/context/context.service';
import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseCommand } from '../base.command';

interface ConfigsSetOption {
key: string;
Expand All @@ -14,15 +16,19 @@ interface ConfigsSetOption {
description: 'Set a cortex configuration',
})
@SetCommandContext()
export class ConfigsSetCommand extends CommandRunner {
export class ConfigsSetCommand extends BaseCommand {
constructor(
private readonly configsUsecases: ConfigsUsecases,
readonly contextService: ContextService,
readonly cortexUsecases: CortexUsecases,
) {
super();
super(cortexUsecases);
}

async run(passedParams: string[], options: ConfigsSetOption): Promise<void> {
async runCommand(
passedParams: string[],
options: ConfigsSetOption,
): Promise<void> {
return this.configsUsecases
.saveConfig(options.key, options.value, options.group)
.then(() => console.log('Set configuration successfully'));
Expand Down
Loading

0 comments on commit 9857448

Please sign in to comment.