diff --git a/cortex-js/src/infrastructure/commanders/models/model-start.command.ts b/cortex-js/src/infrastructure/commanders/models/model-start.command.ts index 7c9268031..0e4aabae2 100644 --- a/cortex-js/src/infrastructure/commanders/models/model-start.command.ts +++ b/cortex-js/src/infrastructure/commanders/models/model-start.command.ts @@ -4,7 +4,7 @@ import { exit } from 'node:process'; import { CortexUsecases } from '@/usecases/cortex/cortex.usecases'; import { SetCommandContext } from '../decorators/CommandContext'; import { ContextService } from '@/infrastructure/services/context/context.service'; -import { createReadStream, existsSync, statSync, watchFile } from 'node:fs'; +import { existsSync } from 'node:fs'; import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service'; import { join } from 'node:path'; import { Engines } from '../types/engine.interface'; @@ -14,11 +14,12 @@ import { isRemoteEngine } from '@/utils/normalize-model-id'; import { downloadProgress } from '@/utils/download-progress'; import { CortexClient } from '../services/cortex.client'; import { DownloadType } from '@/domain/models/download.interface'; +import { printLastErrorLines } from '@/utils/logs'; type ModelStartOptions = { - attach: boolean; preset?: string; }; + @SubCommand({ name: 'start', description: 'Start a model by ID.', @@ -77,16 +78,17 @@ export class ModelStartCommand extends BaseCommand { await downloadProgress(this.cortex, undefined, DownloadType.Engine); } - // Attached - stdout logs - if (options.attach) { - this.attachLogWatch(); - } - const parsedPreset = await this.fileService.getPreset(options.preset); + const startingSpinner = ora('Loading model...').start(); + await this.cortex.models .start(modelId, parsedPreset) - .then(() => options.attach && ora('Model is running...').start()); + .then(() => startingSpinner.succeed('Model loaded')) + .catch(async (error) => { + startingSpinner.fail(error.message ?? error); + printLastErrorLines(await this.fileService.getLogPath()); + }); } modelInquiry = async () => { @@ -104,16 +106,6 @@ export class ModelStartCommand extends BaseCommand { return model; }; - @Option({ - flags: '-a, --attach', - description: 'Attach to interactive chat session', - defaultValue: false, - name: 'attach', - }) - parseAttach() { - return true; - } - @Option({ flags: '-p, --preset ', description: 'Apply a chat preset to the chat session', @@ -121,38 +113,4 @@ export class ModelStartCommand extends BaseCommand { parseTemplate(value: string) { return value; } - - /** - * Attach to the log file and watch for changes - */ - private async attachLogWatch() { - const logPath = await this.fileService.getLogPath(); - const initialSize = statSync(logPath).size; - const logStream = createReadStream(logPath, { - start: initialSize, - encoding: 'utf-8', - autoClose: false, - }); - logStream.on('data', (chunk) => { - console.log(chunk); - }); - watchFile(logPath, (curr, prev) => { - // Check if the file size has increased - if (curr.size > prev.size) { - // Calculate the position to start reading from - const position = prev.size; - - // Create a new read stream from the updated position - const updateStream = createReadStream(logPath, { - encoding: 'utf8', - start: position, - }); - - // Read the newly written content - updateStream.on('data', (chunk) => { - console.log(chunk); - }); - } - }); - } } diff --git a/cortex-js/src/infrastructure/commanders/run.command.ts b/cortex-js/src/infrastructure/commanders/run.command.ts index 086e6f701..1dcd88789 100644 --- a/cortex-js/src/infrastructure/commanders/run.command.ts +++ b/cortex-js/src/infrastructure/commanders/run.command.ts @@ -15,6 +15,7 @@ import { CortexClient } from './services/cortex.client'; import { DownloadType } from '@/domain/models/download.interface'; import { isLocalFile } from '@/utils/urls'; import { parse } from 'node:path'; +import { printLastErrorLines } from '@/utils/logs'; type RunOptions = { threadId?: string; @@ -109,8 +110,10 @@ export class RunCommand extends BaseCommand { if (options.chat) this.chatClient.chat(modelId, options.threadId); else console.log("To start a chat session, use the '--chat' flag"); }) - .catch((e) => { + .catch(async (e) => { startingSpinner.fail(e.message ?? e); + + printLastErrorLines(await this.fileService.getLogPath()); }); } diff --git a/cortex-js/src/utils/huggingface.ts b/cortex-js/src/utils/huggingface.ts index 27908f0f3..9df80eeeb 100644 --- a/cortex-js/src/utils/huggingface.ts +++ b/cortex-js/src/utils/huggingface.ts @@ -225,9 +225,9 @@ export async function getHFModelMetadata( const stopWord: string = metadata['tokenizer.ggml.tokens'][index] ?? ''; const name = metadata['general.name']; const contextLength = metadata['llama.context_length'] ?? 4096; - const ngl = (metadata['llama.block_count'] ?? 32) + 1 + const ngl = (metadata['llama.block_count'] ?? 32) + 1; const version: number = metadata['version']; - + return { contextLength, ngl,