{ "cells": [ { "cell_type": "code", "execution_count": 3, "id": "cf52b7c7", "metadata": {}, "outputs": [], "source": [ "# Copyright (c) 2026 Microsoft Corporation.\n", "# Licensed under the MIT License." ] }, { "cell_type": "markdown", "id": "37a05202", "metadata": {}, "source": [ "## Using the factory via helper util example\n", "\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 4, "id": "027e9c45", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[TextChunk(original='This is a', text='This is a', index=0, start_char=0, end_char=8, token_count=3), TextChunk(original=' test. Another', text=' test. Another', index=1, start_char=9, end_char=22, token_count=3), TextChunk(original=' sentence. And', text=' sentence. And', index=2, start_char=23, end_char=36, token_count=3), TextChunk(original=' another one.', text=' another one.', index=3, start_char=37, end_char=49, token_count=3)]\n" ] } ], "source": [ "import tiktoken\n", "from graphrag_chunking.chunker_factory import create_chunker\n", "from graphrag_chunking.chunking_config import ChunkingConfig\n", "\n", "tokenizer = tiktoken.get_encoding(\"o200k_base\")\n", "config = ChunkingConfig(\n", " strategy=\"tokens\", # type: ignore\n", " size=3,\n", " overlap=0,\n", ")\n", "chunker = create_chunker(config, tokenizer.encode, tokenizer.decode)\n", "\n", "print(chunker.chunk(\"This is a test. Another sentence. And another one.\"))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.9" } }, "nbformat": 4, "nbformat_minor": 5 }