ontocast.tool.chunk¶
Document chunking tools for OntoCast.
ChunkerTool
¶
Bases: Tool
Tool for semantic chunking of documents.
Falls back to naive chunking if sentence-transformers is not available. Includes caching to avoid re-chunking the same text with the same parameters.
Source code in ontocast/tool/chunk/chunker.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
__call__(doc)
¶
Chunk a document into semantic segments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
str
|
The document text to chunk. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of text chunks. |
Source code in ontocast/tool/chunk/chunker.py
__init__(chunk_config=None, cache=None, **kwargs)
¶
Initialize the ChunkerTool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_config
|
ChunkConfig | None
|
Chunking configuration. If None, uses default ChunkConfig. |
None
|
cache
|
Cacher | None
|
Optional shared Cacher instance. If None, creates a new one. |
None
|
**kwargs
|
Additional keyword arguments passed to the parent class. |
{}
|
Source code in ontocast/tool/chunk/chunker.py
embed_texts(texts)
¶
Embed short texts with the chunker's model, or None if unavailable.
Exposed so document-type detection can reuse the model already loaded
for semantic chunking instead of constructing a second one. Returns
None -- rather than raising -- when the semantic extras are absent,
so callers degrade to their deterministic tiers exactly as chunking
itself degrades to naive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
list[str]
|
Short strings to embed (headings or sampled paragraphs). |
required |
Returns:
| Type | Description |
|---|---|
list[list[float]] | None
|
One embedding per input, or |
Source code in ontocast/tool/chunk/chunker.py
embeddings()
¶
Embeddings over the process-shared encoder, or None if unavailable.
The encoder is shared with retrieval and entity clustering when their model names match, so this loads no weights of its own in that case, and its inference is serialised against theirs.
Source code in ontocast/tool/chunk/chunker.py
naive_split(doc)
¶
Split text by paragraph/sentence boundaries up to max_size.
Unlike :meth:_naive_chunk, does not enforce min_size filtering.
Source code in ontocast/tool/chunk/chunker.py
size_text(doc)
¶
Split doc to respect min_size / max_size using naive boundaries.
PrepareOptions
dataclass
¶
Options for the chunk preparation pipeline.
Source code in ontocast/tool/chunk/prepare.py
filter_allowlist_param()
¶
Name of the request option that produced :meth:filter_allowlist.
filter_denylist(schema)
¶
Effective exclusion denylist.
None means "use the resolved schema's default_exclude"; an explicit
[] opts out of exclusion entirely; a non-empty list is used as-is.
Source code in ontocast/tool/chunk/prepare.py
needs_section_prepare()
¶
True when a request option explicitly requires section labels.
Section tagging itself is default-on (see CHUNK_SECTION_CLASSIFIER);
this only reports whether the request carries section-dependent options.
Source code in ontocast/tool/chunk/prepare.py
PreparedChunk
dataclass
¶
A prepared text chunk with optional structural metadata and section label.
section_label_source and section_label_confidence record which tier
of the classification cascade decided the label, so a run can be audited
and weak labels can be told from strong ones.
Source code in ontocast/tool/chunk/prepare.py
SectionSelectionEmptyError
¶
Bases: ValueError
A section selection matched no segment in this document.
Distinct from a malformed parameter: target_sections=["reslts"] is
syntactically fine and only turns out to be wrong once this document has
been classified. Under the default CHUNK_SECTION_FILTER_ON_EMPTY=warn
this is a log line and the run continues to an empty graph, which reads
exactly like a document that genuinely had nothing to extract -- telling
those two apart is the whole point of the error mode.
Subclasses :class:ValueError so existing parameter guards keep their
shape. Deliberately not an api-layer error: nothing under tool/
imports ontocast.api, and the parameter here is well-formed.
Source code in ontocast/tool/chunk/prepare.py
prepare_content_units(docling_doc, splitter, config, options, tools=None)
async
¶
Segment, tag, filter, and size document text into prepared chunks.
Section tagging is default-on: the sections-first flow runs unless
CHUNK_SECTION_CLASSIFIER=off (which also disables section filters and
schema default exclusions; explicit section options are ignored with a
warning in that case).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docling_doc
|
DoclingDocument
|
Converted source document. |
required |
splitter
|
ChunkerTool
|
Chunker used to size oversized sections. |
required |
config
|
ChunkConfig
|
Chunk configuration, including the classifier tier. |
required |
options
|
PrepareOptions
|
Per-request section schema and filters. |
required |
tools
|
'ToolBox | None'
|
ToolBox providing the LLM. Required only when
|
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
|
SectionSelectionEmptyError
|
A section allowlist or denylist removed
every segment and |
Source code in ontocast/tool/chunk/prepare.py
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 | |
size_bounded_text(text, config, split_fn, *, separator=DEFAULT_PART_SEPARATOR)
¶
Split text when needed, then enforce OntoCast chunk size bounds.