chonkiejs

repository·main·Indexed 18 days ago

https://github.com/feyninc/chonkiejs

A lightweight, efficient, and type-safe TypeScript library for text chunking designed for RAG (Retrieval-Augmented Generation) applications. It is a TypeScript port of the Python chonkie library and provides multiple strategies including RecursiveChunker, TokenChunker, TableChunker, FastChunker, SentenceChunker, SemanticChunker, and CodeChunker. The library is organized into @chonkiejs/core for local chunking, @chonkiejs/token for HuggingFace tokenizer support via transformers.js, and @chonkiejs/cloud for cloud-based chunking.

Tokens
43.2K
Snippets
161
Records
209
Agent score
62%

What's inside chonkiejs

  1. Overview of Chonkie packages

    main

    Chonkie is organized into several packages depending on your requirements:

    PackageDescription
    @chonkiejs/coreLocal chunking (Recursive, Token, Sentence, Semantic, Code, Table, Fast) with character-based tokenization.
    @chonkiejs/cloudCloud-based chunkers (Semantic, Neural, Code, etc.) via api.chonkie.ai.
    @chonkiejs/tokenHuggingFace tokenizer support for core chunkers.
  2. Available Chunking Strategies in ChonkieJS

    main

    ChonkieJS provides several chunking strategies tailored to different text processing requirements. Depending on your data type and performance needs, you can choose from the following:

    • RecursiveChunker: Hierarchical splitting using customizable rules. Recommended for general-purpose chunking.
    • TokenChunker: Fixed-size chunking based on token counts. Best for maintaining consistent chunk sizes.
    • SentenceChunker: Splits text at sentence boundaries to preserve semantic completeness.
    • SemanticChunker: Groups content based on semantic similarity (requires custom embeddings).
    • CodeChunker: AST-aware chunking specifically designed for source code files.
    • TableChunker: Chunks markdown or HTML tables by row or token count.
    • FastChunker: High-performance, byte-based chunking utilizing SIMD acceleration.
  3. Common Interface for ChonkieJS Chunkers

    main

    All chunkers in ChonkieJS follow a consistent asynchronous interface. They are initialized via an async create() factory method and provide methods for both single-text and batch processing.

    To use a chunker, call .chunk(text) for a single string or .chunkBatch(texts) for an array of strings. Both methods return a Promise<Chunk[]>.

    // Chunk a single text
    const chunks = await chunker.chunk(text);
    
    // Chunk multiple texts
    const batchResults = await chunker.chunkBatch(texts);
  4. How @chonkiejs/token integrates with @chonkiejs/core

    main

    The integration follows a dynamic import pattern to keep the core library lightweight:

    1. When you call Tokenizer.create('model-name') in @chonkiejs/core, the core attempts to dynamically import @chonkiejs/token.
    2. If installed: The core uses the HuggingFaceTokenizer to perform the requested operations.
    3. If not installed: The core will throw a helpful error message instructing you to install the package.
  5. Initialize chunkers using the async create() factory method

    main

    All chunkers in chonkiejs follow a factory pattern for initialization. Because chunkers may require asynchronous setup (e.g., loading models or tokenizers), you must use the static create() method instead of the new keyword.

    Example for any chunker (e.g., RecursiveChunker):

    const chunker = await RecursiveChunker.create(options);
    // All chunkers: RecursiveChunker, TokenChunker, SentenceChunker, SemanticChunker, CodeChunker, TableChunker, FastChunker
    const chunker = await RecursiveChunker.create(options);
  6. Available Chunkers in @chonkiejs/core

    main

    Chonkie provides several specialized chunking strategies depending on your data type and performance requirements:

    NameDescription
    RecursiveChunkerRecursively splits text using hierarchical rules (paragraphs → sentences → punctuation → words → characters). Each level only activates if chunks exceed the configured size.
    TokenChunkerSplits text into fixed-size token chunks with optional overlap. Uses character-based tokenization by default, or HuggingFace models with @chonkiejs/token.
    TableChunkerSplits Markdown or HTML tables into smaller table chunks while repeating the original header in each chunk. Supports row-based and token-based modes.
    FastChunkerUses byte-based boundary detection through @chonkiejs/chunk for very fast chunking with delimiter or pattern controls.
  7. Partition data using Namespaces in PineconeHandshake

    main

    You can use the namespace parameter to partition different datasets within a single Pinecone index. This allows you to maintain separate logical collections (e.g., 'docs' vs 'faq') using the same index instance.

    When a namespace is configured, the count() method returns the record count specifically for that namespace. If no namespace is set, count() returns the total count for the entire index.

    const docsHandshake = new PineconeHandshake({
      index,
      namespace: 'docs',
      embeddings: embed,
    });
    
    const faqHandshake = new PineconeHandshake({
      index,
      namespace: 'faq',
      embeddings: embed,
    });