Back to Graphrag

Copyright (c) 2026 Microsoft Corporation.

packages/graphrag-chunking/example_notebooks/factory_helper_util_example.ipynb

3.0.9885 B
Original Source
python
# Copyright (c) 2026 Microsoft Corporation.
# Licensed under the MIT License.

Using the factory via helper util example

The create_chunker factory function provides a configuration-driven approach to instantiate chunkers by accepting a ChunkingConfig object that specifies the chunking strategy and parameters. This allows for more flexible and maintainable code by separating chunker configuration from direct instantiation.

python
import tiktoken
from graphrag_chunking.chunker_factory import create_chunker
from graphrag_chunking.chunking_config import ChunkingConfig

tokenizer = tiktoken.get_encoding("o200k_base")
config = ChunkingConfig(
    strategy="tokens",  # type: ignore
    size=3,
    overlap=0,
)
chunker = create_chunker(config, tokenizer.encode, tokenizer.decode)

print(chunker.chunk("This is a test. Another sentence. And another one."))