-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' of https://github.com/janhq/cortex into readme
- Loading branch information
Showing
14 changed files
with
205 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { CommandRunner, SubCommand } from 'nest-commander'; | ||
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; | ||
|
||
@SubCommand({ | ||
name: 'kill', | ||
description: 'Kill running cortex processes', | ||
}) | ||
export class KillCommand extends CommandRunner { | ||
constructor(private readonly usecases: CortexUsecases) { | ||
super(); | ||
} | ||
async run(): Promise<void> { | ||
this.usecases.stopCortex().then(console.log); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { CommandRunner, SubCommand } from 'nest-commander'; | ||
import { PSCliUsecases } from './usecases/ps.cli.usecases'; | ||
|
||
@SubCommand({ | ||
name: 'ps', | ||
description: 'Show running models and their status', | ||
}) | ||
export class PSCommand extends CommandRunner { | ||
constructor(private readonly usecases: PSCliUsecases) { | ||
super(); | ||
} | ||
async run(): Promise<void> { | ||
return this.usecases.getModels().then(console.table); | ||
} | ||
} |
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
85 changes: 85 additions & 0 deletions
85
cortex-js/src/infrastructure/commanders/usecases/ps.cli.usecases.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,85 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { defaultCortexCppHost, defaultCortexCppPort } from 'constant'; | ||
|
||
interface ModelStat { | ||
modelId: string; | ||
engine?: string; | ||
duration?: string; | ||
status: string; | ||
vram?: string; | ||
ram?: string; | ||
} | ||
interface ModelStatResponse { | ||
object: string; | ||
data: any; | ||
} | ||
@Injectable() | ||
export class PSCliUsecases { | ||
/** | ||
* Get models running in the Cortex C++ server | ||
* @param host Cortex host address | ||
* @param port Cortex port address | ||
*/ | ||
async getModels( | ||
host: string = defaultCortexCppHost, | ||
port: number = defaultCortexCppPort, | ||
): Promise<ModelStat[]> { | ||
return new Promise<ModelStat[]>((resolve, reject) => | ||
fetch(`http://${host}:${port}/inferences/server/models`) | ||
.then((res) => { | ||
if (res.ok) { | ||
res | ||
.json() | ||
.then(({ data }: ModelStatResponse) => { | ||
if (data && Array.isArray(data) && data.length > 0) { | ||
resolve( | ||
data.map((e) => { | ||
const startTime = e.start_time ?? new Date(); | ||
const currentTime = new Date(); | ||
const duration = | ||
currentTime.getTime() - new Date(startTime).getTime(); | ||
return { | ||
modelId: e.id, | ||
engine: e.engine ?? 'llama.cpp', // TODO: get engine from model when it's ready | ||
status: 'running', | ||
duration: this.formatDuration(duration), | ||
ram: e.ram ?? '-', | ||
vram: e.vram ?? '-', | ||
}; | ||
}), | ||
); | ||
} else reject(); | ||
}) | ||
.catch(reject); | ||
} else reject(); | ||
}) | ||
.catch(reject), | ||
).catch(() => []); | ||
} | ||
|
||
private formatDuration(milliseconds: number): string { | ||
const days = Math.floor(milliseconds / (1000 * 60 * 60 * 24)); | ||
const hours = Math.floor( | ||
(milliseconds % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60), | ||
); | ||
const minutes = Math.floor((milliseconds % (1000 * 60 * 60)) / (1000 * 60)); | ||
const seconds = Math.floor((milliseconds % (1000 * 60)) / 1000); | ||
|
||
let formattedDuration = ''; | ||
|
||
if (days > 0) { | ||
formattedDuration += `${days}d `; | ||
} | ||
if (hours > 0) { | ||
formattedDuration += `${hours}h `; | ||
} | ||
if (minutes > 0) { | ||
formattedDuration += `${minutes}m `; | ||
} | ||
if (seconds > 0) { | ||
formattedDuration += `${seconds}s `; | ||
} | ||
|
||
return formattedDuration.trim(); | ||
} | ||
} |
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
Oops, something went wrong.