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

feat: add config cpp port #973

Merged
merged 4 commits into from
Aug 5, 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
11 changes: 7 additions & 4 deletions cortex-cpp/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@

static Napi::Env* s_env = nullptr;

void start() {
void start(const int port = 3929) {
int thread_num = 1;
std::string host = "127.0.0.1";
int port = 3929;
std::string uploads_folder_path;
int logical_cores = std::thread::hardware_concurrency();
int drogon_thread_num = std::max(thread_num, logical_cores);
Expand Down Expand Up @@ -66,7 +65,11 @@ Napi::Value Start(const Napi::CallbackInfo& info) {
// Register exitCallback with atexit
std::atexit(exitCallback);

start();

Napi::Number jsParam = info[0].As<Napi::Number>();
int port = jsParam.Int32Value();

start(port);
return env.Undefined();
}

Expand All @@ -82,4 +85,4 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
return exports;
}

NODE_API_MODULE(cortex-cpp, Init)
NODE_API_MODULE(cortex-cpp, Init)
2 changes: 1 addition & 1 deletion cortex-cpp/binding/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

/// <reference types="node" />
declare module "cortex-cpp" {
export function start();
export function start(port?: number);
export function stop();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { BenchmarkCommand } from './benchmark.command';
import chalk from 'chalk';
import { ContextService } from '../services/context/context.service';
import { EnginesCommand } from './engines.command';
import { defaultCortexJsHost, defaultCortexJsPort } from '../constants/cortex';
import {
defaultCortexCppPort,
defaultCortexJsHost,
defaultCortexJsPort,
} from '../constants/cortex';
import { getApp } from '@/app';
import { fileManagerService } from '../services/file-manager/file-manager.service';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
Expand All @@ -28,6 +32,7 @@ type ServeOptions = {
dataFolder?: string;
version?: boolean;
name?: string;
enginePort?: string;
};

@RootCommand({
Expand All @@ -53,6 +58,7 @@ export class CortexCommand extends CommandRunner {
port: number;
configHost: string;
configPort: number;
enginePort: number;
constructor(
readonly contextService: ContextService,
readonly cortexUseCases: CortexUsecases,
Expand All @@ -70,19 +76,25 @@ export class CortexCommand extends CommandRunner {
...fileManagerService.defaultConfig(),
apiServerHost: options?.address || defaultCortexJsHost,
apiServerPort: options?.port || defaultCortexJsPort,
cortexCppPort: Number(options?.enginePort) || defaultCortexCppPort,
});
}
}
const {
apiServerHost: configApiServerHost,
apiServerPort: configApiServerPort,
cortexCppPort: configCortexCppPort,
} = await fileManagerService.getConfig();

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

this.host = options?.address || configApiServerHost || defaultCortexJsHost;
this.port = options?.port || configApiServerPort || defaultCortexJsPort;
this.enginePort =
Number(options?.enginePort) ||
configCortexCppPort ||
defaultCortexCppPort;
const showLogs = options?.logs || false;
const showVersion = options?.version || false;
const dataFolderPath = options?.dataFolder;
Expand Down Expand Up @@ -140,6 +152,7 @@ export class CortexCommand extends CommandRunner {
apiServerHost: this.host,
apiServerPort: this.port,
dataFolderPath: dataFolderPath || config.dataFolderPath,
cortexCppPort: this.enginePort,
});
if (!attach) process.exit(0);
} catch (e) {
Expand Down Expand Up @@ -178,7 +191,7 @@ export class CortexCommand extends CommandRunner {
}

@Option({
flags: '--dataFolder <dataFolderPath>',
flags: '-df, --dataFolder <dataFolderPath>',
description: 'Set the data folder directory',
})
parseDataFolder(value: string) {
Expand All @@ -192,6 +205,7 @@ export class CortexCommand extends CommandRunner {
parseVersion() {
return true;
}

@Option({
flags: '-n, --name <name>',
description: 'Name of the process',
Expand All @@ -200,4 +214,12 @@ export class CortexCommand extends CommandRunner {
fileManagerService.setConfigProfile(value);
return value;
}

@Option({
flags: '-ep, --engine-port <port>',
description: 'Port to serve the engine',
})
parseEnginePort(value: string) {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ export default class CortexProvider extends OAIEngineExtension {
return { error: 'Cannot split prompt template' };
};

public setUrls(host: string, port: number): void {
this.apiUrl = `http://${host}:${port}/inferences/server/chat_completion`;
this.loadModelUrl = `http://${host}:${port}/inferences/server/loadmodel`;
this.unloadModelUrl = `http://${host}:${port}/inferences/server/unloadmodel`;
}

private persistEngineVersion = async () => {
const versionFilePath = join(
await fileManagerService.getCortexCppEnginePath(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,18 @@ export class ExtensionRepositoryImpl implements ExtensionRepository {
}

private async loadCoreExtensions() {
const { cortexCppPort, cortexCppHost } =
await fileManagerService.getConfig();
const llamaCPPEngine = new LlamaCPPProvider(this.httpService);
llamaCPPEngine.setUrls(cortexCppHost, cortexCppPort);
llamaCPPEngine.status = existsSync(
join(await fileManagerService.getCortexCppEnginePath(), Engines.llamaCPP),
)
? EngineStatus.READY
: EngineStatus.NOT_INITIALIZED;

const onnxEngine = new Onnxprovider(this.httpService);
onnxEngine.setUrls(cortexCppHost, cortexCppPort);
onnxEngine.status =
existsSync(
join(await fileManagerService.getCortexCppEnginePath(), Engines.onnx),
Expand All @@ -103,6 +107,7 @@ export class ExtensionRepositoryImpl implements ExtensionRepository {
: EngineStatus.NOT_INITIALIZED;

const tensorrtLLMEngine = new TensorrtLLMProvider(this.httpService);
onnxEngine.setUrls(cortexCppHost, cortexCppPort);
tensorrtLLMEngine.status =
existsSync(
join(
Expand Down
1 change: 1 addition & 0 deletions cortex-js/src/usecases/cortex/cortex.usecases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class CortexUsecases implements BeforeApplicationShutdown {
delimiter,
engineDir,
),
CORTEX_CPP_PORT: port.toString(),
// // Vulkan - Support 1 device at a time for now
// ...(executableOptions.vkVisibleDevices?.length > 0 && {
// GGML_VULKAN_DEVICE: executableOptions.vkVisibleDevices[0],
Expand Down
7 changes: 6 additions & 1 deletion cortex-js/src/utils/cortex-cpp.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import * as cortexCPP from 'cortex-cpp';

cortexCPP.start();
const port = process.env.CORTEX_CPP_PORT
? parseInt(process.env.CORTEX_CPP_PORT)
: 3929;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
cortexCPP.start(port);
Loading