-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
226 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
cortex-js/src/infrastructure/commanders/init.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { createWriteStream, existsSync, rmSync } from 'fs'; | ||
import { CommandRunner, SubCommand, InquirerService } from 'nest-commander'; | ||
import { resolve } from 'path'; | ||
import { HttpService } from '@nestjs/axios'; | ||
import { Presets, SingleBar } from 'cli-progress'; | ||
import decompress from 'decompress'; | ||
|
||
@SubCommand({ name: 'init', aliases: ['setup'] }) | ||
export class InitCommand extends CommandRunner { | ||
CORTEX_RELEASES_URL = 'https://api.github.com/repos/janhq/cortex/releases'; | ||
|
||
constructor( | ||
private readonly httpService: HttpService, | ||
private readonly inquirerService: InquirerService, | ||
) { | ||
super(); | ||
} | ||
|
||
async run(input: string[], options?: any): Promise<void> { | ||
options = await this.inquirerService.ask('create-init-questions', options); | ||
const version = input[0] ?? 'latest'; | ||
|
||
await this.download(this.parseEngineFileName(options), version); | ||
} | ||
|
||
download = async ( | ||
engineFileName: string, | ||
version: string = 'latest', | ||
): Promise<any> => { | ||
const res = await this.httpService | ||
.get( | ||
this.CORTEX_RELEASES_URL + `${version === 'latest' ? '/latest' : ''}`, | ||
{ | ||
headers: { | ||
'X-GitHub-Api-Version': '2022-11-28', | ||
Accept: 'application/vnd.github+json', | ||
}, | ||
}, | ||
) | ||
.toPromise(); | ||
|
||
if (!res?.data) { | ||
console.log('Failed to fetch releases'); | ||
process.exit(1); | ||
} | ||
|
||
let release = res?.data; | ||
if (Array.isArray(res?.data)) { | ||
release = Array(res?.data)[0].find( | ||
(e) => e.name === version.replace('v', ''), | ||
); | ||
} | ||
const toDownloadAsset = release.assets.find((s: any) => | ||
s.name.includes(engineFileName), | ||
); | ||
|
||
if (!toDownloadAsset) { | ||
console.log(`Could not find engine file ${engineFileName}`); | ||
process.exit(1); | ||
} | ||
|
||
console.log(`Downloading engine file ${engineFileName}`); | ||
const engineDir = resolve(this.rootDir(), 'cortex-cpp'); | ||
if (existsSync(engineDir)) rmSync(engineDir, { recursive: true }); | ||
|
||
const download = await this.httpService | ||
.get(toDownloadAsset.browser_download_url, { | ||
responseType: 'stream', | ||
}) | ||
.toPromise(); | ||
if (!download) { | ||
throw new Error('Failed to download model'); | ||
} | ||
|
||
const destination = resolve(this.rootDir(), toDownloadAsset.name); | ||
|
||
await new Promise((resolve, reject) => { | ||
const writer = createWriteStream(destination); | ||
let receivedBytes = 0; | ||
const totalBytes = download.headers['content-length']; | ||
|
||
writer.on('finish', () => { | ||
bar.stop(); | ||
resolve(true); | ||
}); | ||
|
||
writer.on('error', (error) => { | ||
bar.stop(); | ||
reject(error); | ||
}); | ||
|
||
const bar = new SingleBar({}, Presets.shades_classic); | ||
bar.start(100, 0); | ||
|
||
download.data.on('data', (chunk: any) => { | ||
receivedBytes += chunk.length; | ||
bar.update(Math.floor((receivedBytes / totalBytes) * 100)); | ||
}); | ||
|
||
download.data.pipe(writer); | ||
}); | ||
|
||
try { | ||
await decompress( | ||
resolve(this.rootDir(), destination), | ||
resolve(this.rootDir()), | ||
); | ||
} catch (e) { | ||
console.log(e); | ||
process.exit(1); | ||
} | ||
process.exit(0); | ||
}; | ||
|
||
parseEngineFileName = (options: { | ||
runMode?: 'CPU' | 'GPU'; | ||
gpuType?: 'Nvidia' | 'Others (Vulkan)'; | ||
instructions?: 'AVX' | 'AVX2' | 'AVX-512' | undefined; | ||
cudaVersion?: '11' | '12'; | ||
}) => { | ||
const platform = | ||
process.platform === 'win32' | ||
? 'windows' | ||
: process.platform === 'darwin' | ||
? 'mac' | ||
: process.platform; | ||
const arch = process.arch === 'arm64' ? process.arch : 'amd64'; | ||
const cudaVersion = | ||
options.runMode === 'GPU' | ||
? options.gpuType === 'Nvidia' | ||
? '-cuda-' + (options.cudaVersion === '11' ? '11.7' : '12.2') | ||
: '-vulkan' | ||
: ''; | ||
const instructions = options.instructions ? `-${options.instructions}` : ''; | ||
const engineName = `${platform}-${arch}${instructions.toLowerCase()}${cudaVersion}`; | ||
return `${engineName}.tar.gz`; | ||
}; | ||
|
||
rootDir = () => resolve(__dirname, `../../../`); | ||
} |
39 changes: 39 additions & 0 deletions
39
cortex-js/src/infrastructure/commanders/inquirer/init.questions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Question, QuestionSet } from 'nest-commander'; | ||
|
||
@QuestionSet({ name: 'create-init-questions' }) | ||
export class CreateInitQuestions { | ||
@Question({ | ||
type: 'list', | ||
message: 'Select run mode', | ||
name: 'runMode', | ||
default: 'CPU', | ||
choices: ['CPU', 'GPU'], | ||
when: () => process.platform !== 'darwin', | ||
}) | ||
parseRunMode(val: string) { | ||
return val; | ||
} | ||
|
||
@Question({ | ||
type: 'list', | ||
message: 'Select GPU type', | ||
name: 'gpuType', | ||
default: 'Nvidia', | ||
choices: ['Nvidia', 'Others (Vulkan)'], | ||
when: (answers: any) => answers.runMode === 'GPU', | ||
}) | ||
parseGPUType(val: string) { | ||
return val; | ||
} | ||
|
||
@Question({ | ||
type: 'list', | ||
message: 'Select CPU instructions set', | ||
name: 'instructions', | ||
choices: ['AVX2', 'AVX', 'AVX-512'], | ||
when: () => process.platform !== 'darwin', | ||
}) | ||
parseContent(val: string) { | ||
return val; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters