Files
Flowise/packages/components/nodes/embeddings/HuggingFaceInferenceEmbedding/core.ts
T
Siddharth Chauhan 0cc7b3036e fix: Upgrade Hugging Face Inference API to support Inference Providers (#5454)
- Upgrade @huggingface/inference from v2.6.1 to v4.13.2
- Update ChatHuggingFace to use InferenceClient with chatCompletion API
- Update HuggingFaceInference (LLM) to use v4 HfInference with Inference Providers
- Update HuggingFaceInferenceEmbedding to use v4 HfInference
- Add endpoint handling logic to ignore custom endpoints for provider-based models
- Add improved error handling and validation for API keys
- Update UI descriptions to guide users on proper configuration

Fixes #5161

Co-authored-by: Henry <hzj94@hotmail.com>
2025-11-25 11:43:36 +00:00

54 lines
1.8 KiB
TypeScript

import { HfInference } from '@huggingface/inference'
import { Embeddings, EmbeddingsParams } from '@langchain/core/embeddings'
import { getEnvironmentVariable } from '../../../src/utils'
export interface HuggingFaceInferenceEmbeddingsParams extends EmbeddingsParams {
apiKey?: string
model?: string
endpoint?: string
}
export class HuggingFaceInferenceEmbeddings extends Embeddings implements HuggingFaceInferenceEmbeddingsParams {
apiKey?: string
endpoint?: string
model: string
client: HfInference
constructor(fields?: HuggingFaceInferenceEmbeddingsParams) {
super(fields ?? {})
this.model = fields?.model ?? 'sentence-transformers/distilbert-base-nli-mean-tokens'
this.apiKey = fields?.apiKey ?? getEnvironmentVariable('HUGGINGFACEHUB_API_KEY')
this.endpoint = fields?.endpoint ?? ''
const hf = new HfInference(this.apiKey)
// v4 uses Inference Providers by default; only override if custom endpoint provided
this.client = this.endpoint ? hf.endpoint(this.endpoint) : hf
}
async _embed(texts: string[]): Promise<number[][]> {
// replace newlines, which can negatively affect performance.
const clean = texts.map((text) => text.replace(/\n/g, ' '))
const obj: any = {
inputs: clean
}
if (!this.endpoint) {
obj.model = this.model
}
const res = await this.caller.callWithOptions({}, this.client.featureExtraction.bind(this.client), obj)
return res as number[][]
}
async embedQuery(document: string): Promise<number[]> {
const res = await this._embed([document])
return res[0]
}
async embedDocuments(documents: string[]): Promise<number[][]> {
return this._embed(documents)
}
}