Skip to content

Commit

Permalink
Merge branch 'dev' into feat/cicd-cortex-js
Browse files Browse the repository at this point in the history
  • Loading branch information
hiento09 committed May 16, 2024
2 parents 52b692d + 6cefe1c commit e4b1f96
Show file tree
Hide file tree
Showing 41 changed files with 403 additions and 439 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/cortex-cpp-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ jobs:
with:
submodules: recursive

- uses: actions/setup-dotnet@v3
if: runner.os == 'Windows'
with:
dotnet-version: "8.0.x"

- name: Install choco on Windows
if: runner.os == 'Windows'
run: |
Expand Down Expand Up @@ -189,13 +194,12 @@ jobs:
run: |
cd cortex-cpp
make pre-package
- name: Code Signing
run: |
cd cortex-cpp
make codesign CODE_SIGN=true AZURE_KEY_VAULT_URI="${{ secrets.AZURE_KEY_VAULT_URI }}" AZURE_CLIENT_ID="${{ secrets.AZURE_CLIENT_ID }}" AZURE_TENANT_ID="${{ secrets.AZURE_TENANT_ID }}" AZURE_CLIENT_SECRET="${{ secrets.AZURE_CLIENT_SECRET }}" AZURE_CERT_NAME="${{ secrets.AZURE_CERT_NAME }}" DEVELOPER_ID="${{ secrets.DEVELOPER_ID }}"
- name: Package
run: |
cd cortex-cpp
Expand Down
6 changes: 4 additions & 2 deletions cortex-cpp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ endif

ifeq ($(OS),Windows_NT)
@powershell -Command "dotnet tool install --global AzureSignTool;"
@powershell -Command "Get-ChildItem -Path .\cortex-cpp -Recurse | ForEach-Object { & 'azuresigntool.exe' sign -kvu '$(AZURE_KEY_VAULT_URI)' -kvi '$(AZURE_CLIENT_ID)' -kvt '$(AZURE_TENANT_ID)' -kvs '$(AZURE_CLIENT_SECRET)' -kvc '$(AZURE_CERT_NAME)' -tr http://timestamp.globalsign.com/tsa/r6advanced1 -v '$_.FullName' };"
@powershell -Command 'azuresigntool.exe sign -kvu "$(AZURE_KEY_VAULT_URI)" -kvi "$(AZURE_CLIENT_ID)" -kvt "$(AZURE_TENANT_ID)" -kvs "$(AZURE_CLIENT_SECRET)" -kvc "$(AZURE_CERT_NAME)" -tr http://timestamp.globalsign.com/tsa/r6advanced1 -v ".\cortex-cpp\cortex-cpp.exe";'
@powershell -Command 'azuresigntool.exe sign -kvu "$(AZURE_KEY_VAULT_URI)" -kvi "$(AZURE_CLIENT_ID)" -kvt "$(AZURE_TENANT_ID)" -kvs "$(AZURE_CLIENT_SECRET)" -kvc "$(AZURE_CERT_NAME)" -tr http://timestamp.globalsign.com/tsa/r6advanced1 -v ".\cortex-cpp\engines\cortex.llamacpp\engine.dll";'
else ifeq ($(shell uname -s),Linux)
@echo "Skipping Code Sign for linux"
@exit 0
Expand Down Expand Up @@ -102,4 +103,5 @@ ifeq ($(OS),Windows_NT)
else ifeq ($(shell uname -s),Linux)
@rm -rf build; rm -rf build-deps; rm -rf cortex-cpp; rm -rf cortex-cpp.tar.gz;
else
@rm -rf build; rm -rf build-deps; rm -rf cortex-cpp; rm -rf cortex-cpp.tar.gz;
@rm -rf build; rm -rf build-deps; rm -rf cortex-cpp; rm -rf cortex-cpp.tar.gz;
endif
3 changes: 3 additions & 0 deletions cortex-js/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ export const databaseFile = `${databaseName}.db`;

export const defaultCortexJsHost = 'localhost';
export const defaultCortexJsPort = 7331;

export const defaultCortexCppHost = '127.0.0.1';
export const defaultCortexCppPort = 3928;
8 changes: 3 additions & 5 deletions cortex-js/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,26 @@ import { DevtoolsModule } from '@nestjs/devtools-integration';
import { DatabaseModule } from './infrastructure/database/database.module';
import { ChatModule } from './usecases/chat/chat.module';
import { AssistantsModule } from './usecases/assistants/assistants.module';
import { InferenceSettingsModule } from './usecases/inference-settings/inference-settings.module';
import { ExtensionModule } from './infrastructure/repositories/extensions/extension.module';
import { CortexModule } from './usecases/cortex/cortex.module';
import { ConfigModule } from '@nestjs/config';
import { env } from 'node:process';

@Module({
imports: [
DevtoolsModule.register({
http: process.env.NODE_ENV !== 'production',
http: env.NODE_ENV !== 'production',
}),
ConfigModule.forRoot({
isGlobal: true,
envFilePath:
process.env.NODE_ENV === 'production' ? '.env' : '.env.development',
envFilePath: env.NODE_ENV !== 'production' ? '.env.development' : '.env',
}),
DatabaseModule,
MessagesModule,
ThreadsModule,
ModelsModule,
ChatModule,
AssistantsModule,
InferenceSettingsModule,
CortexModule,
ExtensionModule,
],
Expand Down
20 changes: 16 additions & 4 deletions cortex-js/src/command.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import { DatabaseModule } from './infrastructure/database/database.module';
import { ConfigModule } from '@nestjs/config';
import { CortexModule } from './usecases/cortex/cortex.module';
import { ServeCommand } from './infrastructure/commanders/serve.command';
import { PullCommand } from './infrastructure/commanders/pull.command';
import { InferenceCommand } from './infrastructure/commanders/inference.command';
import { ChatCommand } from './infrastructure/commanders/chat.command';
import { ModelsCommand } from './infrastructure/commanders/models.command';
import { StartCommand } from './infrastructure/commanders/start.command';
import { ExtensionModule } from './infrastructure/repositories/extensions/extension.module';
import { ChatModule } from './usecases/chat/chat.module';
import { InitCommand } from './infrastructure/commanders/init.command';
import { HttpModule } from '@nestjs/axios';
import { CreateInitQuestions } from './infrastructure/commanders/inquirer/init.questions';
import { ModelStartCommand } from './infrastructure/commanders/models/model-start.command';
import { ModelStopCommand } from './infrastructure/commanders/models/model-stop.command';
import { ModelListCommand } from './infrastructure/commanders/models/model-list.command';
import { ModelGetCommand } from './infrastructure/commanders/models/model-get.command';
import { ModelRemoveCommand } from './infrastructure/commanders/models/model-remove.command';
import { ModelPullCommand } from './infrastructure/commanders/models/model-pull.command';

@Module({
imports: [
Expand All @@ -32,12 +37,19 @@ import { CreateInitQuestions } from './infrastructure/commanders/inquirer/init.q
providers: [
BasicCommand,
ModelsCommand,
PullCommand,
ServeCommand,
InferenceCommand,
ChatCommand,
StartCommand,
InitCommand,
CreateInitQuestions,

// Model commands
ModelStartCommand,
ModelStopCommand,
ModelListCommand,
ModelGetCommand,
ModelRemoveCommand,
ModelPullCommand,
],
})
export class CommandModule {}
8 changes: 6 additions & 2 deletions cortex-js/src/domain/abstracts/engine.abstract.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Model } from '../models/model.interface';
/* eslint-disable no-unused-vars, @typescript-eslint/no-unused-vars */
import { Model, ModelSettingParams } from '../models/model.interface';
import { Extension } from './extension.abstract';

export abstract class EngineExtension extends Extension {
abstract provider: string;

abstract inference(completion: any, req: any, stream: any, res?: any): void;

async loadModel(model: Model): Promise<void> {}
async loadModel(
model: Model,
settingParams?: ModelSettingParams,
): Promise<void> {}

async unloadModel(modelId: string): Promise<void> {}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { RootCommand, CommandRunner, Option } from 'nest-commander';
import { PullCommand } from './pull.command';
import { ServeCommand } from './serve.command';
import { InferenceCommand } from './inference.command';
import { ChatCommand } from './chat.command';
import { ModelsCommand } from './models.command';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { defaultCortexJsHost, defaultCortexJsPort } from 'constant';
import { InitCommand } from './init.command';
import { StartCommand } from './start.command';

@RootCommand({
subCommands: [
ModelsCommand,
PullCommand,
ServeCommand,
InferenceCommand,
ChatCommand,
InitCommand,
StartCommand,
],
})
export class BasicCommand extends CommandRunner {
Expand All @@ -26,15 +26,17 @@ export class BasicCommand extends CommandRunner {

switch (command) {
case 'start':
const host = options?.host || defaultCortexJsHost;
const port = options?.port || defaultCortexJsPort;
const host = options?.host || 'localhost';
const port = options?.port || 3928;
return this.cortexUsecases
.startCortex(host, port)
.then((e) => console.log(e));

case 'stop':
return this.cortexUsecases
.stopCortex(defaultCortexJsHost, defaultCortexJsPort)
.then((e) => console.log(e));

default:
console.error(`Command ${command} is not supported`);
return;
Expand Down
15 changes: 15 additions & 0 deletions cortex-js/src/infrastructure/commanders/chat.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ChatUsecases } from '@/usecases/chat/chat.usecases';
import { CommandRunner, SubCommand } from 'nest-commander';
import { ChatCliUsecases } from './usecases/chat.cli.usecases';

@SubCommand({ name: 'chat' })
export class ChatCommand extends CommandRunner {
constructor(private readonly chatUsecases: ChatUsecases) {
super();
}

async run(input: string[]): Promise<void> {
const chatCliService = new ChatCliUsecases(this.chatUsecases);
return chatCliService.run(input);
}
}
11 changes: 6 additions & 5 deletions cortex-js/src/infrastructure/commanders/init.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { resolve } from 'path';
import { HttpService } from '@nestjs/axios';
import { Presets, SingleBar } from 'cli-progress';
import decompress from 'decompress';
import { exit } from 'node:process';

@SubCommand({ name: 'init', aliases: ['setup'] })
export class InitCommand extends CommandRunner {
Expand Down Expand Up @@ -41,7 +42,7 @@ export class InitCommand extends CommandRunner {

if (!res?.data) {
console.log('Failed to fetch releases');
process.exit(1);
exit(1);
}

let release = res?.data;
Expand All @@ -56,7 +57,7 @@ export class InitCommand extends CommandRunner {

if (!toDownloadAsset) {
console.log(`Could not find engine file ${engineFileName}`);
process.exit(1);
exit(1);
}

console.log(`Downloading engine file ${engineFileName}`);
Expand Down Expand Up @@ -107,9 +108,9 @@ export class InitCommand extends CommandRunner {
);
} catch (e) {
console.log(e);
process.exit(1);
exit(1);
}
process.exit(0);
exit(0);
};

parseEngineFileName = (options: {
Expand All @@ -128,7 +129,7 @@ export class InitCommand extends CommandRunner {
const cudaVersion =
options.runMode === 'GPU'
? options.gpuType === 'Nvidia'
? '-cuda-' + (options.cudaVersion === '11' ? '11.7' : '12.2')
? '-cuda-' + (options.cudaVersion === '11' ? '11-7' : '12-0')
: '-vulkan'
: '';
const instructions = options.instructions ? `-${options.instructions}` : '';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Question, QuestionSet } from 'nest-commander';
import { platform } from 'node:process';

@QuestionSet({ name: 'create-init-questions' })
export class CreateInitQuestions {
Expand All @@ -8,7 +9,7 @@ export class CreateInitQuestions {
name: 'runMode',
default: 'CPU',
choices: ['CPU', 'GPU'],
when: () => process.platform !== 'darwin',
when: () => platform !== 'darwin',
})
parseRunMode(val: string) {
return val;
Expand All @@ -31,7 +32,7 @@ export class CreateInitQuestions {
message: 'Select CPU instructions set',
name: 'instructions',
choices: ['AVX2', 'AVX', 'AVX-512'],
when: () => process.platform !== 'darwin',
when: () => platform !== 'darwin',
})
parseContent(val: string) {
return val;
Expand Down
66 changes: 18 additions & 48 deletions cortex-js/src/infrastructure/commanders/models.command.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,22 @@
import { ModelsUsecases } from '@/usecases/models/models.usecases';
import { CommandRunner, SubCommand } from 'nest-commander';
import { PullCommand } from './pull.command';
import { StartCommand } from './start.command';
import { ModelStartCommand } from './models/model-start.command';
import { ModelGetCommand } from './models/model-get.command';
import { ModelListCommand } from './models/model-list.command';
import { ModelStopCommand } from './models/model-stop.command';
import { ModelPullCommand } from './models/model-pull.command';
import { ModelRemoveCommand } from './models/model-remove.command';

@SubCommand({ name: 'models', subCommands: [PullCommand, StartCommand] })
@SubCommand({
name: 'models',
subCommands: [
ModelPullCommand,
ModelStartCommand,
ModelStopCommand,
ModelListCommand,
ModelGetCommand,
ModelRemoveCommand,
],
})
export class ModelsCommand extends CommandRunner {
constructor(private readonly modelsUsecases: ModelsUsecases) {
super();
}

async run(input: string[]): Promise<void> {
const command = input[0];
const modelId = input[1];

if (command !== 'list') {
if (!modelId) {
console.log('Model ID is required');
return;
}
}

switch (command) {
case 'list':
this.modelsUsecases.findAll().then(console.log);
return;
case 'get':
this.modelsUsecases.findOne(modelId).then(console.log);
return;
case 'remove':
this.modelsUsecases.remove(modelId).then(console.log);
return;

case 'stop':
return this.modelsUsecases
.stopModel(modelId)
.then(console.log)
.catch(console.error);

case 'stats':
case 'fetch':
case 'build': {
console.log('Command is not supported yet');
return;
}

default:
console.error(`Command ${command} is not supported`);
return;
}
}
async run(): Promise<void> {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ModelsUsecases } from '@/usecases/models/models.usecases';
import { CommandRunner, SubCommand } from 'nest-commander';
import { ModelsCliUsecases } from '../usecases/models.cli.usecases';
import { exit } from 'node:process';

@SubCommand({ name: 'get' })
export class ModelGetCommand extends CommandRunner {
constructor(private readonly modelsUsecases: ModelsUsecases) {
super();
}

async run(input: string[]): Promise<void> {
if (input.length === 0) {
console.error('Model ID is required');
exit(1);
}

const modelsCliUsecases = new ModelsCliUsecases(this.modelsUsecases);
const models = await modelsCliUsecases.getModel(input[0]);
console.log(models);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ModelsUsecases } from '@/usecases/models/models.usecases';
import { CommandRunner, SubCommand } from 'nest-commander';
import { ModelsCliUsecases } from '../usecases/models.cli.usecases';

@SubCommand({ name: 'list' })
export class ModelListCommand extends CommandRunner {
constructor(private readonly modelsUsecases: ModelsUsecases) {
super();
}

async run(): Promise<void> {
const modelsCliUsecases = new ModelsCliUsecases(this.modelsUsecases);
const models = await modelsCliUsecases.listAllModels();
console.log(models);
}
}
Loading

0 comments on commit e4b1f96

Please sign in to comment.