Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix [ERR_REQUIRE_ESM] error when importing hf_transformers #5746

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions environment_tests/test-exports-cjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@langchain/core": "workspace:*",
"@langchain/openai": "workspace:*",
"@tsconfig/recommended": "^1.0.2",
"@xenova/transformers": "^2.5.4",
"d3-dsv": "2",
"hnswlib-node": "^3.0.0",
"langchain": "workspace:*",
Expand Down
4 changes: 2 additions & 2 deletions environment_tests/test-exports-cjs/src/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ async function test() {
const { LLMChain } = await import("langchain/chains");
const { ChatPromptTemplate } = await import("@langchain/core/prompts");
const { HNSWLib } = await import("@langchain/community/vectorstores/hnswlib");
const { OpenAIEmbeddings } = await import("@langchain/openai");
const { HuggingFaceTransformersEmbeddings } = await import("@langchain/community/embeddings/hf_transformers");
const { Document } = await import("@langchain/core/documents");
const { CSVLoader } = await import("langchain/document_loaders/fs/csv");

Expand All @@ -17,7 +17,7 @@ async function test() {
// Test dynamic imports of peer dependencies
const { HierarchicalNSW } = await HNSWLib.imports();

const vs = new HNSWLib(new OpenAIEmbeddings({ openAIApiKey: "sk-XXXX" }), {
const vs = new HNSWLib(new HuggingFaceTransformersEmbeddings({ model: "Xenova/all-MiniLM-L6-v2" }), {
space: "ip",
numDimensions: 3,
index: new HierarchicalNSW("ip", 3),
Expand Down
4 changes: 2 additions & 2 deletions environment_tests/test-exports-cjs/src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { OpenAI } from "@langchain/openai";
import { LLMChain } from "langchain/chains";
import { HNSWLib } from "@langchain/community/vectorstores/hnswlib";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { OpenAIEmbeddings } from "@langchain/openai";
import { HuggingFaceTransformersEmbeddings } from "@langchain/community/embeddings/hf_transformers";
import { Document } from "@langchain/core/documents";
import { CSVLoader } from "langchain/document_loaders/fs/csv";

Expand All @@ -16,7 +16,7 @@ assert(typeof HNSWLib === "function");
// Test dynamic imports of peer dependencies
const { HierarchicalNSW } = await HNSWLib.imports();

const vs = new HNSWLib(new OpenAIEmbeddings({ openAIApiKey: "sk-XXXX" }), {
const vs = new HNSWLib(new HuggingFaceTransformersEmbeddings({ model: "Xenova/all-MiniLM-L6-v2" }), {
space: "ip",
numDimensions: 3,
index: new HierarchicalNSW("ip", 3),
Expand Down
4 changes: 2 additions & 2 deletions environment_tests/test-exports-cjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { OpenAI } from "@langchain/openai";
import { LLMChain } from "langchain/chains";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { HNSWLib } from "@langchain/community/vectorstores/hnswlib";
import { OpenAIEmbeddings } from "@langchain/openai";
import { HuggingFaceTransformersEmbeddings } from "@langchain/community/embeddings/hf_transformers";
import { Document } from "@langchain/core/documents";
import { CSVLoader } from "langchain/document_loaders/fs/csv";

Expand All @@ -26,7 +26,7 @@ async function test(useAzure: boolean = false) {
: {
openAIApiKey: "sk-XXXX",
};
const vs = new HNSWLib(new OpenAIEmbeddings({ openAIApiKey: "sk-XXXX" }), {
const vs = new HNSWLib(new HuggingFaceTransformersEmbeddings({ model: "Xenova/all-MiniLM-L6-v2" }), {
space: "ip",
numDimensions: 3,
index: new HierarchicalNSW("ip", 3),
Expand Down
4 changes: 2 additions & 2 deletions environment_tests/test-exports-cjs/src/require.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { OpenAI } = require("@langchain/openai");
const { LLMChain } = require("langchain/chains");
const { ChatPromptTemplate } = require("@langchain/core/prompts");
const { HNSWLib } = require("@langchain/community/vectorstores/hnswlib");
const { OpenAIEmbeddings } = require("@langchain/openai");
const { HuggingFaceTransformersEmbeddings } = require("@langchain/community/embeddings/hf_transformers");
const { Document } = require("@langchain/core/documents");
const { CSVLoader } = require("langchain/document_loaders/fs/csv");

Expand All @@ -17,7 +17,7 @@ async function test() {
// Test dynamic imports of peer dependencies
const { HierarchicalNSW } = await HNSWLib.imports();

const vs = new HNSWLib(new OpenAIEmbeddings({ openAIApiKey: "sk-XXXX" }), {
const vs = new HNSWLib(new HuggingFaceTransformersEmbeddings({ model: "Xenova/all-MiniLM-L6-v2" }), {
space: "ip",
numDimensions: 3,
index: new HierarchicalNSW("ip", 3),
Expand Down
1 change: 1 addition & 0 deletions environment_tests/test-exports-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@langchain/core": "workspace:*",
"@langchain/openai": "workspace:*",
"@tsconfig/recommended": "^1.0.2",
"@xenova/transformers": "^2.5.4",
"d3-dsv": "2",
"hnswlib-node": "^3.0.0",
"langchain": "workspace:*",
Expand Down
4 changes: 2 additions & 2 deletions environment_tests/test-exports-esm/src/import.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ async function test() {
const { LLMChain } = await import("langchain/chains");
const { ChatPromptTemplate } = await import("@langchain/core/prompts");
const { HNSWLib } = await import("@langchain/community/vectorstores/hnswlib");
const { OpenAIEmbeddings } = await import("@langchain/openai");
const { HuggingFaceTransformersEmbeddings } = await import("@langchain/community/embeddings/hf_transformers");
const { Document } = await import("@langchain/core/documents");
const { CSVLoader } = await import("langchain/document_loaders/fs/csv");

Expand All @@ -17,7 +17,7 @@ async function test() {
// Test dynamic imports of peer dependencies
const { HierarchicalNSW } = await HNSWLib.imports();

const vs = new HNSWLib(new OpenAIEmbeddings({ openAIApiKey: "sk-XXXX" }), {
const vs = new HNSWLib(new HuggingFaceTransformersEmbeddings({ model: "Xenova/all-MiniLM-L6-v2", }), {
space: "ip",
numDimensions: 3,
index: new HierarchicalNSW("ip", 3),
Expand Down
6 changes: 3 additions & 3 deletions environment_tests/test-exports-esm/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { OpenAI } from "@langchain/openai";
import { LLMChain } from "langchain/chains";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { HNSWLib } from "@langchain/community/vectorstores/hnswlib";
import { OpenAIEmbeddings } from "@langchain/openai";
import { HuggingFaceTransformersEmbeddings } from "@langchain/community/embeddings/hf_transformers";
import { Document } from "@langchain/core/documents";
import { CSVLoader } from "langchain/document_loaders/fs/csv";
import { CallbackManager } from "@langchain/core/callbacks/manager";
Expand All @@ -13,13 +13,13 @@ assert(typeof OpenAI === "function");
assert(typeof LLMChain === "function");
assert(typeof ChatPromptTemplate === "function");
assert(typeof HNSWLib === "function");
assert(typeof OpenAIEmbeddings === "function");
assert(typeof HuggingFaceTransformersEmbeddings === "function");
assert(typeof CallbackManager === "function");

// Test dynamic imports of peer dependencies
const { HierarchicalNSW } = await HNSWLib.imports();

const vs = new HNSWLib(new OpenAIEmbeddings({ openAIApiKey: "sk-XXXX" }), {
const vs = new HNSWLib(new HuggingFaceTransformersEmbeddings({ model: "Xenova/all-MiniLM-L6-v2", }), {
space: "ip",
numDimensions: 3,
index: new HierarchicalNSW("ip", 3),
Expand Down
4 changes: 2 additions & 2 deletions environment_tests/test-exports-esm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { OpenAI } from "@langchain/openai";
import { LLMChain } from "langchain/chains";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { HNSWLib } from "@langchain/community/vectorstores/hnswlib";
import { OpenAIEmbeddings } from "@langchain/openai";
import { HuggingFaceTransformersEmbeddings } from "@langchain/community/embeddings/hf_transformers";
import { Document } from "@langchain/core/documents";
import { CSVLoader } from "langchain/document_loaders/fs/csv";

Expand All @@ -27,7 +27,7 @@ async function test(useAzure: boolean = false) {
openAIApiKey: "sk-XXXX",
};

const vs = new HNSWLib(new OpenAIEmbeddings(openAIParameters), {
const vs = new HNSWLib(new HuggingFaceTransformersEmbeddings({ model: "Xenova/all-MiniLM-L6-v2", }), {
space: "ip",
numDimensions: 3,
index: new HierarchicalNSW("ip", 3),
Expand Down
4 changes: 2 additions & 2 deletions environment_tests/test-exports-esm/src/require.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { OpenAI } = require("@langchain/openai");
const { LLMChain } = require("langchain/chains");
const { ChatPromptTemplate } = require("@langchain/core/prompts");
const { HNSWLib } = require("@langchain/community/vectorstores/hnswlib");
const { OpenAIEmbeddings } = require("@langchain/openai");
const { HuggingFaceTransformersEmbeddings } = require("@langchain/community/embeddings/hf_transformers");
const { Document } = require("@langchain/core/documents");
const { CSVLoader } = require("langchain/document_loaders/fs/csv");

Expand All @@ -17,7 +17,7 @@ async function test() {
// Test dynamic imports of peer dependencies
const { HierarchicalNSW } = await HNSWLib.imports();

const vs = new HNSWLib(new OpenAIEmbeddings({ openAIApiKey: "sk-XXXX" }), {
const vs = new HNSWLib(new HuggingFaceTransformersEmbeddings({ model: "Xenova/all-MiniLM-L6-v2", }), {
space: "ip",
numDimensions: 3,
index: new HierarchicalNSW("ip", 3),
Expand Down
9 changes: 4 additions & 5 deletions libs/langchain-community/src/embeddings/hf_transformers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Pipeline, pipeline } from "@xenova/transformers";
import type { Pipeline } from "@xenova/transformers";
import { Embeddings, type EmbeddingsParams } from "@langchain/core/embeddings";
import { chunkArray } from "@langchain/core/utils/chunk_array";

Expand Down Expand Up @@ -100,10 +100,9 @@ export class HuggingFaceTransformersEmbeddings
}

private async runEmbedding(texts: string[]) {
const pipe = await (this.pipelinePromise ??= pipeline(
"feature-extraction",
this.model
));
const pipe = await (this.pipelinePromise ??= (
await import("@xenova/transformers")
).pipeline("feature-extraction", this.model));

return this.caller.call(async () => {
const output = await pipe(texts, { pooling: "mean", normalize: true });
Expand Down
Loading