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:
+126
@@ -0,0 +1,126 @@
|
||||
import type {
|
||||
RecursiveCharacterTextSplitterParams,
|
||||
SupportedTextSplitterLanguage,
|
||||
} from '@langchain/textsplitters';
|
||||
import { RecursiveCharacterTextSplitter } from '@langchain/textsplitters';
|
||||
import {
|
||||
NodeConnectionTypes,
|
||||
type INodeType,
|
||||
type INodeTypeDescription,
|
||||
type ISupplyDataFunctions,
|
||||
type SupplyData,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { logWrapper, getConnectionHintNoticeField } from '@n8n/ai-utilities';
|
||||
|
||||
const supportedLanguages: SupportedTextSplitterLanguage[] = [
|
||||
'cpp',
|
||||
'go',
|
||||
'java',
|
||||
'js',
|
||||
'php',
|
||||
'proto',
|
||||
'python',
|
||||
'rst',
|
||||
'ruby',
|
||||
'rust',
|
||||
'scala',
|
||||
'swift',
|
||||
'markdown',
|
||||
'latex',
|
||||
'html',
|
||||
];
|
||||
export class TextSplitterRecursiveCharacterTextSplitter implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Recursive Character Text Splitter',
|
||||
name: 'textSplitterRecursiveCharacterTextSplitter',
|
||||
icon: 'fa:grip-lines-vertical',
|
||||
iconColor: 'black',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
description: 'Split text into chunks by characters recursively, recommended for most use cases',
|
||||
defaults: {
|
||||
name: 'Recursive Character Text Splitter',
|
||||
},
|
||||
codex: {
|
||||
categories: ['AI'],
|
||||
subcategories: {
|
||||
AI: ['Text Splitters'],
|
||||
},
|
||||
resources: {
|
||||
primaryDocumentation: [
|
||||
{
|
||||
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.textsplitterrecursivecharactertextsplitter/',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
inputs: [],
|
||||
|
||||
outputs: [NodeConnectionTypes.AiTextSplitter],
|
||||
outputNames: ['Text Splitter'],
|
||||
properties: [
|
||||
getConnectionHintNoticeField([NodeConnectionTypes.AiDocument]),
|
||||
{
|
||||
displayName: 'Chunk Size',
|
||||
name: 'chunkSize',
|
||||
type: 'number',
|
||||
default: 1000,
|
||||
},
|
||||
{
|
||||
displayName: 'Chunk Overlap',
|
||||
name: 'chunkOverlap',
|
||||
type: 'number',
|
||||
default: 0,
|
||||
},
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
placeholder: 'Add Option',
|
||||
description: 'Additional options to add',
|
||||
type: 'collection',
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Split Code',
|
||||
name: 'splitCode',
|
||||
default: 'markdown',
|
||||
type: 'options',
|
||||
options: supportedLanguages.map((lang) => ({ name: lang, value: lang })),
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> {
|
||||
this.logger.debug('Supply Data for Text Splitter');
|
||||
|
||||
const chunkSize = this.getNodeParameter('chunkSize', itemIndex) as number;
|
||||
const chunkOverlap = this.getNodeParameter('chunkOverlap', itemIndex) as number;
|
||||
const splitCode = this.getNodeParameter(
|
||||
'options.splitCode',
|
||||
itemIndex,
|
||||
null,
|
||||
) as SupportedTextSplitterLanguage | null;
|
||||
const params: RecursiveCharacterTextSplitterParams = {
|
||||
// TODO: These are the default values, should we allow the user to change them?
|
||||
separators: ['\n\n', '\n', ' ', ''],
|
||||
chunkSize,
|
||||
chunkOverlap,
|
||||
keepSeparator: false,
|
||||
};
|
||||
let splitter: RecursiveCharacterTextSplitter;
|
||||
|
||||
if (splitCode && supportedLanguages.includes(splitCode)) {
|
||||
splitter = RecursiveCharacterTextSplitter.fromLanguage(splitCode, params);
|
||||
} else {
|
||||
splitter = new RecursiveCharacterTextSplitter(params);
|
||||
}
|
||||
|
||||
return {
|
||||
response: logWrapper(splitter, this),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user