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: support cortex configs and engines commands #782

Merged
merged 6 commits into from
Jun 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
10 changes: 10 additions & 0 deletions cortex-js/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import { StatusController } from './infrastructure/controllers/status.controller
import { ProcessController } from './infrastructure/controllers/process.controller';
import { DownloadManagerModule } from './infrastructure/services/download-manager/download-manager.module';
import { ContextModule } from './infrastructure/services/context/context.module';
import { ExtensionsModule } from './extensions/extensions.module';
import { ConfigsModule } from './usecases/configs/configs.module';
import { EnginesModule } from './usecases/engines/engines.module';
import { ConfigsController } from './infrastructure/controllers/configs.controller';
import { EnginesController } from './infrastructure/controllers/engines.controller';

@Module({
imports: [
Expand All @@ -50,6 +55,9 @@ import { ContextModule } from './infrastructure/services/context/context.module'
TelemetryModule,
ContextModule,
DownloadManagerModule,
ExtensionsModule,
ConfigsModule,
EnginesModule,
],
controllers: [
AssistantsController,
Expand All @@ -60,6 +68,8 @@ import { ContextModule } from './infrastructure/services/context/context.module'
StatusController,
ProcessController,
EventsController,
ConfigsController,
EnginesController,
],
providers: [
{
Expand Down
5 changes: 4 additions & 1 deletion cortex-js/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ export const getApp = async () => {
const app = await NestFactory.create(AppModule, {
snapshot: true,
cors: true,
logger: console
logger: console,
});

// Set the global prefix for the API /v1/
app.setGlobalPrefix('v1');

const fileService = app.get(FileManagerService);
await fileService.getConfig();

Expand Down
24 changes: 24 additions & 0 deletions cortex-js/src/command.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ import { EventEmitterModule } from '@nestjs/event-emitter';
import { DownloadManagerModule } from './infrastructure/services/download-manager/download-manager.module';
import { ServeStopCommand } from './infrastructure/commanders/sub-commands/serve-stop.command';
import { ContextModule } from './infrastructure/services/context/context.module';
import { ExtensionsModule } from './extensions/extensions.module';
import { ConfigsCommand } from './infrastructure/commanders/configs.command';
import { EnginesCommand } from './infrastructure/commanders/engines.command';
import { ConfigsModule } from './usecases/configs/configs.module';
import { EnginesModule } from './usecases/engines/engines.module';
import { ConfigsGetCommand } from './infrastructure/commanders/configs/configs-get.command';
import { ConfigsListCommand } from './infrastructure/commanders/configs/configs-list.command';
import { ConfigsSetCommand } from './infrastructure/commanders/configs/configs-set.command';
import { EnginesListCommand } from './infrastructure/commanders/engines/engines-list.command';
import { EnginesGetCommand } from './infrastructure/commanders/engines/engines-get.command';

@Module({
imports: [
Expand All @@ -55,6 +65,9 @@ import { ContextModule } from './infrastructure/services/context/context.module'
TelemetryModule,
ContextModule,
DownloadManagerModule,
ExtensionsModule,
ConfigsModule,
EnginesModule,
],
providers: [
CortexCommand,
Expand All @@ -67,6 +80,7 @@ import { ContextModule } from './infrastructure/services/context/context.module'
PresetCommand,
EmbeddingCommand,
BenchmarkCommand,
EnginesCommand,

// Questions
InitRunModeQuestions,
Expand All @@ -88,6 +102,16 @@ import { ContextModule } from './infrastructure/services/context/context.module'

// Serve
ServeStopCommand,

// Configs
ConfigsCommand,
ConfigsGetCommand,
ConfigsListCommand,
ConfigsSetCommand,

// Engines
EnginesListCommand,
EnginesGetCommand,
],
})
export class CommandModule {}
2 changes: 1 addition & 1 deletion cortex-js/src/domain/abstracts/engine.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Model, ModelSettingParams } from '../../domain/models/model.interface';
import { Extension } from './extension.abstract';

export abstract class EngineExtension extends Extension {
abstract provider: string;
abstract onLoad(): void;

abstract inference(
dto: any,
Expand Down
2 changes: 1 addition & 1 deletion cortex-js/src/domain/abstracts/extension.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
export abstract class Extension {
/** @type {string} Name of the extension. */
name?: string;
name: string;

/** @type {string} Product Name of the extension. */
productName?: string;
Expand Down
2 changes: 2 additions & 0 deletions cortex-js/src/domain/abstracts/oai.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export abstract class OAIEngineExtension extends EngineExtension {
super();
}

override onLoad(): void {}

override async inference(
createChatDto: any,
headers: Record<string, string>,
Expand Down
25 changes: 25 additions & 0 deletions cortex-js/src/extensions/extensions.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Module } from '@nestjs/common';
import GroqEngineExtension from './groq.engine';
import MistralEngineExtension from './mistral.engine';
import OpenAIEngineExtension from './openai.engine';
import { HttpModule, HttpService } from '@nestjs/axios';
import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';
import { ConfigsModule } from '@/usecases/configs/configs.module';

const provider = {
provide: 'EXTENSIONS_PROVIDER',
inject: [HttpService, ConfigsUsecases],
useFactory: (httpService: HttpService, configUsecases: ConfigsUsecases) => [
new OpenAIEngineExtension(httpService, configUsecases),
new GroqEngineExtension(httpService, configUsecases),
new MistralEngineExtension(httpService, configUsecases),
],
};

@Module({
imports: [HttpModule, ConfigsModule],
controllers: [],
providers: [provider],
exports: [provider],
})
export class ExtensionsModule {}
31 changes: 31 additions & 0 deletions cortex-js/src/extensions/groq.engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { HttpService } from '@nestjs/axios';
import { OAIEngineExtension } from '../domain/abstracts/oai.abstract';
import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';

/**
* A class that implements the InferenceExtension interface from the @janhq/core package.
* The class provides methods for initializing and stopping a model, and for making inference requests.
* It also subscribes to events emitted by the @janhq/core package and handles new message requests.
*/
export default class GroqEngineExtension extends OAIEngineExtension {
apiUrl = 'https://api.groq.com/openai/v1/chat/completions';
name = 'groq';
productName = 'Groq Inference Engine';
description = 'This extension enables fast Groq chat completion API calls';
version = '0.0.1';

constructor(
protected readonly httpService: HttpService,
protected readonly configsUsecases: ConfigsUsecases,
) {
super(httpService);
}

async onLoad() {
const configs = (await this.configsUsecases.getGroupConfigs(
this.name,
)) as unknown as { apiKey: string };
if (!configs?.apiKey)
await this.configsUsecases.saveConfig('apiKey', '', this.name);
}
}
11 changes: 0 additions & 11 deletions cortex-js/src/extensions/groq/index.ts

This file was deleted.

31 changes: 0 additions & 31 deletions cortex-js/src/extensions/groq/package.json

This file was deleted.

14 changes: 0 additions & 14 deletions cortex-js/src/extensions/groq/tsconfig.json

This file was deleted.

31 changes: 31 additions & 0 deletions cortex-js/src/extensions/mistral.engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { HttpService } from '@nestjs/axios';
import { OAIEngineExtension } from '../domain/abstracts/oai.abstract';
import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';

/**
* A class that implements the InferenceExtension interface from the @janhq/core package.
* The class provides methods for initializing and stopping a model, and for making inference requests.
* It also subscribes to events emitted by the @janhq/core package and handles new message requests.
*/
export default class MistralEngineExtension extends OAIEngineExtension {
apiUrl = 'https://api.mistral.ai/v1/chat/completions';
name = 'mistral';
productName = 'Mistral Inference Engine';
description = 'This extension enables Mistral chat completion API calls';
version = '0.0.1';

constructor(
protected readonly httpService: HttpService,
protected readonly configsUsecases: ConfigsUsecases,
) {
super(httpService);
}

async onLoad() {
const configs = (await this.configsUsecases.getGroupConfigs(
this.name,
)) as unknown as { apiKey: string };
if (!configs?.apiKey)
await this.configsUsecases.saveConfig('apiKey', '', this.name);
}
}
11 changes: 0 additions & 11 deletions cortex-js/src/extensions/mistral/index.ts

This file was deleted.

30 changes: 0 additions & 30 deletions cortex-js/src/extensions/mistral/package.json

This file was deleted.

14 changes: 0 additions & 14 deletions cortex-js/src/extensions/mistral/tsconfig.json

This file was deleted.

31 changes: 31 additions & 0 deletions cortex-js/src/extensions/openai.engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { HttpService } from '@nestjs/axios';
import { OAIEngineExtension } from '../domain/abstracts/oai.abstract';
import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';

/**
* A class that implements the InferenceExtension interface from the @janhq/core package.
* The class provides methods for initializing and stopping a model, and for making inference requests.
* It also subscribes to events emitted by the @janhq/core package and handles new message requests.
*/
export default class OpenAIEngineExtension extends OAIEngineExtension {
apiUrl = 'https://api.openai.com/v1/chat/completions';
name = 'openai';
productName = 'OpenAI Inference Engine';
description = 'This extension enables OpenAI chat completion API calls';
version = '0.0.1';

constructor(
protected readonly httpService: HttpService,
protected readonly configsUsecases: ConfigsUsecases,
) {
super(httpService);
}

async onLoad() {
const configs = (await this.configsUsecases.getGroupConfigs(
this.name,
)) as unknown as { apiKey: string };
if (!configs?.apiKey)
await this.configsUsecases.saveConfig('apiKey', '', this.name);
}
}
11 changes: 0 additions & 11 deletions cortex-js/src/extensions/openai/index.ts

This file was deleted.

Loading
Loading