mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 15:00:57 +03:00
add endpoint to HF
This commit is contained in:
+11
-1
@@ -1,6 +1,6 @@
|
||||
import { INode, INodeData, INodeParams } from '../../../src/Interface'
|
||||
import { getBaseClasses } from '../../../src/utils'
|
||||
import { HuggingFaceInferenceEmbeddings, HuggingFaceInferenceEmbeddingsParams } from 'langchain/embeddings/hf'
|
||||
import { HuggingFaceInferenceEmbeddings, HuggingFaceInferenceEmbeddingsParams } from './core'
|
||||
|
||||
class HuggingFaceInferenceEmbedding_Embeddings implements INode {
|
||||
label: string
|
||||
@@ -31,6 +31,14 @@ class HuggingFaceInferenceEmbedding_Embeddings implements INode {
|
||||
name: 'modelName',
|
||||
type: 'string',
|
||||
optional: true
|
||||
},
|
||||
{
|
||||
label: 'Endpoint',
|
||||
name: 'endpoint',
|
||||
type: 'string',
|
||||
placeholder: 'https://xyz.eu-west-1.aws.endpoints.huggingface.cloud/sentence-transformers/all-MiniLM-L6-v2',
|
||||
description: 'Using your own inference endpoint',
|
||||
optional: true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -38,12 +46,14 @@ class HuggingFaceInferenceEmbedding_Embeddings implements INode {
|
||||
async init(nodeData: INodeData): Promise<any> {
|
||||
const apiKey = nodeData.inputs?.apiKey as string
|
||||
const modelName = nodeData.inputs?.modelName as string
|
||||
const endpoint = nodeData.inputs?.endpoint as string
|
||||
|
||||
const obj: Partial<HuggingFaceInferenceEmbeddingsParams> = {
|
||||
apiKey
|
||||
}
|
||||
|
||||
if (modelName) obj.model = modelName
|
||||
if (endpoint) obj.endpoint = endpoint
|
||||
|
||||
const model = new HuggingFaceInferenceEmbeddings(obj)
|
||||
return model
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import { HfInference } from '@huggingface/inference'
|
||||
import { Embeddings, EmbeddingsParams } from 'langchain/embeddings/base'
|
||||
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 ?? ''
|
||||
this.client = new HfInference(this.apiKey)
|
||||
if (this.endpoint) this.client.endpoint(this.endpoint)
|
||||
}
|
||||
|
||||
async _embed(texts: string[]): Promise<number[][]> {
|
||||
// replace newlines, which can negatively affect performance.
|
||||
const clean = texts.map((text) => text.replace(/\n/g, ' '))
|
||||
return this.caller.call(() =>
|
||||
this.client.featureExtraction({
|
||||
model: this.model,
|
||||
inputs: clean
|
||||
})
|
||||
) as Promise<number[][]>
|
||||
}
|
||||
|
||||
embedQuery(document: string): Promise<number[]> {
|
||||
return this._embed([document]).then((embeddings) => embeddings[0])
|
||||
}
|
||||
|
||||
embedDocuments(documents: string[]): Promise<number[][]> {
|
||||
return this._embed(documents)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user