From 741b97e80e36a4bd28e770a9a7f4ce5eb1015f2e Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 20 Jul 2023 01:07:29 +0100 Subject: [PATCH] add fix to HF Embeddings --- .../HuggingFaceInferenceEmbedding/core.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/components/nodes/embeddings/HuggingFaceInferenceEmbedding/core.ts b/packages/components/nodes/embeddings/HuggingFaceInferenceEmbedding/core.ts index 41e63aa49..c75658d45 100644 --- a/packages/components/nodes/embeddings/HuggingFaceInferenceEmbedding/core.ts +++ b/packages/components/nodes/embeddings/HuggingFaceInferenceEmbedding/core.ts @@ -30,18 +30,26 @@ export class HuggingFaceInferenceEmbeddings extends Embeddings implements Huggin async _embed(texts: string[]): Promise { // replace newlines, which can negatively affect performance. const clean = texts.map((text) => text.replace(/\n/g, ' ')) + const hf = new HfInference(this.apiKey) const obj: any = { inputs: clean } - if (!this.endpoint) obj.model = this.model - return this.caller.call(() => this.client.featureExtraction(obj)) as Promise + if (this.endpoint) { + hf.endpoint(this.endpoint) + } else { + obj.model = this.model + } + + const res = await this.caller.callWithOptions({}, hf.featureExtraction.bind(hf), obj) + return res as number[][] } - embedQuery(document: string): Promise { - return this._embed([document]).then((embeddings) => embeddings[0]) + async embedQuery(document: string): Promise { + const res = await this._embed([document]) + return res[0] } - embedDocuments(documents: string[]): Promise { + async embedDocuments(documents: string[]): Promise { return this._embed(documents) } }