Skip to content

Commit

Permalink
add dynamic import and optional dep
Browse files Browse the repository at this point in the history
  • Loading branch information
PylotLight committed Dec 30, 2024
1 parent b9f2d71 commit 2783dc3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
3 changes: 3 additions & 0 deletions libs/langchain-community/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,9 @@
"optional": true
}
},
"optionalDependencies": {
"@xenova/transformers": "^2.17.2"
},
"publishConfig": {
"access": "public"
},
Expand Down
39 changes: 36 additions & 3 deletions libs/langchain-community/src/embeddings/hf_transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { chunkArray } from "@langchain/core/utils/chunk_array";

export interface HuggingFaceTransformersEmbeddingsParams
extends EmbeddingsParams {

/**
* Model name to use
* Alias for `model`
Expand Down Expand Up @@ -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;
}

}

0 comments on commit 2783dc3

Please sign in to comment.