Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: reduce dependencies load
Browse files Browse the repository at this point in the history
louis-menlo committed Jul 26, 2024
1 parent cd59c7a commit e64d481
Showing 20 changed files with 104 additions and 94 deletions.
2 changes: 2 additions & 0 deletions cortex-js/src/command.module.ts
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ 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';
import { EnginesSetCommand } from './infrastructure/commanders/engines/engines-set.command';
import { CortexClientModule } from './infrastructure/commanders/services/cortex.client.module';

@Module({
imports: [
@@ -41,6 +42,7 @@ import { EnginesSetCommand } from './infrastructure/commanders/engines/engines-s
FileManagerModule,
TelemetryModule,
ContextModule,
CortexClientModule,
],
providers: [
CortexCommand,
2 changes: 1 addition & 1 deletion cortex-js/src/extensions/extensions.module.ts
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import OpenAIEngineExtension from './openai.engine';
import { HttpModule, HttpService } from '@nestjs/axios';
import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';
import { ConfigsModule } from '@/usecases/configs/configs.module';
import { EventEmitter2, EventEmitterModule } from '@nestjs/event-emitter';
import { EventEmitter2 } from '@nestjs/event-emitter';
import AnthropicEngineExtension from './anthropic.engine';

const provider = {
30 changes: 0 additions & 30 deletions cortex-js/src/infrastructure/commanders/base.subcommand.ts

This file was deleted.

5 changes: 3 additions & 2 deletions cortex-js/src/infrastructure/commanders/benchmark.command.ts
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ import { BenchmarkHardware } from '@/domain/telemetry/telemetry.interface';
import { defaultBenchmarkConfiguration } from '../constants/benchmark';
import { inspect } from 'util';
import { Cortex } from '@cortexso/cortex.js';
import { BaseSubCommand } from './base.subcommand';
import { CortexClient } from './services/cortex.client';

@SubCommand({
name: 'benchmark',
@@ -27,11 +27,12 @@ import { BaseSubCommand } from './base.subcommand';
description:
'Benchmark and analyze the performance of a specific AI model using a variety of system resources',
})
export class BenchmarkCommand extends BaseSubCommand {
export class BenchmarkCommand extends BaseCommand {
constructor(
private readonly cortexUsecases: CortexUsecases,
private readonly fileService: FileManagerService,
private readonly telemetryUsecases: TelemetryUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUsecases);
}
7 changes: 4 additions & 3 deletions cortex-js/src/infrastructure/commanders/chat.command.ts
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ import { isRemoteEngine } from '@/utils/normalize-model-id';
import { Cortex } from '@cortexso/cortex.js';
import { ChatClient } from './services/chat-client';
import { downloadModelProgress } from '@/utils/pull-model';
import { BaseSubCommand } from './base.subcommand';
import { CortexClient } from './services/cortex.client';

type ChatOptions = {
threadId?: string;
@@ -37,7 +37,7 @@ type ChatOptions = {
},
})
@SetCommandContext()
export class ChatCommand extends BaseSubCommand {
export class ChatCommand extends BaseCommand {
chatClient: ChatClient;

constructor(
@@ -46,6 +46,7 @@ export class ChatCommand extends BaseSubCommand {
private readonly fileService: FileManagerService,
protected readonly cortexUsecases: CortexUsecases,
protected readonly contextService: ContextService,
protected readonly cortex: CortexClient,
) {
super(cortexUsecases);
this.chatClient = new ChatClient(this.cortex);
@@ -66,7 +67,7 @@ export class ChatCommand extends BaseSubCommand {
// 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 { data: models } = await this.cortex.models.list();
if (models.length === 1) {
5 changes: 3 additions & 2 deletions cortex-js/src/infrastructure/commanders/embeddings.command.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseCommand } from './base.command';
import { Cortex } from '@cortexso/cortex.js';
import ora from 'ora';
import { BaseSubCommand } from './base.subcommand';
import { CortexClient } from './services/cortex.client';

interface EmbeddingCommandOptions {
encoding_format?: string;
@@ -21,9 +21,10 @@ interface EmbeddingCommandOptions {
'Model to use for embedding. If not provided, it will prompt to select from running models.',
},
})
export class EmbeddingCommand extends BaseSubCommand {
export class EmbeddingCommand extends BaseCommand {
constructor(
private readonly inquirerService: InquirerService,
private readonly cortex: CortexClient,
readonly cortexUsecases: CortexUsecases,
) {
super(cortexUsecases);
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { ContextService } from '@/infrastructure/services/context/context.servic
import { EngineNamesMap, Engines } from '../types/engine.interface';
import { BaseCommand } from '../base.command';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

@SubCommand({
name: '<name> get',
@@ -14,10 +14,11 @@ import { BaseSubCommand } from '../base.subcommand';
},
})
@SetCommandContext()
export class EnginesGetCommand extends BaseSubCommand {
export class EnginesGetCommand extends BaseCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUsecases: CortexUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUsecases);
}
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import { FileManagerService } from '@/infrastructure/services/file-manager/file-
import { BaseCommand } from '../base.command';
import { defaultInstallationOptions } from '@/utils/init';
import { Presets, SingleBar } from 'cli-progress';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

@SubCommand({
name: '<name> init',
@@ -18,11 +18,12 @@ import { BaseSubCommand } from '../base.subcommand';
},
})
@SetCommandContext()
export class EnginesInitCommand extends BaseSubCommand {
export class EnginesInitCommand extends BaseCommand {
constructor(
private readonly cortexUsecases: CortexUsecases,
private readonly fileManagerService: FileManagerService,
readonly contextService: ContextService,
private readonly cortex: CortexClient,
) {
super(cortexUsecases);
}
@@ -47,29 +48,28 @@ export class EnginesInitCommand extends BaseSubCommand {
await this.cortexUsecases.stopCortex();
}
console.log(`Installing engine ${engine}...`);
await this.cortex.engines
.init(engine, params)
const response = await this.cortex.events.downloadEvent()

const progressBar = new SingleBar({}, Presets.shades_classic);
progressBar.start(100, 0);

for await (const stream of response) {
if (stream.length) {
const data = stream[0] as any;
if (data.status === 'downloaded') break;
let totalBytes = 0;
let totalTransferred = 0;
data.children.forEach((child: any) => {
totalBytes += child.size.total;
totalTransferred += child.size.transferred;
});
progressBar.update(Math.floor((totalTransferred / totalBytes) * 100));
}
await this.cortex.engines.init(engine, params);
const response = await this.cortex.events.downloadEvent();

const progressBar = new SingleBar({}, Presets.shades_classic);
progressBar.start(100, 0);

for await (const stream of response) {
if (stream.length) {
const data = stream[0] as any;
if (data.status === 'downloaded') break;
let totalBytes = 0;
let totalTransferred = 0;
data.children.forEach((child: any) => {
totalBytes += child.size.total;
totalTransferred += child.size.transferred;
});
progressBar.update(Math.floor((totalTransferred / totalBytes) * 100));
}
progressBar.stop();
console.log('Engine installed successfully');
process.exit(0);
}
progressBar.stop();
console.log('Engine installed successfully');
process.exit(0);
}

@Option({
Original file line number Diff line number Diff line change
@@ -4,17 +4,18 @@ import { ContextService } from '@/infrastructure/services/context/context.servic
import { EngineNamesMap } from '../types/engine.interface';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseCommand } from '../base.command';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

@SubCommand({
name: 'list',
description: 'Get all cortex engines',
})
@SetCommandContext()
export class EnginesListCommand extends BaseSubCommand {
export class EnginesListCommand extends BaseCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUseCases);
}
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import { SubCommand } from 'nest-commander';
import { SetCommandContext } from '../decorators/CommandContext';
import { BaseCommand } from '../base.command';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

@SubCommand({
name: '<name> set <config> <value>',
@@ -12,8 +12,11 @@ import { BaseSubCommand } from '../base.subcommand';
},
})
@SetCommandContext()
export class EnginesSetCommand extends BaseSubCommand {
constructor(readonly cortexUsecases: CortexUsecases) {
export class EnginesSetCommand extends BaseCommand {
constructor(
readonly cortexUsecases: CortexUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUsecases);
}

Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { SubCommand } from 'nest-commander';
import { exit } from 'node:process';
import { SetCommandContext } from '../decorators/CommandContext';
import { ContextService } from '@/infrastructure/services/context/context.service';
import { BaseCommand } from '../base.command';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

@SubCommand({
name: 'get',
@@ -15,10 +14,11 @@ import { BaseSubCommand } from '../base.subcommand';
},
})
@SetCommandContext()
export class ModelGetCommand extends BaseSubCommand {
export class ModelGetCommand extends BaseCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUseCases);
}
Original file line number Diff line number Diff line change
@@ -3,17 +3,18 @@ import { SetCommandContext } from '../decorators/CommandContext';
import { ContextService } from '@/infrastructure/services/context/context.service';
import { BaseCommand } from '../base.command';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

interface ModelListOptions {
format: 'table' | 'json';
}
@SubCommand({ name: 'list', description: 'List all models locally.' })
@SetCommandContext()
export class ModelListCommand extends BaseSubCommand {
export class ModelListCommand extends BaseCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUseCases);
}
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ import { Engines } from '../types/engine.interface';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseCommand } from '../base.command';
import { downloadModelProgress } from '@/utils/pull-model';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

@SubCommand({
name: 'pull',
@@ -27,11 +27,12 @@ import { BaseSubCommand } from '../base.subcommand';
'Download a model from a registry. Working with HuggingFace repositories. For available models, please visit https://huggingface.co/cortexso',
})
@SetCommandContext()
export class ModelPullCommand extends BaseSubCommand {
export class ModelPullCommand extends BaseCommand {
constructor(
private readonly fileService: FileManagerService,
readonly contextService: ContextService,
private readonly telemetryUsecases: TelemetryUsecases,
private readonly cortex: CortexClient,
readonly contextService: ContextService,
readonly cortexUsecases: CortexUsecases,
) {
super(cortexUsecases);
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { SetCommandContext } from '../decorators/CommandContext';
import { ContextService } from '@/infrastructure/services/context/context.service';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseCommand } from '../base.command';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

@SubCommand({
name: 'remove',
@@ -15,10 +15,11 @@ import { BaseSubCommand } from '../base.subcommand';
},
})
@SetCommandContext()
export class ModelRemoveCommand extends BaseSubCommand {
export class ModelRemoveCommand extends BaseCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUseCases);
}
Original file line number Diff line number Diff line change
@@ -4,20 +4,15 @@ import { exit } from 'node:process';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { SetCommandContext } from '../decorators/CommandContext';
import { ContextService } from '@/infrastructure/services/context/context.service';
import {
createReadStream,
existsSync,
statSync,
watchFile,
} from 'node:fs';
import { createReadStream, existsSync, statSync, watchFile } from 'node:fs';
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
import { join } from 'node:path';
import { Engines } from '../types/engine.interface';
import { checkModelCompatibility } from '@/utils/model-check';
import { BaseCommand } from '../base.command';
import { isRemoteEngine } from '@/utils/normalize-model-id';
import { downloadModelProgress } from '@/utils/pull-model';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

type ModelStartOptions = {
attach: boolean;
@@ -33,12 +28,13 @@ type ModelStartOptions = {
},
})
@SetCommandContext()
export class ModelStartCommand extends BaseSubCommand {
export class ModelStartCommand extends BaseCommand {
constructor(
private readonly inquirerService: InquirerService,
readonly cortexUsecases: CortexUsecases,
private readonly fileService: FileManagerService,
readonly contextService: ContextService,
private readonly cortex: CortexClient,
) {
super(cortexUsecases);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { SubCommand } from 'nest-commander';
import { exit } from 'node:process';
import { SetCommandContext } from '../decorators/CommandContext';
import { ContextService } from '@/infrastructure/services/context/context.service';
import { BaseCommand } from '../base.command';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import ora from 'ora';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

@SubCommand({
name: 'stop',
@@ -16,10 +15,11 @@ import { BaseSubCommand } from '../base.subcommand';
},
})
@SetCommandContext()
export class ModelStopCommand extends BaseSubCommand {
export class ModelStopCommand extends BaseCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUseCases);
}
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import { UpdateModelDto } from '@/infrastructure/dtos/models/update-model.dto';
import { ContextService } from '@/infrastructure/services/context/context.service';
import { BaseCommand } from '../base.command';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseSubCommand } from '../base.subcommand';
import { CortexClient } from '../services/cortex.client';

type UpdateOptions = {
model?: string;
@@ -21,10 +21,11 @@ type UpdateOptions = {
},
})
@SetCommandContext()
export class ModelUpdateCommand extends BaseSubCommand {
export class ModelUpdateCommand extends BaseCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
private readonly cortex: CortexClient,
) {
super(cortexUseCases);
}
5 changes: 3 additions & 2 deletions cortex-js/src/infrastructure/commanders/run.command.ts
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ import { BaseCommand } from './base.command';
import { isRemoteEngine } from '@/utils/normalize-model-id';
import { ChatClient } from './services/chat-client';
import { downloadModelProgress } from '@/utils/pull-model';
import { BaseSubCommand } from './base.subcommand';
import { CortexClient } from './services/cortex.client';

type RunOptions = {
threadId?: string;
@@ -28,12 +28,13 @@ type RunOptions = {
},
description: 'Shortcut to start a model and chat',
})
export class RunCommand extends BaseSubCommand {
export class RunCommand extends BaseCommand {
chatClient: ChatClient;
constructor(
protected readonly cortexUsecases: CortexUsecases,
private readonly inquirerService: InquirerService,
private readonly fileService: FileManagerService,
private readonly cortex: CortexClient,
) {
super(cortexUsecases);

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { CortexClient } from './cortex.client';

@Module({
imports: [],
providers: [CortexClient],
exports: [CortexClient],
})
export class CortexClientModule {}
20 changes: 20 additions & 0 deletions cortex-js/src/infrastructure/commanders/services/cortex.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
cortexNamespace,
cortexServerAPI,
} from '@/infrastructure/constants/cortex';
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
import Cortex from '@cortexso/cortex.js';

export class CortexClient extends Cortex {
serverConfigs: { host: string; port: number };

constructor() {
const fileManagerService: FileManagerService = new FileManagerService();
const configs = fileManagerService.getServerConfig();
super({
baseURL: cortexServerAPI(configs.host, configs.port),
apiKey: cortexNamespace,
});
this.serverConfigs = configs;
}
}

0 comments on commit e64d481

Please sign in to comment.