Skip to content
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

[front] Adjust token count estimate #9961

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions front/lib/tokenization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import config from "./api/config";

export async function tokenCountForTexts(
texts: string[],
model: { providerId: string; modelId: string }
model: { providerId: string; modelId: string; tokenCountAdjustment?: number }
): Promise<Result<Array<number>, Error>> {
const BATCHES_COUNT = 3;
try {
Expand All @@ -32,7 +32,9 @@ export async function tokenCountForTexts(
);
}
for (const tokens of res.value.tokens) {
counts.push(tokens.length);
counts.push(
Math.round(tokens.length * (model.tokenCountAdjustment ?? 1))
);
}
}

Expand Down
8 changes: 8 additions & 0 deletions types/src/front/lib/assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ export type ModelConfigurationType = {
// This meta-prompt is injected into the assistant's system instructions if the assistant is in a tool-use context.
toolUseMetaPrompt?: string;

// Adjust the token count estimation by a ratio. Only needed for anthropic models, where the token count is higher than our estimate
tokenCountAdjustment?: number;

supportsVision: boolean;

// Only used for O-series OpenAI models.
Expand Down Expand Up @@ -400,6 +403,7 @@ export const CLAUDE_3_OPUS_DEFAULT_MODEL_CONFIG: ModelConfigurationType = {
delimitersConfiguration: ANTHROPIC_DELIMITERS_CONFIGURATION,
supportsVision: true,
toolUseMetaPrompt: ANTHROPIC_TOOL_USE_META_PROMPT,
tokenCountAdjustment: 1.15,
};

export const CLAUDE_3_5_SONNET_20240620_DEPRECATED_MODEL_CONFIG: ModelConfigurationType =
Expand All @@ -417,6 +421,7 @@ export const CLAUDE_3_5_SONNET_20240620_DEPRECATED_MODEL_CONFIG: ModelConfigurat
delimitersConfiguration: ANTHROPIC_DELIMITERS_CONFIGURATION,
supportsVision: true,
toolUseMetaPrompt: ANTHROPIC_TOOL_USE_META_PROMPT,
tokenCountAdjustment: 1.15,
};

export const CLAUDE_3_5_SONNET_DEFAULT_MODEL_CONFIG: ModelConfigurationType = {
Expand All @@ -433,6 +438,7 @@ export const CLAUDE_3_5_SONNET_DEFAULT_MODEL_CONFIG: ModelConfigurationType = {
delimitersConfiguration: ANTHROPIC_DELIMITERS_CONFIGURATION,
supportsVision: true,
toolUseMetaPrompt: ANTHROPIC_TOOL_USE_META_PROMPT,
tokenCountAdjustment: 1.15,
};
export const CLAUDE_3_5_HAIKU_DEFAULT_MODEL_CONFIG: ModelConfigurationType = {
providerId: "anthropic",
Expand All @@ -447,6 +453,7 @@ export const CLAUDE_3_5_HAIKU_DEFAULT_MODEL_CONFIG: ModelConfigurationType = {
shortDescription: "Anthropic's cost-effective model.",
isLegacy: false,
supportsVision: false,
tokenCountAdjustment: 1.15,
};
export const CLAUDE_3_HAIKU_DEFAULT_MODEL_CONFIG: ModelConfigurationType = {
providerId: "anthropic",
Expand All @@ -461,6 +468,7 @@ export const CLAUDE_3_HAIKU_DEFAULT_MODEL_CONFIG: ModelConfigurationType = {
shortDescription: "Anthropic's cost-effective model.",
isLegacy: false,
supportsVision: true,
tokenCountAdjustment: 1.15,
};
export const CLAUDE_2_DEFAULT_MODEL_CONFIG: ModelConfigurationType = {
providerId: "anthropic",
Expand Down
Loading