Repacker
Modules for repacking, a process of incorporating retrieved chunks into a prompt as context.
Repacker(method='forward', mode='chunk', delimiter='\n\n', size_func=lambda chunk: len(chunk.content), size_limit=None, context_config=None, pack='truncate', batch_size=None)
Bases: Component
Repacks chunks by running the PACK -> ORDER -> SHAPE stages (see the module docstring).
Attributes:
| Name | Type | Description |
|---|---|---|
method |
RepackMethod
|
The ORDER stage strategy - how chunks are arranged within each batch. |
mode |
RepackerMode
|
The SHAPE stage output - how the ordered batches collapse into the return value ("chunk", "context"). |
pack |
RepackStrategy
|
The PACK stage strategy - how chunks are fitted against |
delimiter |
str
|
The delimiter the SHAPE stage joins chunk contents with. |
size_func |
Callable[[Chunk], int]
|
Function the PACK stage uses to measure a single chunk. |
size_limit |
int | None
|
The maximum total chunk size the PACK stage fits into a batch. |
batch_size |
int | None | Literal['auto']
|
The PACK-stage batching knob. See the constructor's
docstring for the |
strategy |
BaseRepackStrategy
|
The resolved ORDER stage strategy for |
context_config |
ContextConfig | None
|
Context field selection and rendering configuration. |
Initialize the Repacker component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str | RepackMethod
|
The ORDER stage strategy - how chunks are arranged within each batch ("forward", "reverse", "sides"). Defaults to "forward". |
'forward'
|
mode
|
str | RepackerMode
|
The SHAPE stage output ("chunk", "context"). Defaults to "chunk". |
'chunk'
|
delimiter
|
str
|
The delimiter the SHAPE stage joins chunk contents with. Defaults to "\n\n". |
'\n\n'
|
size_func
|
Callable[[Chunk], int]
|
Function used to measure the size of a single chunk. Defaults to the length of the chunk content. |
lambda chunk: len(content)
|
size_limit
|
int | None
|
The maximum allowed total size for a batch. Defaults to None. Note: The size limit only accounts for the chunks, not including the delimiter. |
None
|
context_config
|
ContextConfig | None
|
Configuration for rendering context text from chunks. Defaults to None, in which case legacy str(chunk.content) rendering is used. |
None
|
pack
|
str | RepackStrategy
|
The PACK stage strategy. Defaults to "truncate". |
'truncate'
|
batch_size
|
int | None | Literal['auto']
|
The PACK-stage batching knob:
1. |
None
|
repack(chunks)
async
Repack the input chunks based on the chosen pack, method, mode, and batch_size.
The return type depends on mode and batch_size together:
mode="chunk"withbatch_size=None(default):list[Chunk].mode="chunk"withbatch_sizea positive int or"auto":list[list[Chunk]].mode="context"withbatch_size=None(default):str.mode="context"withbatch_sizea positive int or"auto":list[str].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunks
|
list[Chunk]
|
The input chunks to repack. |
required |
Returns:
| Type | Description |
|---|---|
list[Chunk] | list[list[Chunk]] | str | list[str]
|
list[Chunk] | list[list[Chunk]] | str | list[str]: The repacked chunks, shaped per the combinations above. |
Raises:
| Type | Description |
|---|---|
GenerationValueError
|
If chunks is not a list of Chunk objects. |