From 2783dc3032be35fa2c51f9969f355f8e24ed438a Mon Sep 17 00:00:00 2001 From: PylotLight Date: Mon, 30 Dec 2024 15:41:29 +1100 Subject: [PATCH] add dynamic import and optional dep --- libs/langchain-community/package.json | 3 ++ .../src/embeddings/hf_transformers.ts | 39 +++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/libs/langchain-community/package.json b/libs/langchain-community/package.json index 985282258cbe..4b32aa9abf36 100644 --- a/libs/langchain-community/package.json +++ b/libs/langchain-community/package.json @@ -713,6 +713,9 @@ "optional": true } }, + "optionalDependencies": { + "@xenova/transformers": "^2.17.2" + }, "publishConfig": { "access": "public" }, diff --git a/libs/langchain-community/src/embeddings/hf_transformers.ts b/libs/langchain-community/src/embeddings/hf_transformers.ts index 8996e477cd40..4eb14eca1896 100644 --- a/libs/langchain-community/src/embeddings/hf_transformers.ts +++ b/libs/langchain-community/src/embeddings/hf_transformers.ts @@ -8,6 +8,7 @@ import { chunkArray } from "@langchain/core/utils/chunk_array"; export interface HuggingFaceTransformersEmbeddingsParams extends EmbeddingsParams { + /** * Model name to use * Alias for `model` @@ -124,13 +125,45 @@ export class HuggingFaceTransformersEmbeddings } private async runEmbedding(texts: string[]) { - const pipe = await (this.pipelinePromise ??= ( - await import("@huggingface/transformers") - ).pipeline("feature-extraction", this.model, this.pretrainedOptions)); + const pipe = await this.getPipeline(); return this.caller.call(async () => { const output = await pipe(texts, this.pipelineOptions); return output.tolist(); }); } + + private async getPipeline() { + if (!this.pipelinePromise) { + try { + // Try importing the new package + const { pipeline } = await import("@huggingface/transformers"); + this.pipelinePromise = pipeline( + "feature-extraction", + this.model, + this.pretrainedOptions + ); + } catch (e) { + console.warn( + "Failed to load @huggingface/transformers. Falling back to @xenova/transformers." + ); + + try { + // Fallback to the old package + const { pipeline } = await import("@xenova/transformers"); + this.pipelinePromise = pipeline( + "feature-extraction", + this.model, + this.pretrainedOptions + ); + } catch (fallbackError) { + throw new Error( + "Failed to load both @huggingface/transformers and @xenova/transformers. Ensure one of them is installed." + ); + } + } + } + return this.pipelinePromise; + } + }