-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: add max_page to chunk_by_title and remove multipage_sections #4382
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
b7b64f9
39b24c2
d8d0226
506597d
8be5ede
0d91c18
2508e3a
6c39d78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| __version__ = "0.23.3" # pragma: no cover | ||
| __version__ = "0.23.4" # pragma: no cover |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| PreChunkCombiner, | ||
| PreChunker, | ||
| is_on_next_page, | ||
| is_on_page_exceeding_max, | ||
| is_title, | ||
| ) | ||
| from unstructured.documents.elements import Element | ||
|
|
@@ -26,6 +27,7 @@ def chunk_by_title( | |
| combine_text_under_n_chars: Optional[int] = None, | ||
| include_orig_elements: Optional[bool] = None, | ||
| max_characters: Optional[int] = None, | ||
| max_page: Optional[int] = None, | ||
| max_tokens: Optional[int] = None, | ||
| multipage_sections: Optional[bool] = None, | ||
|
Contributor
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. P1: Custom agent: Flag AI Slop and Fabricated Changes PR description claims multipage_sections is 'removed entirely' and 'passing it now raises TypeError', but the code adds it back as an optional parameter with a DeprecationWarning and backward-compatibility logic instead. Prompt for AI agents |
||
| new_after_n_chars: Optional[int] = None, | ||
|
|
@@ -61,6 +63,11 @@ def chunk_by_title( | |
| max_characters | ||
| Chunks elements text and text_as_html (if present) into chunks of length | ||
| n characters (hard max). Mutually exclusive with `max_tokens`. | ||
| max_page | ||
| Maximum number of pages a single chunk may span. When set, a new chunk is started whenever | ||
| an element's page number is more than `max_page - 1` pages past the page where the current | ||
| chunk began. Elements without a page number are assumed to continue the current page. | ||
| Must be >= 1 when specified. | ||
| max_tokens | ||
| Chunks elements into chunks of n tokens (hard max). Requires `tokenizer` to be specified. | ||
| Mutually exclusive with `max_characters`. | ||
|
|
@@ -102,6 +109,7 @@ def chunk_by_title( | |
| combine_text_under_n_chars=combine_text_under_n_chars, | ||
| include_orig_elements=include_orig_elements, | ||
| max_characters=max_characters, | ||
| max_page=max_page, | ||
| max_tokens=max_tokens, | ||
| multipage_sections=multipage_sections, | ||
| new_after_n_chars=new_after_n_chars, | ||
|
|
@@ -154,6 +162,8 @@ def iter_boundary_predicates() -> Iterator[BoundaryPredicate]: | |
| yield is_title | ||
| if not self.multipage_sections: | ||
| yield is_on_next_page() | ||
| if self.max_page is not None: | ||
| yield is_on_page_exceeding_max(self.max_page) | ||
|
|
||
| return tuple(iter_boundary_predicates()) | ||
|
|
||
|
|
@@ -169,6 +179,11 @@ def combine_text_under_n_chars(self) -> int: | |
| arg_value = self._kwargs.get("combine_text_under_n_chars") | ||
| return self.hard_max if arg_value is None else arg_value | ||
|
|
||
| @cached_property | ||
| def max_page(self) -> Optional[int]: | ||
| """Maximum number of pages a single chunk may span, or None for no page-count limit.""" | ||
| return self._kwargs.get("max_page") | ||
|
|
||
| @cached_property | ||
| def multipage_sections(self) -> bool: | ||
| """When False, break pre-chunks on page-boundaries.""" | ||
|
|
@@ -197,3 +212,6 @@ def _validate(self) -> None: | |
| f"'combine_text_under_n_chars' argument must not exceed `max_characters`" | ||
| f" value, got {self.combine_text_under_n_chars} > {self.hard_max}" | ||
| ) | ||
|
|
||
| if self.max_page is not None and self.max_page < 1: | ||
| raise ValueError(f"'max_page' argument must be >= 1, got {self.max_page}") | ||
Uh oh!
There was an error while loading. Please reload this page.