Change our tokenizer a bit to be more accurate #616
Merged
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.
Description of the Change
We have a lightweight tokenizer class that attempts to determine how many tokens are in a string. This is done mostly by determining how many characters are in the string and how many characters are in a single token. This is not meant to be 100% accurate but is meant to be close enough for our use case (which is ensuring we stay within the token limits of our model).
Recently I ran across an article that was super long and I received a token length error when trying to process it. In debugging, I found we weren't being aggressive enough with the counting of tokens, and thus we weren't trimming enough of the content to stay within the model limits.
This PR lowers the number of characters per token from 4 to 3.5. This fixed the issue I ran into and seems to be more accurate in counting tokens.
How to test the Change
Send content to OpenAI (either generate an excerpt or generate titles) and ensure things still work and no errors are shown
Changelog Entry
Credits
Props @dkotter
Checklist: