-
-
Notifications
You must be signed in to change notification settings - Fork 24.2k
Add Row Text Splitter node for line-based document chunking #6138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dexterity104
wants to merge
8
commits into
FlowiseAI:main
Choose a base branch
from
Dexterity104:feature/row-text-splitter-node
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+128
−0
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2bbbec8
add Row Text Splitter node for line-based chunking
Dexterity104 f6f64da
refactor(RowTextSplitter): optimize line splitting logic to handle di…
Dexterity104 1195c03
Merge branch 'main' into feature/row-text-splitter-node
Dexterity104 fb19cda
Merge branch 'main' into feature/row-text-splitter-node
Dexterity104 7fd32d4
Merge branch 'main' into feature/row-text-splitter-node
Dexterity104 3e93a61
Merge branch 'main' into feature/row-text-splitter-node
Dexterity104 ccabd7e
Merge branch 'main' into feature/row-text-splitter-node
Dexterity104 3e2a16d
Merge branch 'main' into feature/row-text-splitter-node
Dexterity104 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
packages/components/nodes/textsplitters/RowTextSplitter/RowTextSplitter.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { INode, INodeData, INodeParams } from '../../../src/Interface' | ||
| import { getBaseClasses } from '../../../src/utils' | ||
| import { TextSplitter, TextSplitterParams } from '@langchain/textsplitters' | ||
|
|
||
| interface RowTextSplitterParams extends TextSplitterParams { | ||
| lineSeparator: string | ||
| trimWhitespace: boolean | ||
| includeEmptyLines: boolean | ||
| } | ||
|
|
||
| class RowTextSplitter extends TextSplitter implements RowTextSplitterParams { | ||
| static lc_name() { | ||
| return 'RowTextSplitter' | ||
| } | ||
|
|
||
| lineSeparator: string | ||
| trimWhitespace: boolean | ||
| includeEmptyLines: boolean | ||
|
|
||
| constructor(fields?: Partial<RowTextSplitterParams>) { | ||
| super({ | ||
| ...fields, | ||
| chunkSize: Number.MAX_SAFE_INTEGER, | ||
| chunkOverlap: 0 | ||
| }) | ||
| this.lineSeparator = fields?.lineSeparator ?? '\n' | ||
| this.trimWhitespace = fields?.trimWhitespace ?? true | ||
| this.includeEmptyLines = fields?.includeEmptyLines ?? false | ||
| } | ||
|
|
||
| async splitText(text: string): Promise<string[]> { | ||
| if (!text) return [] | ||
|
|
||
| const rawLines = text.split(this.lineSeparator) | ||
| const lines: string[] = [] | ||
|
|
||
| for (let raw of rawLines) { | ||
| if (this.lineSeparator === '\n') { | ||
| raw = raw.replace(/\r$/, '') | ||
| } | ||
|
|
||
| const line = this.trimWhitespace ? raw.trim() : raw | ||
|
|
||
| if (!this.includeEmptyLines && line.length === 0) { | ||
| continue | ||
| } | ||
|
|
||
| lines.push(line) | ||
| } | ||
|
|
||
| return lines | ||
| } | ||
| } | ||
|
|
||
| class RowTextSplitter_TextSplitters implements INode { | ||
| label: string | ||
| name: string | ||
| version: number | ||
| description: string | ||
| type: string | ||
| icon: string | ||
| category: string | ||
| baseClasses: string[] | ||
| inputs: INodeParams[] | ||
|
|
||
| constructor() { | ||
| this.label = 'Row Text Splitter' | ||
| this.name = 'rowTextSplitter' | ||
| this.version = 1.0 | ||
| this.type = 'RowTextSplitter' | ||
| this.icon = 'rowTextSplitter.svg' | ||
| this.category = 'Text Splitters' | ||
| this.description = `Splits text into individual rows/lines. Ideal for database table rows, CSV data, or line-based logs.` | ||
| this.baseClasses = [this.type, ...getBaseClasses(RowTextSplitter)] | ||
| this.inputs = [ | ||
| { | ||
| label: 'Line Separator', | ||
| name: 'lineSeparator', | ||
| type: 'string', | ||
| description: 'Character or string that separates rows. Defaults to newline (\\n).', | ||
| placeholder: '\\n', | ||
| optional: true | ||
| }, | ||
| { | ||
| label: 'Trim Whitespace', | ||
| name: 'trimWhitespace', | ||
| type: 'boolean', | ||
| description: 'Trim whitespace from the start and end of each row.', | ||
| default: true, | ||
| optional: true, | ||
| additionalParams: true | ||
| }, | ||
| { | ||
| label: 'Include Empty Lines', | ||
| name: 'includeEmptyLines', | ||
| type: 'boolean', | ||
| description: 'Whether to include empty lines as separate rows.', | ||
| default: false, | ||
| optional: true, | ||
| additionalParams: true | ||
| } | ||
| ] | ||
| } | ||
|
|
||
| async init(nodeData: INodeData): Promise<any> { | ||
| const lineSeparatorInput = (nodeData.inputs?.lineSeparator as string) || '' | ||
| const trimWhitespace = (nodeData.inputs?.trimWhitespace as boolean) ?? true | ||
| const includeEmptyLines = (nodeData.inputs?.includeEmptyLines as boolean) ?? false | ||
|
|
||
| const splitter = new RowTextSplitter({ | ||
| lineSeparator: this.normalizeSeparator(lineSeparatorInput), | ||
| trimWhitespace, | ||
| includeEmptyLines | ||
| }) | ||
|
|
||
| return splitter | ||
| } | ||
|
|
||
| private normalizeSeparator(separator: string): string { | ||
| if (!separator) return '\n' | ||
|
|
||
| return separator.replace(/\\r/g, '\r').replace(/\\n/g, '\n').replace(/\\t/g, '\t') | ||
| } | ||
| } | ||
|
|
||
| module.exports = { nodeClass: RowTextSplitter_TextSplitters } | ||
6 changes: 6 additions & 0 deletions
6
packages/components/nodes/textsplitters/RowTextSplitter/rowTextSplitter.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of line splitting and carriage return handling can be optimized. Performing a regex replacement inside a loop for every line is less efficient than handling it during the split operation, especially for large documents like logs or CSVs. Using a regex in the
splitmethod when the separator is a newline is a more performant and idiomatic approach to handle both LF and CRLF line endings.References