-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Move getAdjustedTokenCountFromModel from CLI to core #9968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+94
−31
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8da07de
Move getAdjustedTokenCountFromModel from CLI to core
RomneyDa d762690
fix: move token adjustment to separate module
RomneyDa 7c32e1e
fix: mistral model matching
RomneyDa 6c7df35
fix: mistral model matching
RomneyDa 318cb66
Merge branch 'main' of https://github.com/continuedev/continue into m…
RomneyDa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { getAdjustedTokenCountFromModel } from "./getAdjustedTokenCount"; | ||
|
|
||
| describe("getAdjustedTokenCountFromModel", () => { | ||
| it("should return base tokens for non-special models", () => { | ||
| expect(getAdjustedTokenCountFromModel(100, "gpt-4")).toBe(100); | ||
| expect(getAdjustedTokenCountFromModel(100, "llama2")).toBe(100); | ||
| expect(getAdjustedTokenCountFromModel(100, "random-model")).toBe(100); | ||
| }); | ||
|
|
||
| it("should apply multiplier for Claude models", () => { | ||
| expect(getAdjustedTokenCountFromModel(100, "claude-3-opus")).toBe(123); | ||
| expect(getAdjustedTokenCountFromModel(100, "claude-3.5-sonnet")).toBe(123); | ||
| expect(getAdjustedTokenCountFromModel(100, "CLAUDE-2")).toBe(123); | ||
| expect(getAdjustedTokenCountFromModel(50, "claude")).toBe(62); // 50 * 1.23 = 61.5, ceiled to 62 | ||
| }); | ||
|
|
||
| it("should apply multiplier for Gemini models", () => { | ||
| expect(getAdjustedTokenCountFromModel(100, "gemini-pro")).toBe(118); | ||
| expect(getAdjustedTokenCountFromModel(100, "gemini-1.5-pro")).toBe(118); | ||
| expect(getAdjustedTokenCountFromModel(100, "GEMINI-flash")).toBe(118); | ||
| expect(getAdjustedTokenCountFromModel(50, "gemini")).toBe(59); // 50 * 1.18 = 59 | ||
| }); | ||
|
|
||
| it("should apply multiplier for Mistral family models", () => { | ||
| expect(getAdjustedTokenCountFromModel(100, "mistral-large")).toBe(126); | ||
| expect(getAdjustedTokenCountFromModel(100, "mixtral-8x7b")).toBe(126); | ||
| expect(getAdjustedTokenCountFromModel(100, "devstral")).toBe(126); | ||
| expect(getAdjustedTokenCountFromModel(100, "CODESTRAL")).toBe(126); | ||
| expect(getAdjustedTokenCountFromModel(50, "mistral")).toBe(63); // 50 * 1.26 = 63 | ||
| }); | ||
|
|
||
| it("should handle edge cases", () => { | ||
| expect(getAdjustedTokenCountFromModel(0, "claude")).toBe(0); | ||
| expect(getAdjustedTokenCountFromModel(1, "gemini")).toBe(2); // 1 * 1.18 = 1.18, ceiled to 2 | ||
| expect(getAdjustedTokenCountFromModel(1000, "mixtral")).toBe(1260); | ||
| }); | ||
|
|
||
| it("should handle empty or undefined model names", () => { | ||
| expect(getAdjustedTokenCountFromModel(100, "")).toBe(100); | ||
| expect(getAdjustedTokenCountFromModel(100, undefined as any)).toBe(100); | ||
| }); | ||
|
|
||
| it("should be case-insensitive", () => { | ||
| expect(getAdjustedTokenCountFromModel(100, "ClAuDe-3-OpUs")).toBe(123); | ||
| expect(getAdjustedTokenCountFromModel(100, "GeMiNi-PrO")).toBe(118); | ||
| expect(getAdjustedTokenCountFromModel(100, "MiXtRaL")).toBe(126); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // Importing a bunch of tokenizers can be very resource intensive (MB-scale per tokenizer) | ||
| // Using token counting APIs (e.g. for anthropic) can be complicated and unreliable in many environments | ||
| // So for now we will just use super fast gpt-tokenizer and apply safety buffers | ||
| // I'm using rough estimates from this article to apply safety buffers to common tokenizers | ||
| // which will have HIGHER token counts than gpt. Roughly using token ratio from article + 10% | ||
| // https://medium.com/@disparate-ai/not-all-tokens-are-created-equal-7347d549af4d | ||
| const ANTHROPIC_TOKEN_MULTIPLIER = 1.23; | ||
| const GEMINI_TOKEN_MULTIPLIER = 1.18; | ||
| const MISTRAL_TOKEN_MULTIPLIER = 1.26; | ||
|
|
||
| /** | ||
| * Adjusts token count based on model-specific tokenizer differences. | ||
| * Since we use llama tokenizer (~= gpt tokenizer) for all models, we apply | ||
| * multipliers for models known to have higher token counts. | ||
| * | ||
| * @param baseTokens - Token count from llama/gpt tokenizer | ||
| * @param modelName - Name of the model | ||
| * @returns Adjusted token count with safety buffer | ||
| */ | ||
| export function getAdjustedTokenCountFromModel( | ||
| baseTokens: number, | ||
| modelName: string, | ||
| ): number { | ||
| let multiplier = 1; | ||
| const lowerModelName = modelName?.toLowerCase() ?? ""; | ||
| if (lowerModelName.includes("claude")) { | ||
| multiplier = ANTHROPIC_TOKEN_MULTIPLIER; | ||
| } else if (lowerModelName.includes("gemini")) { | ||
| multiplier = GEMINI_TOKEN_MULTIPLIER; | ||
| } else if ( | ||
| lowerModelName.includes("stral") || | ||
| lowerModelName.includes("mixtral") | ||
| ) { | ||
| // Mistral family models: mistral, mixtral, codestral, devstral, etc | ||
| multiplier = MISTRAL_TOKEN_MULTIPLIER; | ||
| } | ||
| return Math.ceil(baseTokens * multiplier); | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.