Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: watch models and engines update for proper data retrieval #960

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cortex-js/src/extensions/anthropic.engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class AnthropicEngineExtension extends OAIEngineExtension {
const system = data.messages.find((m: any) => m.role === 'system');
const messages = data.messages.filter((m: any) => m.role !== 'system');
return {
system: system.content,
system: system?.content ?? '',
messages,
...pick(data, ['model', 'stream', 'max_tokens']),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Extension } from '@/domain/abstracts/extension.abstract';
import { readdir, lstat } from 'fs/promises';
import { join } from 'path';
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
import { existsSync } from 'fs';
import { existsSync, mkdirSync, watch } from 'fs';
import { Engines } from '@/infrastructure/commanders/types/engine.interface';
import { OAIEngineExtension } from '@/domain/abstracts/oai.abstract';
import { HttpService } from '@nestjs/axios';
Expand All @@ -27,6 +27,16 @@ export class ExtensionRepositoryImpl implements ExtensionRepository {
) {
this.loadCoreExtensions();
this.loadExternalExtensions();

// Watch engine folder only for now
fileService.getCortexCppEnginePath().then((path) => {
if (!existsSync(path)) mkdirSync(path);
watch(path, (eventType, filename) => {
this.extensions.clear();
this.loadCoreExtensions();
this.loadExternalExtensions();
});
});
}
/**
* Persist extension to the extensions map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
readdirSync,
rmSync,
writeFileSync,
watch,
} from 'fs';
import { load, dump } from 'js-yaml';
import { isLocalModel, normalizeModelId } from '@/utils/normalize-model-id';
Expand All @@ -25,6 +26,12 @@ export class ModelRepositoryImpl implements ModelRepository {

constructor(private readonly fileService: FileManagerService) {
this.loadModels();
fileService.getModelsPath().then((path) => {
if (!existsSync(path)) mkdirSync(path);
watch(path, (eventType, filename) => {
this.loadModels(true);
});
});
}

/**
Expand Down Expand Up @@ -121,11 +128,14 @@ export class ModelRepositoryImpl implements ModelRepository {
* This would load all the models from the models folder
* @returns the list of models
*/
private async loadModels(): Promise<Model[]> {
if (this.loaded) return Array.from(this.models.values());
private async loadModels(forceReload: boolean = false): Promise<Model[]> {
if (this.loaded && !forceReload) return Array.from(this.models.values());
const modelsPath =
process.env.EXTENSIONS_PATH ?? (await this.fileService.getModelsPath());

this.models.clear();
this.fileModel.clear();

if (!existsSync(modelsPath)) return [];

const modelFiles = readdirSync(modelsPath)
Expand Down
Loading