Skip to content

Commit bb3148f

Browse files
[autofix.ci] apply automated fixes
1 parent ad74c49 commit bb3148f

File tree

6 files changed

+11952
-14794
lines changed

6 files changed

+11952
-14794
lines changed

backend/src/config/config-loader.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { getConfigPath } from './common-path';
66
import { ConfigType } from 'src/downloader/universal-utils';
77
import { Logger } from '@nestjs/common';
88

9-
109
export interface ModelConfig {
1110
model: string;
1211
endpoint?: string;
@@ -55,7 +54,6 @@ export const exampleConfigContent = `{
5554
}]
5655
}`;
5756

58-
5957
export class ConfigLoader {
6058
readonly logger = new Logger(ConfigLoader.name);
6159
private type: string;
@@ -69,7 +67,10 @@ export class ConfigLoader {
6967
this.loadConfig();
7068
}
7169

72-
public static getInstance(type: ConfigType, configPath?: string): ConfigLoader {
70+
public static getInstance(
71+
type: ConfigType,
72+
configPath?: string,
73+
): ConfigLoader {
7374
if (!ConfigLoader.instances.has(type)) {
7475
ConfigLoader.instances.set(type, new ConfigLoader(type, configPath));
7576
}
@@ -113,7 +114,7 @@ export class ConfigLoader {
113114
throw error;
114115
}
115116
}
116-
117+
117118
this.logger.log(ConfigLoader.config);
118119
}
119120

@@ -182,19 +183,21 @@ export class ConfigLoader {
182183
}
183184

184185
getAllConfigs(): EmbeddingConfig[] | ModelConfig[] | null {
185-
let res = ConfigLoader.config[this.type];
186+
const res = ConfigLoader.config[this.type];
186187
return Array.isArray(res) ? res : null;
187188
}
188189

189-
getConfig(name: string): EmbeddingConfig | ModelConfig | null{
190+
getConfig(name: string): EmbeddingConfig | ModelConfig | null {
190191
const res = ConfigLoader.config[this.type];
191-
192-
if (!Array.isArray(res)) {
193-
return null;
194-
}
195192

196-
const config = res.find((item: EmbeddingConfig | ModelConfig) => item.model === name);
197-
return config || null;
193+
if (!Array.isArray(res)) {
194+
return null;
195+
}
196+
197+
const config = res.find(
198+
(item: EmbeddingConfig | ModelConfig) => item.model === name,
199+
);
200+
return config || null;
198201
}
199202

200203
validateConfig() {
@@ -219,7 +222,9 @@ export class ConfigLoader {
219222
}
220223
});
221224

222-
const defaultChats = ConfigLoader.config.models.filter((chat) => chat.default);
225+
const defaultChats = ConfigLoader.config.models.filter(
226+
(chat) => chat.default,
227+
);
223228
if (defaultChats.length > 1) {
224229
throw new Error(
225230
'Invalid configuration: Multiple default chat configurations found',
@@ -228,7 +233,7 @@ export class ConfigLoader {
228233
}
229234

230235
if (ConfigLoader.config[ConfigType.EMBEDDINGS]) {
231-
this.logger.log(ConfigLoader.config[ConfigType.EMBEDDINGS])
236+
this.logger.log(ConfigLoader.config[ConfigType.EMBEDDINGS]);
232237
if (!Array.isArray(ConfigLoader.config[ConfigType.EMBEDDINGS])) {
233238
throw new Error("Invalid configuration: 'embeddings' must be an array");
234239
}
@@ -241,7 +246,9 @@ export class ConfigLoader {
241246
}
242247
});
243248

244-
const defaultChats = ConfigLoader.config[ConfigType.EMBEDDINGS].filter((chat) => chat.default);
249+
const defaultChats = ConfigLoader.config[ConfigType.EMBEDDINGS].filter(
250+
(chat) => chat.default,
251+
);
245252
if (defaultChats.length > 1) {
246253
throw new Error(
247254
'Invalid configuration: Multiple default emb configurations found',

backend/src/downloader/__tests__/loadAllChatsModels.spec.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import path from 'path';
22
import * as fs from 'fs';
3-
import { ConfigLoader, ModelConfig, EmbeddingConfig } from '../../config/config-loader';
3+
import {
4+
ConfigLoader,
5+
ModelConfig,
6+
EmbeddingConfig,
7+
} from '../../config/config-loader';
48
import { UniversalDownloader } from '../universal-downloader';
59
import { ConfigType, downloadAll } from '../universal-utils';
610
import { getConfigDir, getConfigPath } from 'src/config/common-path';
@@ -39,11 +43,11 @@ describe('loadAllChatsModels with real model loading', () => {
3943
let modelConfigLoader: ConfigLoader;
4044
let embConfigLoader: ConfigLoader;
4145
beforeAll(async () => {
42-
ConfigLoader.initConfigFile(getConfigPath("config"));
46+
ConfigLoader.initConfigFile(getConfigPath('config'));
4347
modelConfigLoader = ConfigLoader.getInstance(ConfigType.CHATS);
4448
embConfigLoader = ConfigLoader.getInstance(ConfigType.EMBEDDINGS);
4549
const modelConfig: ModelConfig = {
46-
model: "Felladrin/onnx-flan-alpaca-base",
50+
model: 'Felladrin/onnx-flan-alpaca-base',
4751
endpoint: 'http://localhost:11434/v1',
4852
token: 'your-token-here',
4953
default: true, // Set to true if it's the default model

backend/src/downloader/universal-downloader.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@ export class UniversalDownloader {
88
private static instance: UniversalDownloader;
99
public static getInstance(): UniversalDownloader {
1010
if (!UniversalDownloader.instance) {
11-
UniversalDownloader.instance = new UniversalDownloader();
11+
UniversalDownloader.instance = new UniversalDownloader();
1212
}
1313
return UniversalDownloader.instance;
1414
}
1515

1616
async getPipeline(task: string, model: string, path: string): Promise<any> {
17-
try{
18-
const pipelineInstance = await pipeline(task as PipelineType, model, {
19-
cache_dir: path,
20-
});
21-
return pipelineInstance;
22-
}catch(error){
23-
this.logger.log(`Download: ${model} failed with error: ${error.message || error}`)
24-
return null;
25-
}
17+
try {
18+
const pipelineInstance = await pipeline(task as PipelineType, model, {
19+
cache_dir: path,
20+
});
21+
return pipelineInstance;
22+
} catch (error) {
23+
this.logger.log(
24+
`Download: ${model} failed with error: ${error.message || error}`,
25+
);
26+
return null;
27+
}
2628
}
2729

2830
public async getLocalModel(task: string, model: string): Promise<any> {
@@ -33,7 +35,9 @@ export class UniversalDownloader {
3335
});
3436
return pipelineInstance;
3537
} catch (error) {
36-
this.logger.error(`Failed to load local model: ${model} with error: ${error.message || error}`);
38+
this.logger.error(
39+
`Failed to load local model: ${model} with error: ${error.message || error}`,
40+
);
3741
throw null;
3842
}
3943
}

backend/src/downloader/universal-status.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33
import { getConfigPath } from 'src/config/common-path';
4-
4+
55
export interface UniversalStatus {
66
isDownloaded: boolean;
77
lastChecked: Date;
@@ -20,7 +20,10 @@ export class UniversalStatusManager {
2020
public static getInstance(type: string): UniversalStatusManager {
2121
if (!UniversalStatusManager.instances.has(type)) {
2222
const statusFilePath = getConfigPath(`${type}-status`);
23-
UniversalStatusManager.instances.set(type, new UniversalStatusManager(statusFilePath));
23+
UniversalStatusManager.instances.set(
24+
type,
25+
new UniversalStatusManager(statusFilePath),
26+
);
2427
}
2528
return UniversalStatusManager.instances.get(type)!;
2629
}

backend/src/downloader/universal-utils.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { UniversalDownloader } from './universal-downloader';
33
import { Logger } from '@nestjs/common';
44
import { UniversalStatusManager } from './universal-status';
5-
import {ModelConfig} from '../config/config-loader';
5+
import { ModelConfig } from '../config/config-loader';
66
import { ConfigLoader } from 'src/config/config-loader';
77
import { getEmbDir, getModelsDir } from 'src/config/common-path';
88

@@ -12,20 +12,20 @@ export enum ConfigType {
1212
EMBEDDINGS = 'embeddings',
1313
CHATS = 'models',
1414
}
15-
export enum TaskType{
16-
CHAT = "text2text-generation",
17-
EMBEDDING = "feature-extraction",
15+
export enum TaskType {
16+
CHAT = 'text2text-generation',
17+
EMBEDDING = 'feature-extraction',
1818
}
1919
export async function downloadAllConfig(): Promise<void> {
2020
downloadAll(ConfigType.CHATS);
2121
downloadAll(ConfigType.EMBEDDINGS);
22-
2322
}
2423
export async function downloadAll(type: ConfigType): Promise<void> {
2524
const configLoader = ConfigLoader.getInstance(type);
2625
const statusManager = UniversalStatusManager.getInstance(type);
2726
const downloader = UniversalDownloader.getInstance();
28-
let storePath: string = (type == ConfigType.EMBEDDINGS) ? getEmbDir() : getModelsDir();
27+
const storePath: string =
28+
type == ConfigType.EMBEDDINGS ? getEmbDir() : getModelsDir();
2929

3030
const chatConfigs = configLoader.getAllConfigs();
3131
logger.log('Loaded chat configurations:', chatConfigs);
@@ -47,7 +47,11 @@ export async function downloadAll(type: ConfigType): Promise<void> {
4747

4848
try {
4949
logger.log(`Downloading model: ${model} for task: ${task || 'chat'}`);
50-
await downloader.getPipeline(task || 'text2text-generation', model, storePath);
50+
await downloader.getPipeline(
51+
task || 'text2text-generation',
52+
model,
53+
storePath,
54+
);
5155

5256
statusManager.updateStatus(model, true);
5357
logger.log(`Successfully downloaded model: ${model}`);
@@ -67,17 +71,22 @@ export async function downloadAll(type: ConfigType): Promise<void> {
6771
}
6872
}
6973

70-
export async function downloadModel(type: ConfigType, modelName: string): Promise<void> {
74+
export async function downloadModel(
75+
type: ConfigType,
76+
modelName: string,
77+
): Promise<void> {
7178
const configLoader = ConfigLoader.getInstance(type);
7279
const statusManager = UniversalStatusManager.getInstance(type);
73-
const downloader = UniversalDownloader.getInstance();
74-
80+
const downloader = UniversalDownloader.getInstance();
7581

7682
const config = configLoader.getConfig(modelName);
7783
if (!config) {
7884
throw new Error(`Model configuration not found for: ${modelName}`);
7985
}
80-
const task = type === ConfigType.EMBEDDINGS ? TaskType.EMBEDDING : (config as ModelConfig).task || TaskType.CHAT;
86+
const task =
87+
type === ConfigType.EMBEDDINGS
88+
? TaskType.EMBEDDING
89+
: (config as ModelConfig).task || TaskType.CHAT;
8190
const path = type === ConfigType.EMBEDDINGS ? getEmbDir() : getModelsDir();
8291
try {
8392
logger.log(`Downloading model: ${modelName}`);

0 commit comments

Comments
 (0)