A Rust library that learns a vocabulary from your text, turns text into token ids, and turns ids back into the same text. It also predicts the next token from counts.
Research preview, version 0.1.0. It has no release yet; install it from git. The API is small and may change. This README change did not build or run the tests (not verified).
You create a Tokenizer with a context order, train it on a text, and then encode and decode. Training counts n-grams (runs of bytes) in your text in a hash table. Encoding and decoding use that table, so the cost per token is a fixed number of table lookups. There is no merge list to run through.
The same counts also give a next-token prediction. predict returns the most likely next token for a context. distribution returns the ranked list behind it.
The vocabulary is whatever your text produced, so token ids do not match any other tokenizer's ids. No claim is made about how it compares with other tokenizers or models. No benchmark is included in this repository.
use tokenizer::Tokenizer;
let mut tok = Tokenizer::new(3);
tok.train("the quick brown fox jumps over the lazy dog");
let ids = tok.encode("the quick brown fox");
assert_eq!(tok.decode(&ids), "the quick brown fox");Install and test (commands from Cargo.toml; not run for this change):
cargo add --git https://github.com/webfold-ai/tokenizer
cargo testOther public methods in src/lib.rs: vocab_size, contains, add_token, predict, distribution and context_hash. A TokenId is a u32.
- It is not a replacement for the tokenizer of a pretrained model. It has no
tokenizer.json, no special-token table and no subword merges. - It is not async, streaming or
no_std. - It has
#![forbid(unsafe_code)]and one dependency,apeiron, for the hash table. - Round trip:
decode(encode(x))returnsxfor text the tokenizer was trained on, including text that ends in the middle of a character. This is the intended behaviour, stated in the crate documentation.
Apache-2.0. See LICENSE and NOTICE.
See NOTICE. The only dependency is apeiron (Apache-2.0). No third-party source is copied into this repository.
Report vulnerabilities privately. See SECURITY.md. Do not open a public issue.