✨ feat(mq-lang,mq-run): add token_count(text, model) builtin#2008
Open
harehare wants to merge 2 commits into
Open
✨ feat(mq-lang,mq-run): add token_count(text, model) builtin#2008harehare wants to merge 2 commits into
harehare wants to merge 2 commits into
Conversation
Add a native token_count(text, model) builtin for LLM context-window budgeting (e.g. select(token_count(.) < N)), pairing with the existing toon.mq module for minimizing token usage. Two-tier: a dependency-free chars-per-token heuristic (adjusted by detected script — CJK tokenizes much denser than Latin) ships by default and costs nothing in binary size, including for mq-wasm. Exact counting via tiktoken-rs is opt-in behind a new `tiktoken` Cargo feature (mq-lang and, forwarded, mq-run), since resolving `model` at runtime makes every vendored encoding reachable and unremovable by dead-code elimination.
Support token_count(text) alongside token_count(text, model), so callers who only want the heuristic estimate aren't forced to name a model just to satisfy arity. Since builtin dispatch resolves pipe injection purely by argument count, this collides with the previous text | token_count(model) idiom (1 explicit arg); that form is replaced by explicit two-arg calls, e.g. token_count(md, "gpt-4").
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a native token_count(text, model) builtin for LLM context-window budgeting (e.g. select(token_count(.) < N)), pairing with the existing toon.mq module for minimizing token usage. Two-tier: a dependency-free chars-per-token heuristic (adjusted by detected script — CJK tokenizes much denser than Latin) ships by default and costs nothing in binary size, including for mq-wasm. Exact counting via tiktoken-rs is opt-in behind a new
tiktokenCargo feature (mq-lang and, forwarded, mq-run), since resolvingmodelat runtime makes every vendored encoding reachable and unremovable by dead-code elimination.Type of Change
Checklist
cargo fmtandcargo clippyand addressed any warningsjust test-alland all tests pass/docs, crateREADME.md) if neededAdditional Context