batching
Batch text chunks together to fit within a max tokens per batch.
The max tokens per batch is expected to be a subset of the model's context window size.
Functions:
| Name | Description |
|---|---|
batch_chunks_by_token_limit |
Batch text chunks together to fit within a max tokens per batch. |
batch_chunks_by_token_limit
๐
batch_chunks_by_token_limit(chunks: Iterable[str], max_tokens_per_batch: TokenCount, joiner: str = '\n\n---\n\n') -> Iterator[str]
Batch text chunks together to fit within a max tokens per batch.
This function takes an iterable of text chunks and combines them into larger batches that will fit within the specified max tokens per batch. It uses token estimation to determine how many chunks can be combined safely.
Chunks are combined with a joiner (defined by the joiner parameter) to help
distinguish between different chunks in the same batch.
This batching optimizes API usage by reducing the number of calls to the LLM provider, which can help avoid rate limiting errors for repositories with many commits or other text chunks.
The size of each chunk is overestimated to be conservative, that is, to ensure that the chunk will fit within the max tokens per batch. The size of each batch is estimated by summing the sizes of its chunks plus the joiner tokens, which is also an approximation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Iterable[str]
|
An iterable of strings, where each string is a chunk of text. |
required |
|
TokenCount
|
The maximum number of tokens allowed per batch. |
required |
|
str
|
The string to use for joining chunks when batching them together. |
'\n\n---\n\n'
|
Yields:
| Type | Description |
|---|---|
str
|
Batches of text chunks, each fitting within the max tokens per batch. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If max_tokens_per_batch is not positive. |