CodeChunker splits code into chunks based on its structure, leveraging Abstract Syntax Trees (ASTs) to create contextually relevant segments.
Overview
- Supports 165+ languages
- Powered by tree-sitter-language-pack
- Auto language detection via Magika, a language detection library made by Google
Supported Languages
Show all supported languages
Show all supported languages
API Reference
To use theCodeChunker via the API, check out the API reference documentation.
Installation
CodeChunker requires additional dependencies for code parsing. You can install it with:For installation instructions, see the Installation
Guide.
Initialization
Automatic language detection using Magika can impact performance. For best results, specify the language directly via the
language parameter.Parameters
str
required
The programming language of the code. Accepts languages supported by
tree-sitter-language-pack.Union[str, Callable, Any]
default:"character"
Tokenizer or token counting function to use for measuring chunk size.
int
default:"2048"
Maximum number of tokens per chunk.
bool
default:"False"
Whether to include AST node information (Note: with the base Chunk type, node
information is not stored).
Usage
Single Code Chunking
- Python
- JavaScript
Batch Chunking
- Python
- JavaScript
Using as a Callable
Return Type
CodeChunker returns chunks asChunk objects:
As of version 1.3.0, CodeChunker returns the base
Chunk type instead of the
specialized CodeChunk type. This simplifies integration with other chunkers
and refineries.