top | item 42100819

Show HN: Chonkie – A Fast, Lightweight Text Chunking Library for RAG

199 points| bhavnicksm | 1 year ago |github.com

I built Chonkie because I was tired of rewriting chunking code for RAG applications. Existing libraries were either too bloated (80MB+) or too basic, with no middle ground.

Core features:

- 21MB default install vs 80-171MB alternatives

- 33x faster token chunking than popular alternatives

- Supports multiple chunking strategies: token, word, sentence, and semantic

- Works with all major tokenizers (transformers, tokenizers, tiktoken)

- Zero external dependencies for basic functionality

Technical optimizations:

- Uses tiktoken with multi-threading for faster tokenization

- Implements aggressive caching and precomputation

- Running mean pooling for efficient semantic chunking

- Modular dependency system (install only what you need)

Benchmarks and code: https://github.com/bhavnicksm/chonkie

Looking for feedback on the architecture and performance optimizations. What other chunking strategies would be useful for RAG applications?

36 comments

order

mattmein|1 year ago

Also check out https://github.com/D-Star-AI/dsRAG/ for a bit more involved chunking strategy.

cadence-|1 year ago

This looks pretty amazing. I will take it for a spin next week. I want to make a RAG that will answer questions related to my new car. The manual is huge and it is often hard to find answers in it, so I think this will be a big help to owners of the same car. I think your library can help me chunk that huge PDF easily.

simonw|1 year ago

Would it make sense for this to offer a chunking strategy that doesn't need a tokenizer at all? I love the goal to keep it small, but "tokenizers" is still a pretty huge dependency (and one that isn't currently compatible with Python 3.13).

I've been hoping to find an ultra light-weight chunking library that can do things like very simple regex-based sentence/paragraph/markdown-aware chunking with minimal additional dependencies.

parhamn|1 year ago

Across a broad enough dataset (char count / 4) is very close to the actual token count in english -- we verified across millions of queries. We had to switch to using an actual tokenizer for chinese and other unicode languages, as that simple formula misses the mark for context stuffing.

The more complicated stuff is the effective bin-packing problem that emerges depending on how much different contextual sources you have.

andai|1 year ago

I made a rudimentary semantic chunking in just a few lines of code.

I just removed one sentence at a time from the left until there was a jump in the embedding distance. Then repeated for the right side.

bhavnicksm|1 year ago

Thank you so much for giving Chonkie a chance! Just to note Chonkie is still in beta mode (with v0.1.2 running) with a bunch of things planned for it. It's an initial working version, which seemed promising enough to present.

I hope that you will stick with Chonkie for the journey of making the 'perfect' chunking library!

Thanks again!

mixeden|1 year ago

> Token Chunking: 33x faster than the slowest alternative

1) what

petesergeant|1 year ago

> What other chunking strategies would be useful for RAG applications?

I’m using o1-preview for chunking, creating summary subdocuments.

bhavnicksm|1 year ago

That's pretty cool! I believe a research paper called LumberChunker recently evaluated that to be pretty decent as well.

Thanks for responding, I'll try to make it easier to use something like that in Chonkie in the future!

vlovich123|1 year ago

Out of curiosity where does the 21 MiB come from? The codebase clone is 1.2 MiB and the src folder is only 68 KiB.

ekianjo|1 year ago

Dependencies in the venv?

Dowwie|1 year ago

When would you ever want anything other than Semantic chunking? Cutting chunks into fixed lengths is fast, but it's arbitrarily encoding potentially dissimilar information.

samlinnfer|1 year ago

How does it work for code? (Chunking code that is)

nostrebored|1 year ago

Poorly, just like it does for text.

Chunking is easily where all of these problems die beyond PoC scale.

I’ve talked to multiple code generation companies in the past week — most are stuck with BM25 and taking in whole files.

bhavnicksm|1 year ago

Right now, we haven't worked on adding support for code -- some things like comments (#, //) have punctuations that adversely affect chunking, along with indentation and other issues.

But, it's on the roadmap, so please hold on!

bravura|1 year ago

One thing I've been looking for, and was a bit tricky implementing myself to be very fast, is this:

I have a particular max token length in mind, and I have a tokenizer like tiktoken. I have a string and I want to quickly find the maximum length truncation of the string that is <= target max token length.

Does chonkie handle this?

bhavnicksm|1 year ago

I don't fully understand what you mean by "maximum length truncation of the string"; but if you're talking about splitting the sentence into 'chunks' which have token counts less than a pre-specified max_token length then, yes!

Is that what you meant?

will-burner|1 year ago

Love the name Chonkie and Moo Deng, the hippo, as the image/logo!!

edit: Get some Moo Deng jokes in the docs!

spullara|1 year ago

21MB? to split text? have you analyzed the footprint?

bhavnicksm|1 year ago

Just to clarify, the 21MB is the size of the package itself! Other package sizes are way larger.

Memory footprint of the chunking itself would vary widely based on the dataset, and it's not something we tested on... usually other providers don't test it either, as long as it doesn't bust up the computer/server.

If saving memory during runtime is important for your application, let me know! I'd run some benchmarks for it...

Thanks!

trwhite|1 year ago

What's RAG?

adwf|1 year ago

Retrieval-Augmented Generation (AI).

Think of it as if ChatGPT (or other models) didn't just have the embedded unstructured knowledge in their weights from learning, but also an extra DB on the side with specific structured knowledge that it can lookup on the fly.

opendang|1 year ago

[deleted]

xivusr|1 year ago

IMO comments like this go against the spirit of HN - why not offer more constructive feedback? Implying defects without suggestions on how to improve (or proof) is low effort and what I expect on a YouTube comment thread, not HN.

ilidur|1 year ago

Review: Chonkie is an MIT license project to help with chunking your sentences. It boasts fixed length, word length, sentence and semantic methods. The instructions for installing and usage are simple.

The Benchmark numbers are massaged to look really impressive but upon scrutiny the improvements are at most <1.86x compared to the leading product LangChain in a further page describing the measurements. It claims to beat it on all aspects but where it gets close, the author's library uses a warmed up version so the numbers are not comparable. The author acknowledged this but didn't change the methodology to provide a direct comparison.

The author is Bhavnick S. Minhas, an early career ML engineer with both research and industry experience and very prolific with his GitHub contributions.