How chonkie-inc/chonkie works
Project Type and Purpose
• Type: A Python library (SDK) for text “chunking.”
• Purpose: Provide a suite of chunkers (token-based, sentence-based, semantic, recursive, code, etc.), embed those chunks, and optionally run them locally or via Chonkie Cloud.
File Structure
• Top-level “src/chonkie” package with subpackages:
– chunker (base classes + specific algorithms)
– cloud/chunker (mirror of chunker but delegating to cloud APIs)
– embeddings (pluggable embedding providers + registry)
– refinery (post-processing refinement steps like overlap removal)
– tokenizer (token counting abstractions)
– types (data models for chunks)
– utils (hubbing, visualization helpers)
• Ancillary files: pyproject.toml (build), tests (unit coverage), docs, CI/CD workflows.
Extract Insights from README
• Core API: user imports a Chunker, calls it with text → returns list of Chunk objects.
• Integrations: supports third-party tokenizers and embedding models (OpenAI, Cohere, etc.).
• Cloud-Ready: mirror implementations under src/chonkie/cloud invoke Chonkie Cloud endpoints.
Main Components
a. API Entrypoint
– Chonkie Python package.
– Exported classes (e.g., RecursiveChunker, SemanticChunker).
b. Core Chunkers
– BaseChunker (abstract) → TokenChunker, SentenceChunker, RecursiveChunker, SDPMChunker, LateChunker, CodeChunker, SemanticChunker.
c. Tokenizer Subsystem
– Abstract Tokenizer interface → Tiktoken, HuggingFace, custom.
d. Embeddings Subsystem
– EmbeddingProvider interface → OpenAI, Cohere, Jina, SentenceTransformer, Model2Vec + registry.
e. Refinery
– Overlap removal, embedding-based splits, etc.
f. Types / Data Models
– Chunk object, with text, token_count, metadata.
g. Cloud Layer
– CloudChunker classes (mirror core chunkers) → HTTP client → Chonkie Cloud API.
h. Utils
– Hub (model download), Viz (plotting chunk boundaries).
i. External Services
– Chonkie Cloud API (hosted service), Third-party embedding endpoints.
Relationships & Data Flow
1. User code calls Chonkie API → Instantiates a Chunker.
2. Chunker receives raw text →
a) Passes text to Tokenizer → obtains token counts / token IDs.
b) Applies chunking logic (sentence/semantic rules).
c) Emits preliminary Chunk objects (types).
d) Optionally calls Refinery on the chunks.
e) Optionally calls EmbeddingProvider to embed each chunk.
3. If using cloud chunker: same interface but forwards text to remote API over HTTPS → Cloud service performs steps 2a–2e and returns chunks.
4. Utilities can be invoked at any stage for logging, visualization, or model management.
Architectural Patterns & Principles
• Strategy Pattern: Chunker algorithms interchangeable via a common interface.
• Adapter/Plugin Pattern: Tokenizers and EmbeddingProviders conform to abstract base classes.
• Mirror Pattern: Local vs Cloud implementations share API but differ in execution context.
• Single Responsibility: Separation of concerns between splitting (chunkers), embedding, and refinement.