first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
+123
@@ -0,0 +1,123 @@
|
||||
import type { InferenceProviderOrPolicy } from '@huggingface/inference';
|
||||
import { PROVIDERS_OR_POLICIES } from '@huggingface/inference';
|
||||
import { HuggingFaceInferenceEmbeddings } from '@langchain/community/embeddings/hf';
|
||||
import {
|
||||
NodeConnectionTypes,
|
||||
NodeOperationError,
|
||||
type INodeType,
|
||||
type INodeTypeDescription,
|
||||
type ISupplyDataFunctions,
|
||||
type SupplyData,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { logWrapper, getConnectionHintNoticeField } from '@n8n/ai-utilities';
|
||||
|
||||
export class EmbeddingsHuggingFaceInference implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Embeddings Hugging Face Inference',
|
||||
name: 'embeddingsHuggingFaceInference',
|
||||
icon: 'file:huggingface.svg',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
description: 'Use HuggingFace Inference Embeddings',
|
||||
defaults: {
|
||||
name: 'Embeddings HuggingFace Inference',
|
||||
},
|
||||
credentials: [
|
||||
{
|
||||
name: 'huggingFaceApi',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
codex: {
|
||||
categories: ['AI'],
|
||||
subcategories: {
|
||||
AI: ['Embeddings'],
|
||||
},
|
||||
resources: {
|
||||
primaryDocumentation: [
|
||||
{
|
||||
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.embeddingshuggingfaceinference/',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
inputs: [],
|
||||
|
||||
outputs: [NodeConnectionTypes.AiEmbedding],
|
||||
outputNames: ['Embeddings'],
|
||||
properties: [
|
||||
getConnectionHintNoticeField([NodeConnectionTypes.AiVectorStore]),
|
||||
{
|
||||
displayName:
|
||||
'Each model is using different dimensional density for embeddings. Please make sure to use the same dimensionality for your vector store. The default model is using 768-dimensional embeddings.',
|
||||
name: 'notice',
|
||||
type: 'notice',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Model Name',
|
||||
name: 'modelName',
|
||||
type: 'string',
|
||||
default: 'sentence-transformers/distilbert-base-nli-mean-tokens',
|
||||
description: 'The model name to use from HuggingFace library',
|
||||
},
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
placeholder: 'Add Option',
|
||||
description: 'Additional options to add',
|
||||
type: 'collection',
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Custom Inference Endpoint',
|
||||
name: 'endpointUrl',
|
||||
default: '',
|
||||
description: 'Custom endpoint URL',
|
||||
type: 'string',
|
||||
},
|
||||
{
|
||||
displayName: 'Provider',
|
||||
name: 'provider',
|
||||
type: 'options',
|
||||
options: PROVIDERS_OR_POLICIES.map((value) => ({ value, name: value })),
|
||||
default: 'auto',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> {
|
||||
this.logger.debug('Supply data for embeddings HF Inference');
|
||||
const model = this.getNodeParameter(
|
||||
'modelName',
|
||||
itemIndex,
|
||||
'sentence-transformers/distilbert-base-nli-mean-tokens',
|
||||
) as string;
|
||||
const credentials = await this.getCredentials('huggingFaceApi');
|
||||
const options = this.getNodeParameter('options', itemIndex, {}) as object;
|
||||
|
||||
if ('provider' in options && !isValidHFProviderOrPolicy(options.provider)) {
|
||||
throw new NodeOperationError(this.getNode(), 'Unsupported provider');
|
||||
}
|
||||
|
||||
const embeddings = new HuggingFaceInferenceEmbeddings({
|
||||
apiKey: credentials.apiKey as string,
|
||||
model,
|
||||
...options,
|
||||
});
|
||||
|
||||
return {
|
||||
response: logWrapper(embeddings, this),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function isValidHFProviderOrPolicy(provider: unknown): provider is InferenceProviderOrPolicy {
|
||||
return (
|
||||
typeof provider === 'string' && (PROVIDERS_OR_POLICIES as readonly string[]).includes(provider)
|
||||
);
|
||||
}
|
||||
+1
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 18 KiB |
Reference in New Issue
Block a user