Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Update the run command to allow model selection and add the Cortex version. #640

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RootCommand, CommandRunner } from 'nest-commander';
import { RootCommand, CommandRunner, Option } from 'nest-commander';
import { ServeCommand } from './serve.command';
import { ChatCommand } from './chat.command';
import { ModelsCommand } from './models.command';
Expand All @@ -7,7 +7,11 @@ import { RunCommand } from './shortcuts/run.command';
import { ModelPullCommand } from './models/model-pull.command';
import { PSCommand } from './ps.command';
import { KillCommand } from './kill.command';
import pkg from '@/../package.json';

interface CortexCommandOptions {
version: boolean;
}
@RootCommand({
subCommands: [
ModelsCommand,
Expand All @@ -22,5 +26,17 @@ import { KillCommand } from './kill.command';
description: 'Cortex CLI',
})
export class CortexCommand extends CommandRunner {
async run(): Promise<void> {}
async run(input: string[], option: CortexCommandOptions): Promise<void> {
if (option.version) console.log(pkg.version);
}

@Option({
flags: '-v, --version',
description: 'Cortex version',
defaultValue: false,
name: 'version',
})
parseVersion() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ModelStartCommand extends CommandRunner {

await this.cortexUsecases
.startCortex(options.attach)
.then(() => this.modelsCliUsecases.startModel(input[0]))
.then(() => this.modelsCliUsecases.startModel(modelId))
.then(console.log)
.then(() => !options.attach && process.exit(0));
}
Expand Down
50 changes: 36 additions & 14 deletions cortex-js/src/infrastructure/commanders/shortcuts/run.command.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { ModelsUsecases } from '@/usecases/models/models.usecases';
import { CommandRunner, SubCommand, Option } from 'nest-commander';
import {
CommandRunner,
SubCommand,
Option,
InquirerService,
} from 'nest-commander';
import { exit } from 'node:process';
import { ChatCliUsecases } from '../usecases/chat.cli.usecases';
import { defaultCortexCppHost, defaultCortexCppPort } from 'constant';
import { ModelsCliUsecases } from '../usecases/models.cli.usecases';

type RunOptions = {
threadId?: string;
Expand All @@ -15,27 +20,29 @@ type RunOptions = {
})
export class RunCommand extends CommandRunner {
constructor(
private readonly modelsUsecases: ModelsUsecases,
private readonly modelsCliUsecases: ModelsCliUsecases,
private readonly cortexUsecases: CortexUsecases,
private readonly chatCliUsecases: ChatCliUsecases,
private readonly inquirerService: InquirerService,
) {
super();
}

async run(input: string[], option?: RunOptions): Promise<void> {
if (input.length === 0) {
console.error('Model Id is required');
exit(1);
let modelId = input[0];
if (!modelId) {
try {
modelId = await this.modelInquiry();
} catch {
console.error('Model ID is required');
exit(1);
}
}
const modelId = input[0];

await this.cortexUsecases.startCortex(
false,
defaultCortexCppHost,
defaultCortexCppPort,
);
await this.modelsUsecases.startModel(modelId);
await this.chatCliUsecases.chat(modelId, option?.threadId);
return this.cortexUsecases
.startCortex(false, defaultCortexCppHost, defaultCortexCppPort)
.then(() => this.modelsCliUsecases.startModel(modelId))
.then(() => this.chatCliUsecases.chat(modelId, option?.threadId));
}

@Option({
Expand All @@ -45,4 +52,19 @@ export class RunCommand extends CommandRunner {
parseThreadId(value: string) {
return value;
}

modelInquiry = async () => {
const models = await this.modelsCliUsecases.listAllModels();
if (!models.length) throw 'No models found';
const { model } = await this.inquirerService.inquirer.prompt({
type: 'list',
name: 'model',
message: 'Select a model to start:',
choices: models.map((e) => ({
name: e.name,
value: e.id,
})),
});
return model;
};
}
55 changes: 24 additions & 31 deletions cortex-js/src/usecases/models/models.usecases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,30 +141,27 @@ export class ModelsUsecases {
if (!engine) {
return {
message: 'No extension handler found for model',
modelId: modelId,
modelId,
};
}

return engine
.loadModel(model, settings)
.then(() => {
return {
message: 'Model loaded successfully',
modelId: modelId,
};
})
.catch((e) => {
if (e.code === AxiosError.ERR_BAD_REQUEST) {
return {
message: 'Model already loaded',
modelId: modelId,
};
}
return {
message: 'Model failed to load',
modelId: modelId,
};
});
.then(() => ({
message: 'Model loaded successfully',
modelId,
}))
.catch((e) =>
e.code === AxiosError.ERR_BAD_REQUEST
? {
message: 'Model already loaded',
modelId,
}
: {
message: 'Model failed to load',
modelId,
},
);
}

async stopModel(modelId: string): Promise<StartModelSuccessDto> {
Expand All @@ -183,18 +180,14 @@ export class ModelsUsecases {

return engine
.unloadModel(modelId)
.then(() => {
return {
message: 'Model is stopped',
modelId,
};
})
.catch(() => {
return {
message: 'Failed to stop model',
modelId,
};
});
.then(() => ({
message: 'Model is stopped',
modelId,
}))
.catch(() => ({
message: 'Failed to stop model',
modelId,
}));
}

async downloadModel(modelId: string, callback?: (progress: number) => void) {
Expand Down