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 default api server config #921

Merged
merged 6 commits into from
Jul 26, 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
2 changes: 1 addition & 1 deletion cortex-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"class-validator": "^0.14.1",
"cli-progress": "^3.12.0",
"cortex-cpp": "0.4.34",
"@cortexso/cortex.js": "^0.1.1",
"@cortexso/cortex.js": "^0.1.2",
"cpu-instructions": "^0.0.11",
"decompress": "^4.2.1",
"js-yaml": "^4.1.0",
Expand Down
19 changes: 0 additions & 19 deletions cortex-js/src/infrastructure/commanders/base.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,11 @@ import { Injectable } from '@nestjs/common';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import Cortex from '@cortexso/cortex.js';
import ora from 'ora';
import { FileManagerService } from '../services/file-manager/file-manager.service';
import { cortexNamespace, cortexServerAPI } from '../constants/cortex';

@Injectable()
export abstract class BaseCommand extends CommandRunner {
// Cortex client instance to communicate with cortex API server
cortex: Cortex;
serverConfigs: { host: string; port: number };

constructor(readonly cortexUseCases: CortexUsecases) {
super();
// No need to inject services, since there is no nested dependencies
const fileManagerService: FileManagerService = new FileManagerService();
this.serverConfigs = fileManagerService.getServerConfig();

// Instantiate cortex client, it will be use throughout the command
this.cortex = new Cortex({
apiKey: cortexNamespace,
baseURL: cortexServerAPI(
this.serverConfigs.host,
this.serverConfigs.port,
),
timeout: 20 * 1000,
});
}
protected abstract runCommand(
passedParam: string[],
Expand Down
30 changes: 30 additions & 0 deletions cortex-js/src/infrastructure/commanders/base.subcommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Injectable } from '@nestjs/common';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import Cortex from '@cortexso/cortex.js';
import { FileManagerService } from '../services/file-manager/file-manager.service';
import { cortexNamespace, cortexServerAPI } from '../constants/cortex';
import { BaseCommand } from './base.command';

@Injectable()
export abstract class BaseSubCommand extends BaseCommand {
// Cortex client instance to communicate with cortex API server
cortex: Cortex;
serverConfigs: { host: string; port: number };

constructor(readonly cortexUseCases: CortexUsecases) {
super(cortexUseCases);
// No need to inject services, since there is no nested dependencies
const fileManagerService: FileManagerService = new FileManagerService();
this.serverConfigs = fileManagerService.getServerConfig();

// Instantiate cortex client, it will be use throughout the command
this.cortex = new Cortex({
apiKey: cortexNamespace,
baseURL: cortexServerAPI(
this.serverConfigs.host,
this.serverConfigs.port,
),
timeout: 20 * 1000,
});
}
}
3 changes: 2 additions & 1 deletion cortex-js/src/infrastructure/commanders/benchmark.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +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';

@SubCommand({
name: 'benchmark',
Expand All @@ -26,7 +27,7 @@ import { Cortex } from '@cortexso/cortex.js';
description:
'Benchmark and analyze the performance of a specific AI model using a variety of system resources',
})
export class BenchmarkCommand extends BaseCommand {
export class BenchmarkCommand extends BaseSubCommand {
constructor(
private readonly cortexUsecases: CortexUsecases,
private readonly fileService: FileManagerService,
Expand Down
3 changes: 2 additions & 1 deletion cortex-js/src/infrastructure/commanders/chat.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +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';

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

constructor(
Expand Down
41 changes: 24 additions & 17 deletions cortex-js/src/infrastructure/commanders/cortex-command.commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ type ServeOptions = {
})
@SetCommandContext()
export class CortexCommand extends CommandRunner {
host: string;
port: number;
configHost: string;
configPort: number;
constructor(
readonly contextService: ContextService,
readonly fileManagerService: FileManagerService,
Expand All @@ -58,8 +62,17 @@ export class CortexCommand extends CommandRunner {
}

async run(passedParams: string[], options?: ServeOptions): Promise<void> {
const host = options?.address || defaultCortexJsHost;
const port = options?.port || defaultCortexJsPort;
const {
apiServerHost: configApiServerHost,
apiServerPort: configApiServerPort,
} = await this.fileManagerService.getConfig();

this.configHost = configApiServerHost || defaultCortexJsHost;
this.configPort = configApiServerPort || defaultCortexJsPort;

this.host = options?.address || configApiServerHost || defaultCortexJsHost;
this.port = options?.port || configApiServerPort || defaultCortexJsPort;

const showLogs = options?.logs || false;
const showVersion = options?.version || false;
const dataFolderPath = options?.dataFolder;
Expand All @@ -70,12 +83,10 @@ export class CortexCommand extends CommandRunner {
console.log(chalk.blue(`Github: ${pkg.homepage}`));
return;
}
return this.startServer(host, port, showLogs, dataFolderPath);
return this.startServer(showLogs, dataFolderPath);
}

private async startServer(
host: string,
port: number,
attach: boolean,
dataFolderPath?: string,
) {
Expand All @@ -89,13 +100,9 @@ export class CortexCommand extends CommandRunner {
startEngineSpinner.succeed('Cortex started successfully');
const isServerOnline = await this.cortexUseCases.isAPIServerOnline();
if (isServerOnline) {
const {
apiServerHost: configApiServerHost,
apiServerPort: configApiServerPort,
} = await this.fileManagerService.getConfig();
console.log(
chalk.blue(
`Server is already running at http://${configApiServerHost}:${configApiServerPort}. Please use 'cortex stop' to stop the server.`,
`Server is already running at http://${this.configHost}:${this.configPort}. Please use 'cortex stop' to stop the server.`,
),
);
process.exit(0);
Expand All @@ -110,18 +117,18 @@ export class CortexCommand extends CommandRunner {
}
if (attach) {
const app = await getApp();
await app.listen(port, host);
await app.listen(this.port, this.host);
} else {
await this.cortexUseCases.startServerDetached(host, port);
await this.cortexUseCases.startServerDetached(this.host, this.port);
}
console.log(chalk.blue(`Started server at http://${host}:${port}`));
console.log(chalk.blue(`Started server at http://${this.host}:${this.port}`));
console.log(
chalk.blue(`API Playground available at http://${host}:${port}/api`),
chalk.blue(`API Playground available at http://${this.host}:${this.port}/api`),
);
await this.fileManagerService.writeConfigFile({
...config,
apiServerHost: host,
apiServerPort: port,
apiServerHost: this.host,
apiServerPort: this.port,
dataFolderPath: dataFolderPath || config.dataFolderPath,
});
if (!attach) process.exit(0);
Expand All @@ -131,7 +138,7 @@ export class CortexCommand extends CommandRunner {
await this.fileManagerService.writeConfigFile({
...config,
});
console.error(`Failed to start server. Is port ${port} in use?`);
console.error(`Failed to start server. Is port ${this.port} in use?`);
process.exit(1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +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';

interface EmbeddingCommandOptions {
encoding_format?: string;
Expand All @@ -20,7 +21,7 @@ interface EmbeddingCommandOptions {
'Model to use for embedding. If not provided, it will prompt to select from running models.',
},
})
export class EmbeddingCommand extends BaseCommand {
export class EmbeddingCommand extends BaseSubCommand {
constructor(
private readonly inquirerService: InquirerService,
readonly cortexUsecases: CortexUsecases,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +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';

@SubCommand({
name: '<name> get',
Expand All @@ -13,7 +14,7 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
},
})
@SetCommandContext()
export class EnginesGetCommand extends BaseCommand {
export class EnginesGetCommand extends BaseSubCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUsecases: CortexUsecases,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +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';

@SubCommand({
name: '<name> init',
Expand All @@ -17,7 +18,7 @@ import { Presets, SingleBar } from 'cli-progress';
},
})
@SetCommandContext()
export class EnginesInitCommand extends BaseCommand {
export class EnginesInitCommand extends BaseSubCommand {
constructor(
private readonly cortexUsecases: CortexUsecases,
private readonly fileManagerService: FileManagerService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ 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';

@SubCommand({
name: 'list',
description: 'Get all cortex engines',
})
@SetCommandContext()
export class EnginesListCommand extends BaseCommand {
export class EnginesListCommand extends BaseSubCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +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';

@SubCommand({
name: '<name> set <config> <value>',
Expand All @@ -11,7 +12,7 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
},
})
@SetCommandContext()
export class EnginesSetCommand extends BaseCommand {
export class EnginesSetCommand extends BaseSubCommand {
constructor(readonly cortexUsecases: CortexUsecases) {
super(cortexUsecases);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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';

@SubCommand({
name: 'get',
Expand All @@ -14,7 +15,7 @@ import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
},
})
@SetCommandContext()
export class ModelGetCommand extends BaseCommand {
export class ModelGetCommand extends BaseSubCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ 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';

interface ModelListOptions {
format: 'table' | 'json';
}
@SubCommand({ name: 'list', description: 'List all models locally.' })
@SetCommandContext()
export class ModelListCommand extends BaseCommand {
export class ModelListCommand extends BaseSubCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +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';

@SubCommand({
name: 'pull',
Expand All @@ -26,7 +27,7 @@ import { downloadModelProgress } from '@/utils/pull-model';
'Download a model from a registry. Working with HuggingFace repositories. For available models, please visit https://huggingface.co/cortexso',
})
@SetCommandContext()
export class ModelPullCommand extends BaseCommand {
export class ModelPullCommand extends BaseSubCommand {
constructor(
private readonly fileService: FileManagerService,
readonly contextService: ContextService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +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';

@SubCommand({
name: 'remove',
Expand All @@ -14,7 +15,7 @@ import { BaseCommand } from '../base.command';
},
})
@SetCommandContext()
export class ModelRemoveCommand extends BaseCommand {
export class ModelRemoveCommand extends BaseSubCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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';

type ModelStartOptions = {
attach: boolean;
Expand All @@ -32,7 +33,7 @@ type ModelStartOptions = {
},
})
@SetCommandContext()
export class ModelStartCommand extends BaseCommand {
export class ModelStartCommand extends BaseSubCommand {
constructor(
private readonly inquirerService: InquirerService,
readonly cortexUsecases: CortexUsecases,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ContextService } from '@/infrastructure/services/context/context.servic
import { BaseCommand } from '../base.command';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import ora from 'ora';
import { BaseSubCommand } from '../base.subcommand';

@SubCommand({
name: 'stop',
Expand All @@ -15,7 +16,7 @@ import ora from 'ora';
},
})
@SetCommandContext()
export class ModelStopCommand extends BaseCommand {
export class ModelStopCommand extends BaseSubCommand {
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
Expand Down
Loading
Loading