Core
Library for breaking documents into chunks of sentences.
When a text-to-text model (e.g. a large language model with a fixed context size) can not accommodate a large document, this library can help us break the document into chunks of a required maximum length that we can perform inference on.
CharInterval(start_pos=None, end_pos=None)
dataclass
Class for representing a character interval.
Attributes:
| Name | Type | Description |
|---|---|---|
start_pos |
int | None
|
The starting position of the interval (inclusive). |
end_pos |
int | None
|
The ending position of the interval (exclusive). |
TokenUtilError
Bases: BaseException
Error raised when token_util returns unexpected values.
TextChunk(token_interval, tokenized_text, document_text=None)
dataclass
Stores a text chunk with attributes to the source document.
Attributes:
| Name | Type | Description |
|---|---|---|
token_interval |
TokenInterval
|
The token interval of the chunk in the source document. |
tokenized_text |
TokenizedText
|
The source document in its tokenized form as TokenizedText obj. |
document |
TokenizedText
|
The source document. |
Properties: get_tokenized_text: TokenizedText of the current document. chunk_text: Text of the current chunk as string. sanitized_chunk_text Text of the current chunk as a sanitized string. -> _sanitize: Converts all whitespace characters in input text to a single space. char_interval: CharInterval of the chunk.
get_tokenized_text
property
Gets the tokenized text from the source document.
chunk_text
property
Gets the chunk text. Raises an error if document_text is not set.
sanitized_chunk_text
property
Gets the sanitized chunk text.
char_interval
property
Gets the character interval corresponding to the token interval.
Returns:
| Type | Description |
|---|---|
CharInterval
|
data.CharInterval: The character interval for this chunk. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If document_text is not set. |
SentenceIterator(tokenized_text, curr_token_pos=0)
Iterate through sentences of a tokenized text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenized_text
|
TokenizedText
|
Document to iterate through. |
required |
curr_token_pos
|
int
|
Iterate through sentences from this token position. |
0
|
Raises:
| Type | Description |
|---|---|
IndexError
|
if curr_token_pos is not within the document. |
Source code in src/kibad_llm/extractors/chunking_utils/core.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | |
__next__()
Returns next sentence's interval starting from current token position.
Returns:
| Type | Description |
|---|---|
TokenInterval
|
Next sentence token interval starting from current token position. |
Raises:
| Type | Description |
|---|---|
StopIteration
|
If end of text is reached. |
Source code in src/kibad_llm/extractors/chunking_utils/core.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
ChunkIterator(document, max_char_buffer, tokenizer_impl)
Iterate through chunks of a tokenized text.
Chunks may consist of sentences or sentence fragments that can fit into the maximum character buffer that we can run inference on.
Chunk cases:
A) If a sentence length exceeds the max char buffer, then it needs to be broken into chunks that can fit within the max char buffer. We do this in a way that maximizes the chunk length while respecting newlines (if present) and token boundaries. Consider this sentence from a poem by John Donne:
No man is an island,
Entire of itself,
Every man is a piece of the continent,
A part of the main.
With max_char_buffer=40, the chunks are: * "No man is an island,\nEntire of itself," len=38 * "Every man is a piece of the continent," len=38 * "A part of the main." len=19
B) If a single token exceeds the max char buffer, it comprises the whole chunk. Consider the sentence: "This is antidisestablishmentarianism." With max_char_buffer=20, the chunks are: * "This is" len=7 * "antidisestablishmentarianism" len=28 * "." len(1)
C) If multiple whole sentences can fit within the max char buffer, then they are used to form the chunk. Consider the sentences: "Roses are red. Violets are blue. Flowers are nice. And so are you." With max_char_buffer=60, the chunks are: * "Roses are red. Violets are blue. Flowers are nice." len=50 * "And so are you." len=15
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document
|
str
|
Document to chunk. Can be either a string or a tokenized text. |
required |
max_char_buffer
|
int
|
Size of buffer that we can run inference on. |
required |
tokenizer_impl
|
Tokenizer
|
Tokenizer instance to use. |
required |
Source code in src/kibad_llm/extractors/chunking_utils/core.py
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | |
create_token_interval(start_index, end_index)
Creates a token interval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_index
|
int
|
first token's index (inclusive). |
required |
end_index
|
int
|
last token's index + 1 (exclusive). |
required |
Returns:
| Type | Description |
|---|---|
TokenInterval
|
Token interval. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the token indices are invalid. |
Source code in src/kibad_llm/extractors/chunking_utils/core.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
get_token_interval_text(tokenized_text, token_interval)
Get the text within an interval of tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenized_text
|
TokenizedText
|
Tokenized documents. |
required |
token_interval
|
TokenInterval
|
An interval specifying the start (inclusive) and end
(exclusive) indices of the tokens to extract. These indices refer to the
positions in the list of tokens within |
required |
Returns:
| Type | Description |
|---|---|
str
|
Text within the token interval. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the token indices are invalid. |
TokenUtilError
|
If tokenizer_lib.tokens_text returns an empty string. |
Source code in src/kibad_llm/extractors/chunking_utils/core.py
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 | |
get_char_interval(tokenized_text, token_interval)
Returns the char interval corresponding to the token interval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenized_text
|
TokenizedText
|
Document. |
required |
token_interval
|
TokenInterval
|
Token interval. |
required |
Returns:
| Type | Description |
|---|---|
CharInterval
|
Char interval of the token interval of interest. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the token_interval is invalid. |
Source code in src/kibad_llm/extractors/chunking_utils/core.py
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 | |