fix(analyze): count prompt characters, not dict length, in count_tokens - #2031
Open
manunicholasjacob wants to merge 1 commit into
Open
fix(analyze): count prompt characters, not dict length, in count_tokens#2031manunicholasjacob wants to merge 1 commit into
manunicholasjacob wants to merge 1 commit into
Conversation
count_tokens called len() directly on r["prompt"], which is a dict for reports written by generators that store structured prompts, so the token count was the number of dict keys rather than the prompt length. The outputs path already had a local _to_text helper for exactly this; hoist it to module scope and use it for the prompt as well. Adds a regression test that fails on the old code path. Signed-off-by: Manu Nicholas Jacob <manunicholasjacob@gmail.com>
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
count_tokensreports the wrong input character count.r["prompt"]is a serialisedTurn, which has exactly two fields (role,content), solen(r["prompt"])is always2regardless of how long the prompt is.The module docstring describes counting "the number of characters sent and received", and
the function already unwraps dict-shaped outputs via a nested
_to_texthelper - theprompt side never got the same treatment.
Effect
A report with 100 attempts, 4000-character prompts and
run.generations: 5printsInput chars: 1000instead of 2,000,000. The figure is independent of prompt length.What this changes
Promotes
_to_textto module level, makes it recursive so it can reachprompt.content.text, and uses it for the prompt as well as the outputs. Reports with aplain-string prompt still work.
Testing
Adds
tests/analyze/test_count_tokens.pycovering a nested prompt and a legacystring prompt. The nested case fails on
mainand passes with this change.