Skip to content

Commit

Permalink
feat: support cortex configs and engines commands
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan committed Jun 26, 2024
1 parent 2f32dc5 commit c6ce3b8
Show file tree
Hide file tree
Showing 34 changed files with 512 additions and 177 deletions.
6 changes: 6 additions & 0 deletions cortex-js/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ 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';

@Module({
imports: [
Expand All @@ -50,6 +53,9 @@ import { ContextModule } from './infrastructure/services/context/context.module'
TelemetryModule,
ContextModule,
DownloadManagerModule,
ExtensionsModule,
ConfigsModule,
EnginesModule,
],
controllers: [
AssistantsController,
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: 2 additions & 0 deletions cortex-js/src/domain/abstracts/engine.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { Extension } from './extension.abstract';
export abstract class EngineExtension extends Extension {
abstract provider: string;

abstract onLoad(): void;

abstract inference(
dto: any,
headers: Record<string, 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 {}
30 changes: 30 additions & 0 deletions cortex-js/src/extensions/groq.engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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 {
provider: string = 'groq';
apiUrl = 'https://api.groq.com/openai/v1/chat/completions';
name = 'Groq Inference Engine';
description = 'This extension enables fast Groq chat completion API calls';

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

async onLoad() {
const configs = (await this.configsUsecases.getGroupConfigs(
this.provider,
)) as unknown as { apiKey: string };
if (!configs?.apiKey)
await this.configsUsecases.saveConfig('apiKey', '', this.provider);
}
}
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.

30 changes: 30 additions & 0 deletions cortex-js/src/extensions/mistral.engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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 {
provider: string = 'mistral';
apiUrl = 'https://api.mistral.ai/v1/chat/completions';
name = 'Mistral Inference Engine';
description = 'This extension enables Mistral chat completion API calls';

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

async onLoad() {
const configs = (await this.configsUsecases.getGroupConfigs(
this.provider,
)) as unknown as { apiKey: string };
if (!configs?.apiKey)
await this.configsUsecases.saveConfig('apiKey', '', this.provider);
}
}
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.

30 changes: 30 additions & 0 deletions cortex-js/src/extensions/openai.engine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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 {
provider: string = 'openai';
apiUrl = 'https://api.openai.com/v1/chat/completions';
name = 'OpenAI Inference Engine';
description? = 'This extension enables OpenAI chat completion API calls';

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

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

This file was deleted.

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

This file was deleted.

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

This file was deleted.

Loading

0 comments on commit c6ce3b8

Please sign in to comment.