Repository navigation
fix(embedding): price non-CJK non-ASCII at the UTF-8 byte ceiling - #2
kuangmi-bit wants to merge 2 commits into
Conversation
A byte-level tokenizer cannot spend more tokens than a string has bytes, but the estimator charged 1 token per non-CJK non-ASCII character: a 4-byte emoji cost 1, Devanagari and Thai cost 1 per 6-byte word-start. For chat text that is the class the guard exists for, the estimate sits below the provider's count and the request is rejected or silently truncated provider-side, which is what issue volcengine#5895 reports. Also adds the Enclosed Ideographic Supplement block (U+1F200-1F2FF) to the CJK ranges; it was falling into the 1-token bucket at 4 bytes per character. Signed-off-by: Mi Kuang <kuangmi@gmail.com>
…d ideographs Signed-off-by: Mi Kuang <kuangmi@gmail.com>
|
Failed to generate code suggestions for PR |
|
@akushonkamen — this is the two gaps from the review on volcengine#5898 (
The one open choice is yours, not a detail I should decide: |
|
Closing this one: both commits are already on your branch ( If the byte-length table for 2-byte scripts turns out to be the shape you want ( |
Follow-up to the review in volcengine#5898 (
#issuecomment-6430153829): the two gaps it names, with tests.What changes
Non-CJK non-ASCII is priced at its UTF-8 byte length, not 1 token/char. For a byte-level tokenizer the byte count is a hard ceiling — an unseen sequence can split into one token per byte — so pricing a 4-byte emoji at 1 was the one place the "upper bound" claim did not hold:
👍🇨🇳👨👩👧नमस्तेสวัสดีCJK keeps
DEFAULT_CJK_TOKENS_PER_CHAR; the docstring now says which of the two is being claimed — a measured ratio with headroom for CJK, a ceiling for everything else.0x1F200–0x1F2FF(Enclosed Ideographic Supplement) added to_CJK_RANGES.🈀🉐🈶were 4 bytes each in the 1-token bucket.Verification
The truncation invariant still holds on emoji-heavy input: for
"👍 中文 hello " * 500and budgets 10/50/500/5000,estimate(truncate(t, n)) <= n.Test changes.
test_non_ascii_non_cjk_costs_at_least_one_token_per_charbecomestest_non_ascii_non_cjk_priced_at_byte_ceiling(equality, not>=), plustest_emoji_keeps_more_than_one_token_per_charand🈀added toCJK_PROBE_CHARS. The three ranges the original issue named, and the suffix-inside-budget behaviour you added, are unchanged.It is a behaviour change for European text too —
caféestimates 3 instead of 2 — which is the cost of an actual upper bound. If you would rather keep the tighter estimate for 2-byte scripts and only raise the ceiling above that, say so and I will narrow it to a per-byte-length table.Not a maintainer; this is the issue author's patch, offered as a PR against your branch rather than a comment so you can take, edit or drop it.