@@ -9,21 +9,32 @@ type TransformersPipeline = (texts: string[], options: Record<string, unknown>)
99
1010export class Embedder {
1111 private static pipeline : TransformersPipeline | null = null ;
12+ private static pipelinePromise : Promise < TransformersPipeline > | null = null ;
1213
1314 readonly dimensions = DEFAULT_DIMENSIONS ;
1415
1516 private static async getPipeline ( ) : Promise < TransformersPipeline > {
1617 if ( Embedder . pipeline ) return Embedder . pipeline ;
1718
18- const mod = await import ( '@huggingface/transformers' ) ;
19- const hfEndpoint = process . env . HF_ENDPOINT ;
20- if ( hfEndpoint && ( mod as any ) . env ) {
21- ( mod as any ) . env . remoteHost = hfEndpoint ;
19+ if ( ! Embedder . pipelinePromise ) {
20+ Embedder . pipelinePromise = ( async ( ) => {
21+ const mod = await import ( '@huggingface/transformers' ) ;
22+ const hfEndpoint = process . env . HF_ENDPOINT ;
23+ if ( hfEndpoint && ( mod as any ) . env ) {
24+ ( mod as any ) . env . remoteHost = hfEndpoint ;
25+ }
26+
27+ const pipe = await mod . pipeline ( 'feature-extraction' , DEFAULT_MODEL_ID ) as TransformersPipeline ;
28+ Embedder . pipeline = pipe ;
29+ return Embedder . pipeline ;
30+ } ) ( ) . catch ( ( err ) => {
31+ Embedder . pipeline = null ;
32+ Embedder . pipelinePromise = null ;
33+ throw err ;
34+ } ) ;
2235 }
2336
24- const pipe = await mod . pipeline ( 'feature-extraction' , DEFAULT_MODEL_ID ) as TransformersPipeline ;
25- Embedder . pipeline = pipe ;
26- return Embedder . pipeline ;
37+ return Embedder . pipelinePromise ;
2738 }
2839
2940 async embed ( text : string ) : Promise < number [ ] > {
0 commit comments