-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathembedding-downloader.ts
39 lines (36 loc) · 1.25 KB
/
embedding-downloader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { Logger } from '@nestjs/common';
import { UniversalStatusManager } from './universal-status';
import { EmbeddingModel, FlagEmbedding } from 'fastembed';
import { getEmbDir } from 'codefox-common';
export class EmbeddingDownloader {
readonly logger = new Logger(EmbeddingDownloader.name);
private static instance: EmbeddingDownloader;
private readonly statusManager = UniversalStatusManager.getInstance();
public static getInstance(): EmbeddingDownloader {
if (!EmbeddingDownloader.instance) {
EmbeddingDownloader.instance = new EmbeddingDownloader();
}
return EmbeddingDownloader.instance;
}
async getPipeline(model: string): Promise<any> {
if (!Object.values(EmbeddingModel).includes(model as EmbeddingModel)) {
this.logger.error(
`Invalid model: ${model} is not a valid EmbeddingModel.`,
);
return null;
}
try {
const embeddingModel = await FlagEmbedding.init({
model: model as EmbeddingModel,
cacheDir: getEmbDir(),
});
this.statusManager.updateStatus(model, true);
return embeddingModel;
} catch (error) {
this.logger.error(
`Failed to load local model: ${model} with error: ${error.message || error}`,
);
return null;
}
}
}