Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit e139355

Browse files
feat: add config cpp port (#973)
Co-authored-by: Louis <[email protected]>
1 parent 967ed0e commit e139355

File tree

7 files changed

+50
-8
lines changed

7 files changed

+50
-8
lines changed

cortex-cpp/addon.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424

2525
static Napi::Env* s_env = nullptr;
2626

27-
void start() {
27+
void start(const int port = 3929) {
2828
int thread_num = 1;
2929
std::string host = "127.0.0.1";
30-
int port = 3929;
3130
std::string uploads_folder_path;
3231
int logical_cores = std::thread::hardware_concurrency();
3332
int drogon_thread_num = std::max(thread_num, logical_cores);
@@ -66,7 +65,11 @@ Napi::Value Start(const Napi::CallbackInfo& info) {
6665
// Register exitCallback with atexit
6766
std::atexit(exitCallback);
6867

69-
start();
68+
69+
Napi::Number jsParam = info[0].As<Napi::Number>();
70+
int port = jsParam.Int32Value();
71+
72+
start(port);
7073
return env.Undefined();
7174
}
7275

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

85-
NODE_API_MODULE(cortex-cpp, Init)
88+
NODE_API_MODULE(cortex-cpp, Init)

cortex-cpp/binding/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
/// <reference types="node" />
44
declare module "cortex-cpp" {
5-
export function start();
5+
export function start(port?: number);
66
export function stop();
77
}

cortex-js/src/infrastructure/commanders/cortex-command.commander.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import { BenchmarkCommand } from './benchmark.command';
1313
import chalk from 'chalk';
1414
import { ContextService } from '../services/context/context.service';
1515
import { EnginesCommand } from './engines.command';
16-
import { defaultCortexJsHost, defaultCortexJsPort } from '../constants/cortex';
16+
import {
17+
defaultCortexCppPort,
18+
defaultCortexJsHost,
19+
defaultCortexJsPort,
20+
} from '../constants/cortex';
1721
import { getApp } from '@/app';
1822
import { fileManagerService } from '../services/file-manager/file-manager.service';
1923
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
@@ -28,6 +32,7 @@ type ServeOptions = {
2832
dataFolder?: string;
2933
version?: boolean;
3034
name?: string;
35+
enginePort?: string;
3136
};
3237

3338
@RootCommand({
@@ -53,6 +58,7 @@ export class CortexCommand extends CommandRunner {
5358
port: number;
5459
configHost: string;
5560
configPort: number;
61+
enginePort: number;
5662
constructor(
5763
readonly contextService: ContextService,
5864
readonly cortexUseCases: CortexUsecases,
@@ -70,19 +76,25 @@ export class CortexCommand extends CommandRunner {
7076
...fileManagerService.defaultConfig(),
7177
apiServerHost: options?.address || defaultCortexJsHost,
7278
apiServerPort: options?.port || defaultCortexJsPort,
79+
cortexCppPort: Number(options?.enginePort) || defaultCortexCppPort,
7380
});
7481
}
7582
}
7683
const {
7784
apiServerHost: configApiServerHost,
7885
apiServerPort: configApiServerPort,
86+
cortexCppPort: configCortexCppPort,
7987
} = await fileManagerService.getConfig();
8088

8189
this.configHost = configApiServerHost || defaultCortexJsHost;
8290
this.configPort = configApiServerPort || defaultCortexJsPort;
8391

8492
this.host = options?.address || configApiServerHost || defaultCortexJsHost;
8593
this.port = options?.port || configApiServerPort || defaultCortexJsPort;
94+
this.enginePort =
95+
Number(options?.enginePort) ||
96+
configCortexCppPort ||
97+
defaultCortexCppPort;
8698
const showLogs = options?.logs || false;
8799
const showVersion = options?.version || false;
88100
const dataFolderPath = options?.dataFolder;
@@ -140,6 +152,7 @@ export class CortexCommand extends CommandRunner {
140152
apiServerHost: this.host,
141153
apiServerPort: this.port,
142154
dataFolderPath: dataFolderPath || config.dataFolderPath,
155+
cortexCppPort: this.enginePort,
143156
});
144157
if (!attach) process.exit(0);
145158
} catch (e) {
@@ -178,7 +191,7 @@ export class CortexCommand extends CommandRunner {
178191
}
179192

180193
@Option({
181-
flags: '--dataFolder <dataFolderPath>',
194+
flags: '-df, --dataFolder <dataFolderPath>',
182195
description: 'Set the data folder directory',
183196
})
184197
parseDataFolder(value: string) {
@@ -192,6 +205,7 @@ export class CortexCommand extends CommandRunner {
192205
parseVersion() {
193206
return true;
194207
}
208+
195209
@Option({
196210
flags: '-n, --name <name>',
197211
description: 'Name of the process',
@@ -200,4 +214,12 @@ export class CortexCommand extends CommandRunner {
200214
fileManagerService.setConfigProfile(value);
201215
return value;
202216
}
217+
218+
@Option({
219+
flags: '-ep, --engine-port <port>',
220+
description: 'Port to serve the engine',
221+
})
222+
parseEnginePort(value: string) {
223+
return value;
224+
}
203225
}

cortex-js/src/infrastructure/providers/cortex/cortex.provider.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ export default class CortexProvider extends OAIEngineExtension {
165165
return { error: 'Cannot split prompt template' };
166166
};
167167

168+
public setUrls(host: string, port: number): void {
169+
this.apiUrl = `http://${host}:${port}/inferences/server/chat_completion`;
170+
this.loadModelUrl = `http://${host}:${port}/inferences/server/loadmodel`;
171+
this.unloadModelUrl = `http://${host}:${port}/inferences/server/unloadmodel`;
172+
}
173+
168174
private persistEngineVersion = async () => {
169175
const versionFilePath = join(
170176
await fileManagerService.getCortexCppEnginePath(),

cortex-js/src/infrastructure/repositories/extensions/extension.repository.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,18 @@ export class ExtensionRepositoryImpl implements ExtensionRepository {
8585
}
8686

8787
private async loadCoreExtensions() {
88+
const { cortexCppPort, cortexCppHost } =
89+
await fileManagerService.getConfig();
8890
const llamaCPPEngine = new LlamaCPPProvider(this.httpService);
91+
llamaCPPEngine.setUrls(cortexCppHost, cortexCppPort);
8992
llamaCPPEngine.status = existsSync(
9093
join(await fileManagerService.getCortexCppEnginePath(), Engines.llamaCPP),
9194
)
9295
? EngineStatus.READY
9396
: EngineStatus.NOT_INITIALIZED;
9497

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

105109
const tensorrtLLMEngine = new TensorrtLLMProvider(this.httpService);
110+
onnxEngine.setUrls(cortexCppHost, cortexCppPort);
106111
tensorrtLLMEngine.status =
107112
existsSync(
108113
join(

cortex-js/src/usecases/cortex/cortex.usecases.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export class CortexUsecases implements BeforeApplicationShutdown {
6262
delimiter,
6363
engineDir,
6464
),
65+
CORTEX_CPP_PORT: port.toString(),
6566
// // Vulkan - Support 1 device at a time for now
6667
// ...(executableOptions.vkVisibleDevices?.length > 0 && {
6768
// GGML_VULKAN_DEVICE: executableOptions.vkVisibleDevices[0],

cortex-js/src/utils/cortex-cpp.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
import * as cortexCPP from 'cortex-cpp';
22

3-
cortexCPP.start();
3+
const port = process.env.CORTEX_CPP_PORT
4+
? parseInt(process.env.CORTEX_CPP_PORT)
5+
: 3929;
6+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
7+
// @ts-expect-error
8+
cortexCPP.start(port);

0 commit comments

Comments
 (0)