Skip to content

Commit

Permalink
feat: add engine init endpoint (#888)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan authored Jul 17, 2024
1 parent 61f6c1b commit f49820f
Show file tree
Hide file tree
Showing 26 changed files with 362 additions and 355 deletions.
2 changes: 2 additions & 0 deletions cortex-js/src/domain/abstracts/engine.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export abstract class EngineExtension extends Extension {

transformResponse?: Function;

initalized: boolean = false;

abstract inference(
dto: any,
headers: Record<string, string>,
Expand Down
3 changes: 3 additions & 0 deletions cortex-js/src/domain/abstracts/extension.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export abstract class Extension {
/** @type {string} Extension's version. */
version?: string;

/** @type {boolean} Whether the extension is initialized or not. */
initalized: boolean;

/**
* Called when the extension is loaded.
* Any initialization logic for the extension should be put here.
Expand Down
1 change: 0 additions & 1 deletion cortex-js/src/domain/config/config.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export interface Config {
dataFolderPath: string;
initialized: boolean;
cortexCppHost: string;
cortexCppPort: number;
}
1 change: 1 addition & 0 deletions cortex-js/src/domain/models/download.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ export interface DownloadStateEvent {
export enum DownloadType {
Model = 'model',
Miscelanous = 'miscelanous',
Engine = 'engine'
}
40 changes: 22 additions & 18 deletions cortex-js/src/extensions/anthropic.engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default class AnthropicEngineExtension extends OAIEngineExtension {
productName = 'Anthropic Inference Engine';
description = 'This extension enables Anthropic chat completion API calls';
version = '0.0.1';
initalized = true;
apiKey?: string;

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

override async inference(dto: any, headers: Record<string, string>): Promise<stream.Readable | any> {
headers['x-api-key'] = this.apiKey as string
headers['Content-Type'] = 'application/json'
headers['anthropic-version'] = '2023-06-01'
return super.inference(dto, headers)
override async inference(
dto: any,
headers: Record<string, string>,
): Promise<stream.Readable | any> {
headers['x-api-key'] = this.apiKey as string;
headers['Content-Type'] = 'application/json';
headers['anthropic-version'] = '2023-06-01';
return super.inference(dto, headers);
}

transformPayload = (data: any): any => {
return _.pick(data, ['messages', 'model', 'stream', 'max_tokens']);
}
};

transformResponse = (data: any): string => {
// handling stream response
if (typeof data === 'string' && data.trim().length === 0) {
return '';
return '';
}
if (typeof data === 'string' && data.startsWith('event: ')) {
return ''
return '';
}
if (typeof data === 'string' && data.startsWith('data: ')) {
data = data.replace('data: ', '');
const parsedData = JSON.parse(data);
if (parsedData.type !== 'content_block_delta') {
return ''
return '';
}
const text = parsedData.delta?.text;
//convert to have this format data.choices[0]?.delta?.content
return JSON.stringify({
choices: [
{
delta: {
content: text
}
}
]
})
{
delta: {
content: text,
},
},
],
});
}
// non-stream response
if (data.content && data.content.length > 0 && data.content[0].text) {
Expand All @@ -88,8 +92,8 @@ export default class AnthropicEngineExtension extends OAIEngineExtension {
],
});
}

console.error('Invalid response format:', data);
return '';
}
};
}
1 change: 1 addition & 0 deletions cortex-js/src/extensions/groq.engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class GroqEngineExtension extends OAIEngineExtension {
productName = 'Groq Inference Engine';
description = 'This extension enables fast Groq chat completion API calls';
version = '0.0.1';
initalized = true;
apiKey?: string;

constructor(
Expand Down
1 change: 1 addition & 0 deletions cortex-js/src/extensions/mistral.engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class MistralEngineExtension extends OAIEngineExtension {
productName = 'Mistral Inference Engine';
description = 'This extension enables Mistral chat completion API calls';
version = '0.0.1';
initalized = true;
apiKey?: string;

constructor(
Expand Down
1 change: 1 addition & 0 deletions cortex-js/src/extensions/openai.engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class OpenAIEngineExtension extends OAIEngineExtension {
productName = 'OpenAI Inference Engine';
description = 'This extension enables OpenAI chat completion API calls';
version = '0.0.1';
initalized = true;
apiKey?: string;

constructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CommandRunner, Option, SubCommand } from 'nest-commander';
import { SetCommandContext } from '../decorators/CommandContext';
import { ContextService } from '@/infrastructure/services/context/context.service';
import { InitCliUsecases } from '../usecases/init.cli.usecases';
import { Engines } from '../types/engine.interface';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
import { EnginesUsecases } from '@/usecases/engines/engines.usecase';

@SubCommand({
name: '<name> init',
Expand All @@ -16,7 +16,7 @@ import { FileManagerService } from '@/infrastructure/services/file-manager/file-
@SetCommandContext()
export class EnginesInitCommand extends CommandRunner {
constructor(
private readonly initUsecases: InitCliUsecases,
private readonly engineUsecases: EnginesUsecases,
private readonly cortexUsecases: CortexUsecases,
private readonly fileManagerService: FileManagerService,
readonly contextService: ContextService,
Expand All @@ -31,7 +31,7 @@ export class EnginesInitCommand extends CommandRunner {
const engine = passedParams[0];
const params = passedParams.includes(Engines.llamaCPP)
? {
...(await this.initUsecases.defaultInstallationOptions()),
...(await this.engineUsecases.defaultInstallationOptions()),
...options,
}
: {};
Expand All @@ -43,7 +43,8 @@ export class EnginesInitCommand extends CommandRunner {
if (await this.cortexUsecases.healthCheck(host, port)) {
await this.cortexUsecases.stopCortex();
}
return this.initUsecases
console.log(`Installing engine ${engine}...`);
return this.engineUsecases
.installEngine(
params,
engine.includes('@') ? engine.split('@')[1] : 'latest',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { ContextService } from '@/infrastructure/services/context/context.servic
import { existsSync } from 'fs';
import { join } from 'node:path';
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
import { InitCliUsecases } from '../usecases/init.cli.usecases';
import { checkModelCompatibility } from '@/utils/model-check';
import { Engines } from '../types/engine.interface';
import { EnginesUsecases } from '@/usecases/engines/engines.usecase';

@SubCommand({
name: 'pull',
Expand All @@ -28,7 +28,7 @@ import { Engines } from '../types/engine.interface';
export class ModelPullCommand extends CommandRunner {
constructor(
private readonly modelsCliUsecases: ModelsCliUsecases,
private readonly initUsecases: InitCliUsecases,
private readonly engineUsecases: EnginesUsecases,
private readonly fileService: FileManagerService,
readonly contextService: ContextService,
private readonly telemetryUsecases: TelemetryUsecases,
Expand Down Expand Up @@ -60,7 +60,7 @@ export class ModelPullCommand extends CommandRunner {
!existsSync(join(await this.fileService.getCortexCppEnginePath(), engine))
) {
console.log('\n');
await this.initUsecases.installEngine(undefined, 'latest', engine);
await this.engineUsecases.installEngine(undefined, 'latest', engine);
}
this.telemetryUsecases.sendEvent(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { ModelsCliUsecases } from '@commanders/usecases/models.cli.usecases';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { SetCommandContext } from '../decorators/CommandContext';
import { ContextService } from '@/infrastructure/services/context/context.service';
import { InitCliUsecases } from '../usecases/init.cli.usecases';
import { createReadStream, existsSync, statSync, watchFile } from 'node:fs';
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
import { join } from 'node:path';
import { Engines } from '../types/engine.interface';
import { checkModelCompatibility } from '@/utils/model-check';
import { EnginesUsecases } from '@/usecases/engines/engines.usecase';

type ModelStartOptions = {
attach: boolean;
Expand All @@ -36,7 +36,7 @@ export class ModelStartCommand extends CommandRunner {
private readonly inquirerService: InquirerService,
private readonly cortexUsecases: CortexUsecases,
private readonly modelsCliUsecases: ModelsCliUsecases,
private readonly initUsecases: InitCliUsecases,
private readonly initUsecases: EnginesUsecases,
private readonly fileService: FileManagerService,
readonly contextService: ContextService,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { ModelNotFoundException } from '@/infrastructure/exception/model-not-fou
import { existsSync } from 'fs';
import { join } from 'path';
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
import { InitCliUsecases } from '../usecases/init.cli.usecases';
import { Engines } from '../types/engine.interface';
import { checkModelCompatibility } from '@/utils/model-check';
import { EnginesUsecases } from '@/usecases/engines/engines.usecase';

type RunOptions = {
threadId?: string;
Expand All @@ -38,7 +38,7 @@ export class RunCommand extends CommandRunner {
private readonly chatCliUsecases: ChatCliUsecases,
private readonly inquirerService: InquirerService,
private readonly fileService: FileManagerService,
private readonly initUsecases: InitCliUsecases,
private readonly initUsecases: EnginesUsecases,
) {
super();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ beforeAll(
// Attempt to create test folder
await fileService.writeConfigFile({
dataFolderPath: join(__dirname, 'test_data'),
initialized: false,
cortexCppHost: 'localhost',
cortexCppPort: 3929
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ beforeAll(
// Attempt to create test folder
await fileService.writeConfigFile({
dataFolderPath: join(__dirname, 'test_data'),
initialized: false,
cortexCppHost: 'localhost',
cortexCppPort: 3929,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Module } from '@nestjs/common';
import { InitCliUsecases } from './init.cli.usecases';
import { HttpModule } from '@nestjs/axios';
import { ModelsCliUsecases } from './models.cli.usecases';
import { ModelsModule } from '@/usecases/models/models.module';
Expand All @@ -13,6 +12,8 @@ import { FileManagerModule } from '@/infrastructure/services/file-manager/file-m
import { PSCliUsecases } from './ps.cli.usecases';
import { BenchmarkCliUsecases } from './benchmark.cli.usecases';
import { TelemetryModule } from '@/usecases/telemetry/telemetry.module';
import { DownloadManagerModule } from '@/infrastructure/services/download-manager/download-manager.module';
import { EnginesModule } from '@/usecases/engines/engines.module';

@Module({
imports: [
Expand All @@ -25,16 +26,16 @@ import { TelemetryModule } from '@/usecases/telemetry/telemetry.module';
MessagesModule,
FileManagerModule,
TelemetryModule,
DownloadManagerModule,
EnginesModule,
],
providers: [
InitCliUsecases,
ModelsCliUsecases,
ChatCliUsecases,
PSCliUsecases,
BenchmarkCliUsecases,
],
exports: [
InitCliUsecases,
ModelsCliUsecases,
ChatCliUsecases,
PSCliUsecases,
Expand Down
Loading

0 comments on commit f49820f

Please sign in to comment.