Skip to content

Commit c92e7bd

Browse files
committed
fix(hf): Upgrade @huggingface/inference to v3
Changes: - Migrated from HfInference to InferenceClient. - Added a provider parameter as it is required for the new version. The parameter can also be set using HUGGINGFACEHUB_PROVIDER environment variable. - Updated documentation accordingly - Fixed broken integration test, and added a new test for when the new provider parameter is used. Other Improvements: - The model parameter now supports being set via the HUGGINGFACEHUB_MODEL environment variable, in addition to the constructor argument. - If no model is provided, a warning is logged that the default `BAAI/bge-base-en-v1.5` is used.
1 parent 210c2ab commit c92e7bd

File tree

5 files changed

+83
-17
lines changed

5 files changed

+83
-17
lines changed

docs/core_docs/docs/integrations/text_embedding/hugging_face_inference.mdx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
# HuggingFace Inference
22

3-
This Embeddings integration uses the HuggingFace Inference API to generate embeddings for a given text using by default the `sentence-transformers/distilbert-base-nli-mean-tokens` model. You can pass a different model name to the constructor to use a different model.
3+
This Embeddings integration uses the HuggingFace Inference API to generate embeddings for a given text, using the `BAAI/bge-base-en-v1.5` model by default. You can pass a different model name to the constructor to use a different model.
4+
The current HuggingFace API also expects you to specify a `provider`, but has a fallback auto-select mode.
45

56
## Setup
67

7-
You'll first need to install the [`@langchain/community`](https://www.npmjs.com/package/@langchain/community) package and the required peer dep:
8+
You'll first need to install the [`@langchain/community`](https://www.npmjs.com/package/@langchain/community) package and the required peer dependency:
89

910
import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";
1011

1112
<IntegrationInstallTooltip></IntegrationInstallTooltip>
1213

1314
```bash npm2yarn
14-
npm install @langchain/community @langchain/core @huggingface/inference@2
15+
npm install @langchain/community @langchain/core @huggingface/inference@3
1516
```
1617

1718
## Usage
@@ -20,10 +21,19 @@ npm install @langchain/community @langchain/core @huggingface/inference@2
2021
import { HuggingFaceInferenceEmbeddings } from "@langchain/community/embeddings/hf";
2122

2223
const embeddings = new HuggingFaceInferenceEmbeddings({
23-
apiKey: "YOUR-API-KEY", // In Node.js defaults to process.env.HUGGINGFACEHUB_API_KEY
24+
apiKey: "YOUR-API-KEY", // Defaults to process.env.HUGGINGFACEHUB_API_KEY
25+
model: "MODEL-NAME", // Defaults to process.env.HUGGINGFACEHUB_MODEL, or `BAAI/bge-base-en-v1.5` if not provided
26+
provider: "MODEL-PROVIDER", // Defaults to process.env.HUGGINGFACEHUB_PROVIDER, or `auto` if not provided
2427
});
2528
```
2629

30+
> **Note:**
31+
> If you do not provide a `model`, a warning will be logged and the default model `BAAI/bge-base-en-v1.5` will be used.
32+
> If you do not provide a `provider`, Hugging Face will default the provider to `auto`, which will select the first provider available for the model based on your settings at https://hf.co/settings/inference-providers.
33+
34+
> **Hint:**
35+
> `hf-inference` is the provider name for models that are hosted directly by Hugging Face.
36+
2737
## Related
2838

2939
- Embedding model [conceptual guide](/docs/concepts/embedding_models)

libs/langchain-community/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"@google-ai/generativelanguage": "^2.5.0",
8080
"@google-cloud/storage": "^7.15.2",
8181
"@gradientai/nodejs-sdk": "^1.2.0",
82-
"@huggingface/inference": "^2.6.4",
82+
"@huggingface/inference": "^3.13.2",
8383
"@huggingface/transformers": "^3.2.3",
8484
"@ibm-cloud/watsonx-ai": "^1.6.4",
8585
"@jest/globals": "^29.5.0",
@@ -254,7 +254,7 @@
254254
"@google-ai/generativelanguage": "*",
255255
"@google-cloud/storage": "^6.10.1 || ^7.7.0",
256256
"@gradientai/nodejs-sdk": "^1.2.0",
257-
"@huggingface/inference": "^2.6.4",
257+
"@huggingface/inference": "^3.13.2",
258258
"@huggingface/transformers": "^3.2.3",
259259
"@ibm-cloud/watsonx-ai": "*",
260260
"@lancedb/lancedb": "^0.12.0",

libs/langchain-community/src/embeddings/hf.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HfInference, HfInferenceEndpoint } from "@huggingface/inference";
1+
import { InferenceClient } from "@huggingface/inference";
22
import { Embeddings, type EmbeddingsParams } from "@langchain/core/embeddings";
33
import { getEnvironmentVariable } from "@langchain/core/utils/env";
44

@@ -10,6 +10,7 @@ export interface HuggingFaceInferenceEmbeddingsParams extends EmbeddingsParams {
1010
apiKey?: string;
1111
model?: string;
1212
endpointUrl?: string;
13+
provider?: string;
1314
}
1415

1516
/**
@@ -27,18 +28,32 @@ export class HuggingFaceInferenceEmbeddings
2728

2829
endpointUrl?: string;
2930

30-
client: HfInference | HfInferenceEndpoint;
31+
provider?: string;
32+
33+
client: InferenceClient;
3134

3235
constructor(fields?: HuggingFaceInferenceEmbeddingsParams) {
3336
super(fields ?? {});
3437

35-
this.model = fields?.model ?? "BAAI/bge-base-en-v1.5";
38+
const envModel = getEnvironmentVariable("HUGGINGFACEHUB_MODEL");
39+
if (fields?.model) {
40+
this.model = fields.model;
41+
} else if (envModel) {
42+
this.model = envModel;
43+
} else {
44+
console.warn(
45+
'[HuggingFaceInferenceEmbeddings] No "model" provided. Using default: "BAAI/bge-base-en-v1.5".'
46+
);
47+
this.model = "BAAI/bge-base-en-v1.5";
48+
}
3649
this.apiKey =
3750
fields?.apiKey ?? getEnvironmentVariable("HUGGINGFACEHUB_API_KEY");
3851
this.endpointUrl = fields?.endpointUrl;
52+
this.provider =
53+
fields?.provider ?? getEnvironmentVariable("HUGGINGFACEHUB_PROVIDER");
3954
this.client = this.endpointUrl
40-
? new HfInference(this.apiKey).endpoint(this.endpointUrl)
41-
: new HfInference(this.apiKey);
55+
? new InferenceClient(this.apiKey).endpoint(this.endpointUrl)
56+
: new InferenceClient(this.apiKey);
4257
}
4358

4459
async _embed(texts: string[]): Promise<number[][]> {
@@ -48,6 +63,7 @@ export class HuggingFaceInferenceEmbeddings
4863
this.client.featureExtraction({
4964
model: this.model,
5065
inputs: clean,
66+
provider: this.provider,
5167
})
5268
) as Promise<number[][]>;
5369
}

libs/langchain-community/src/embeddings/tests/hf.int.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,36 @@ test("HuggingFaceInferenceEmbeddings", async () => {
2323
expect(await store.similaritySearch(texts[4], 2)).toMatchInlineSnapshot(`
2424
[
2525
Document {
26+
"id": undefined,
2627
"metadata": {},
2728
"pageContent": "1 + 1 = 2",
2829
},
2930
Document {
31+
"id": undefined,
3032
"metadata": {},
3133
"pageContent": "1 + 1 = 3",
3234
},
3335
]
3436
`);
3537
});
38+
39+
test("HuggingFaceInferenceEmbeddings with explicit model and provider", async () => {
40+
const model = "BAAI/bge-small-en-v1.5";
41+
const provider = "hf-inference";
42+
const embeddings = new HuggingFaceInferenceEmbeddings({
43+
model,
44+
provider,
45+
});
46+
47+
const texts = ["Integration test input 1", "Integration test input 2"];
48+
49+
const queryEmbedding = await embeddings.embedQuery(texts[0]);
50+
expect(Array.isArray(queryEmbedding)).toBe(true);
51+
expect(typeof queryEmbedding[0]).toBe("number");
52+
53+
const store = await HNSWLib.fromTexts(texts, {}, embeddings);
54+
const results = await store.similaritySearch(texts[1], 1);
55+
56+
expect(results.length).toBe(1);
57+
expect(results[0].pageContent).toBe("Integration test input 2");
58+
});

yarn.lock

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5972,10 +5972,13 @@ __metadata:
59725972
languageName: node
59735973
linkType: hard
59745974

5975-
"@huggingface/inference@npm:^2.6.4":
5976-
version: 2.6.4
5977-
resolution: "@huggingface/inference@npm:2.6.4"
5978-
checksum: 7d48960a62d0621d4c3f1edd183aa5d7829d297110b5720c78291aac17ed58b6a9af8eaf8a3f2cbb9dabfda3cf48931f59cf491cdedefd624f90d93fa3927981
5975+
"@huggingface/inference@npm:^3.13.2":
5976+
version: 3.13.2
5977+
resolution: "@huggingface/inference@npm:3.13.2"
5978+
dependencies:
5979+
"@huggingface/jinja": ^0.5.0
5980+
"@huggingface/tasks": ^0.19.6
5981+
checksum: 347192023adcaa2ec70d4de10b33306592c1b9fbef3cf0eec7de7a11821f2dfd733fbbb53be6e0dbf962a82b499ff816e705758f7c0b7d165367689c6f2ffd87
59795982
languageName: node
59805983
linkType: hard
59815984

@@ -5986,6 +5989,20 @@ __metadata:
59865989
languageName: node
59875990
linkType: hard
59885991

5992+
"@huggingface/jinja@npm:^0.5.0":
5993+
version: 0.5.0
5994+
resolution: "@huggingface/jinja@npm:0.5.0"
5995+
checksum: 8bb04021f381158d5e358a166f8eec250785c0dc9f70a04b92d0aabddc8583a9f20026c808be59d4926787087399ca8f5a4cc8fc37749be35bea63473af19c53
5996+
languageName: node
5997+
linkType: hard
5998+
5999+
"@huggingface/tasks@npm:^0.19.6":
6000+
version: 0.19.8
6001+
resolution: "@huggingface/tasks@npm:0.19.8"
6002+
checksum: 89377ebda7b5cdecf12e805b52eb8800d3f0fb337855e4d3b4e082aeae9c1ad5492f94ea9492e45d1c7c23779836bf168b7f21103be3fb1c1651be67fa0f89bb
6003+
languageName: node
6004+
linkType: hard
6005+
59896006
"@huggingface/transformers@npm:^3.2.3":
59906007
version: 3.2.4
59916008
resolution: "@huggingface/transformers@npm:3.2.4"
@@ -7264,7 +7281,7 @@ __metadata:
72647281
"@google-ai/generativelanguage": ^2.5.0
72657282
"@google-cloud/storage": ^7.15.2
72667283
"@gradientai/nodejs-sdk": ^1.2.0
7267-
"@huggingface/inference": ^2.6.4
7284+
"@huggingface/inference": ^3.13.2
72687285
"@huggingface/transformers": ^3.2.3
72697286
"@ibm-cloud/watsonx-ai": ^1.6.4
72707287
"@jest/globals": ^29.5.0
@@ -7449,7 +7466,7 @@ __metadata:
74497466
"@google-ai/generativelanguage": "*"
74507467
"@google-cloud/storage": ^6.10.1 || ^7.7.0
74517468
"@gradientai/nodejs-sdk": ^1.2.0
7452-
"@huggingface/inference": ^2.6.4
7469+
"@huggingface/inference": ^3.13.2
74537470
"@huggingface/transformers": ^3.2.3
74547471
"@ibm-cloud/watsonx-ai": "*"
74557472
"@lancedb/lancedb": ^0.12.0

0 commit comments

Comments
 (0)