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

Commit f49820f

Browse files
authored
feat: add engine init endpoint (#888)
1 parent 61f6c1b commit f49820f

26 files changed

+362
-355
lines changed

cortex-js/src/domain/abstracts/engine.abstract.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export abstract class EngineExtension extends Extension {
1010

1111
transformResponse?: Function;
1212

13+
initalized: boolean = false;
14+
1315
abstract inference(
1416
dto: any,
1517
headers: Record<string, string>,

cortex-js/src/domain/abstracts/extension.abstract.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ export abstract class Extension {
2121
/** @type {string} Extension's version. */
2222
version?: string;
2323

24+
/** @type {boolean} Whether the extension is initialized or not. */
25+
initalized: boolean;
26+
2427
/**
2528
* Called when the extension is loaded.
2629
* Any initialization logic for the extension should be put here.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export interface Config {
22
dataFolderPath: string;
3-
initialized: boolean;
43
cortexCppHost: string;
54
cortexCppPort: number;
65
}

cortex-js/src/domain/models/download.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@ export interface DownloadStateEvent {
6969
export enum DownloadType {
7070
Model = 'model',
7171
Miscelanous = 'miscelanous',
72+
Engine = 'engine'
7273
}

cortex-js/src/extensions/anthropic.engine.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default class AnthropicEngineExtension extends OAIEngineExtension {
1616
productName = 'Anthropic Inference Engine';
1717
description = 'This extension enables Anthropic chat completion API calls';
1818
version = '0.0.1';
19+
initalized = true;
1920
apiKey?: string;
2021

2122
constructor(
@@ -39,42 +40,45 @@ export default class AnthropicEngineExtension extends OAIEngineExtension {
3940
this.apiKey = configs?.apiKey;
4041
}
4142

42-
override async inference(dto: any, headers: Record<string, string>): Promise<stream.Readable | any> {
43-
headers['x-api-key'] = this.apiKey as string
44-
headers['Content-Type'] = 'application/json'
45-
headers['anthropic-version'] = '2023-06-01'
46-
return super.inference(dto, headers)
43+
override async inference(
44+
dto: any,
45+
headers: Record<string, string>,
46+
): Promise<stream.Readable | any> {
47+
headers['x-api-key'] = this.apiKey as string;
48+
headers['Content-Type'] = 'application/json';
49+
headers['anthropic-version'] = '2023-06-01';
50+
return super.inference(dto, headers);
4751
}
4852

4953
transformPayload = (data: any): any => {
5054
return _.pick(data, ['messages', 'model', 'stream', 'max_tokens']);
51-
}
55+
};
5256

5357
transformResponse = (data: any): string => {
5458
// handling stream response
5559
if (typeof data === 'string' && data.trim().length === 0) {
56-
return '';
60+
return '';
5761
}
5862
if (typeof data === 'string' && data.startsWith('event: ')) {
59-
return ''
63+
return '';
6064
}
6165
if (typeof data === 'string' && data.startsWith('data: ')) {
6266
data = data.replace('data: ', '');
6367
const parsedData = JSON.parse(data);
6468
if (parsedData.type !== 'content_block_delta') {
65-
return ''
69+
return '';
6670
}
6771
const text = parsedData.delta?.text;
6872
//convert to have this format data.choices[0]?.delta?.content
6973
return JSON.stringify({
7074
choices: [
71-
{
72-
delta: {
73-
content: text
74-
}
75-
}
76-
]
77-
})
75+
{
76+
delta: {
77+
content: text,
78+
},
79+
},
80+
],
81+
});
7882
}
7983
// non-stream response
8084
if (data.content && data.content.length > 0 && data.content[0].text) {
@@ -88,8 +92,8 @@ export default class AnthropicEngineExtension extends OAIEngineExtension {
8892
],
8993
});
9094
}
91-
95+
9296
console.error('Invalid response format:', data);
9397
return '';
94-
}
98+
};
9599
}

cortex-js/src/extensions/groq.engine.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default class GroqEngineExtension extends OAIEngineExtension {
1414
productName = 'Groq Inference Engine';
1515
description = 'This extension enables fast Groq chat completion API calls';
1616
version = '0.0.1';
17+
initalized = true;
1718
apiKey?: string;
1819

1920
constructor(

cortex-js/src/extensions/mistral.engine.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default class MistralEngineExtension extends OAIEngineExtension {
1414
productName = 'Mistral Inference Engine';
1515
description = 'This extension enables Mistral chat completion API calls';
1616
version = '0.0.1';
17+
initalized = true;
1718
apiKey?: string;
1819

1920
constructor(

cortex-js/src/extensions/openai.engine.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default class OpenAIEngineExtension extends OAIEngineExtension {
1414
productName = 'OpenAI Inference Engine';
1515
description = 'This extension enables OpenAI chat completion API calls';
1616
version = '0.0.1';
17+
initalized = true;
1718
apiKey?: string;
1819

1920
constructor(

cortex-js/src/infrastructure/commanders/engines/engines-init.command.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { CommandRunner, Option, SubCommand } from 'nest-commander';
22
import { SetCommandContext } from '../decorators/CommandContext';
33
import { ContextService } from '@/infrastructure/services/context/context.service';
4-
import { InitCliUsecases } from '../usecases/init.cli.usecases';
54
import { Engines } from '../types/engine.interface';
65
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
76
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
7+
import { EnginesUsecases } from '@/usecases/engines/engines.usecase';
88

99
@SubCommand({
1010
name: '<name> init',
@@ -16,7 +16,7 @@ import { FileManagerService } from '@/infrastructure/services/file-manager/file-
1616
@SetCommandContext()
1717
export class EnginesInitCommand extends CommandRunner {
1818
constructor(
19-
private readonly initUsecases: InitCliUsecases,
19+
private readonly engineUsecases: EnginesUsecases,
2020
private readonly cortexUsecases: CortexUsecases,
2121
private readonly fileManagerService: FileManagerService,
2222
readonly contextService: ContextService,
@@ -31,7 +31,7 @@ export class EnginesInitCommand extends CommandRunner {
3131
const engine = passedParams[0];
3232
const params = passedParams.includes(Engines.llamaCPP)
3333
? {
34-
...(await this.initUsecases.defaultInstallationOptions()),
34+
...(await this.engineUsecases.defaultInstallationOptions()),
3535
...options,
3636
}
3737
: {};
@@ -43,7 +43,8 @@ export class EnginesInitCommand extends CommandRunner {
4343
if (await this.cortexUsecases.healthCheck(host, port)) {
4444
await this.cortexUsecases.stopCortex();
4545
}
46-
return this.initUsecases
46+
console.log(`Installing engine ${engine}...`);
47+
return this.engineUsecases
4748
.installEngine(
4849
params,
4950
engine.includes('@') ? engine.split('@')[1] : 'latest',

cortex-js/src/infrastructure/commanders/models/model-pull.command.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import { ContextService } from '@/infrastructure/services/context/context.servic
1212
import { existsSync } from 'fs';
1313
import { join } from 'node:path';
1414
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
15-
import { InitCliUsecases } from '../usecases/init.cli.usecases';
1615
import { checkModelCompatibility } from '@/utils/model-check';
1716
import { Engines } from '../types/engine.interface';
17+
import { EnginesUsecases } from '@/usecases/engines/engines.usecase';
1818

1919
@SubCommand({
2020
name: 'pull',
@@ -28,7 +28,7 @@ import { Engines } from '../types/engine.interface';
2828
export class ModelPullCommand extends CommandRunner {
2929
constructor(
3030
private readonly modelsCliUsecases: ModelsCliUsecases,
31-
private readonly initUsecases: InitCliUsecases,
31+
private readonly engineUsecases: EnginesUsecases,
3232
private readonly fileService: FileManagerService,
3333
readonly contextService: ContextService,
3434
private readonly telemetryUsecases: TelemetryUsecases,
@@ -60,7 +60,7 @@ export class ModelPullCommand extends CommandRunner {
6060
!existsSync(join(await this.fileService.getCortexCppEnginePath(), engine))
6161
) {
6262
console.log('\n');
63-
await this.initUsecases.installEngine(undefined, 'latest', engine);
63+
await this.engineUsecases.installEngine(undefined, 'latest', engine);
6464
}
6565
this.telemetryUsecases.sendEvent(
6666
[

0 commit comments

Comments
 (0)