Skip to content

Commit

Permalink
[front] Adjust token count estimate (#9961)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdraier authored Jan 15, 2025
1 parent ebd899a commit c9a7bb8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
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

0 comments on commit c9a7bb8

Please sign in to comment.