-
Notifications
You must be signed in to change notification settings - Fork 3.9k
feat: improved text splitter #5539
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fea5741
feat: text splitter
samdenty 00d483d
chore: add changeset
samdenty 08fd40c
Merge remote-tracking branch 'origin/main' into feat/smooth-stream-cjk
samdenty 6c06aae
chore: add comment
samdenty 5a6be5d
chore: review
samdenty 9555705
fix
samdenty 61d57db
fix: lint
samdenty 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
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,5 @@ | ||
| --- | ||
| 'ai': patch | ||
| --- | ||
|
|
||
| feat(ai): improved text splitter |
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
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 |
|---|---|---|
|
|
@@ -2,10 +2,12 @@ import { InvalidArgumentError } from '@ai-sdk/provider'; | |
| import { delay as originalDelay } from '@ai-sdk/provider-utils'; | ||
| import { TextStreamPart } from './stream-text-result'; | ||
| import { ToolSet } from './tool-set'; | ||
| import { TextSplit, splitText } from './text-splitter'; | ||
|
|
||
| const CHUNKING_REGEXPS = { | ||
| word: /\s*\S+\s+/m, | ||
| line: /[^\n]*\n/m, | ||
| character: /(?!\s)(?=.)/g, | ||
| word: /[\u4E00-\u9FFF\u3040-\u309F\u30A0-\u30FF]|\s+/gm, | ||
| line: /\r\n|\r|\n/g, | ||
|
Comment on lines
+8
to
+10
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what will happen to custom user regexp?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'll add more tests for custom chunking |
||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -22,7 +24,7 @@ export function smoothStream<TOOLS extends ToolSet>({ | |
| _internal: { delay = originalDelay } = {}, | ||
| }: { | ||
| delayInMs?: number | null; | ||
| chunking?: 'word' | 'line' | RegExp; | ||
| chunking?: 'character' | 'word' | 'line' | { split: string } | RegExp; | ||
| /** | ||
| * Internal. For test use only. May change without notice. | ||
| */ | ||
|
|
@@ -32,10 +34,14 @@ export function smoothStream<TOOLS extends ToolSet>({ | |
| } = {}): (options: { | ||
| tools: TOOLS; | ||
| }) => TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>> { | ||
| const chunkingRegexp = | ||
| typeof chunking === 'string' ? CHUNKING_REGEXPS[chunking] : chunking; | ||
| const chunker = | ||
| typeof chunking === 'object' && 'split' in chunking | ||
| ? chunking.split | ||
| : typeof chunking === 'string' | ||
| ? CHUNKING_REGEXPS[chunking] | ||
| : chunking; | ||
|
|
||
| if (chunkingRegexp == null) { | ||
| if (chunker == null) { | ||
| throw new InvalidArgumentError({ | ||
| argument: 'chunking', | ||
| message: `Chunking must be "word" or "line" or a RegExp. Received: ${chunking}`, | ||
|
|
@@ -44,12 +50,24 @@ export function smoothStream<TOOLS extends ToolSet>({ | |
|
|
||
| return () => { | ||
| let buffer = ''; | ||
| let lastSplits: TextSplit[] = []; | ||
| let lastIndex = 0; | ||
|
|
||
| return new TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>>({ | ||
| async transform(chunk, controller) { | ||
| const lastSplit = lastSplits.at(-1); | ||
|
|
||
| if (chunk.type !== 'text-delta') { | ||
| if (buffer.length > 0) { | ||
| controller.enqueue({ type: 'text-delta', textDelta: buffer }); | ||
| if (lastSplits.length > 1 && delayInMs) { | ||
| await delay(delayInMs); | ||
| } | ||
|
|
||
| if (lastSplit) { | ||
| controller.enqueue({ | ||
| type: 'text-delta', | ||
| textDelta: lastSplit.text, | ||
| }); | ||
| lastSplits = []; | ||
| buffer = ''; | ||
| } | ||
|
|
||
|
|
@@ -59,14 +77,24 @@ export function smoothStream<TOOLS extends ToolSet>({ | |
|
|
||
| buffer += chunk.textDelta; | ||
|
|
||
| let match; | ||
| while ((match = chunkingRegexp.exec(buffer)) != null) { | ||
| const chunk = match[0]; | ||
| controller.enqueue({ type: 'text-delta', textDelta: chunk }); | ||
| buffer = buffer.slice(chunk.length); | ||
| const splits = splitText(buffer, chunker); | ||
|
|
||
| await delay(delayInMs); | ||
| // If there's a new split with the start index greater than the last index, | ||
| // push the new split(s) and delay. | ||
| const newSplitIndex = splits.findIndex( | ||
| split => !lastSplit || split.start >= lastIndex, | ||
| ); | ||
|
|
||
| if (newSplitIndex !== -1) { | ||
| for (let i = newSplitIndex; i < splits.length - 1; i++) { | ||
| const split = splits[i]; | ||
| controller.enqueue({ type: 'text-delta', textDelta: split.text }); | ||
| lastIndex = split.end; | ||
| await delay(delayInMs); | ||
| } | ||
| } | ||
|
|
||
| lastSplits = splits; | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
this will create an additional delay