Standardize on u64 for token counts (#32869)

Previously we were using a mix of `u32` and `usize`, e.g. `max_tokens:
usize, max_output_tokens: Option<u32>` in the same `struct`.

Although [tiktoken](https://github.com/openai/tiktoken) uses `usize`,
token counts should be consistent across targets (e.g. the same model
doesn't suddenly get a smaller context window if you're compiling for
wasm32), and these token counts could end up getting serialized using a
binary protocol, so `usize` is not the right choice for token counts.

I chose to standardize on `u64` over `u32` because we don't store many
of them (so the extra size should be insignificant) and future models
may exceed `u32::MAX` tokens.

Release Notes:

- N/A
This commit is contained in:
Richard Feldman 2025-06-17 10:43:07 -04:00 committed by GitHub
parent a391d67366
commit 5405c2c2d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
32 changed files with 191 additions and 192 deletions

View file

@ -46,7 +46,7 @@ impl From<Role> for String {
pub struct Model {
pub name: String,
pub display_name: Option<String>,
pub max_tokens: usize,
pub max_tokens: u64,
pub supports_tool_calls: bool,
pub supports_images: bool,
}
@ -55,7 +55,7 @@ impl Model {
pub fn new(
name: &str,
display_name: Option<&str>,
max_tokens: Option<usize>,
max_tokens: Option<u64>,
supports_tool_calls: bool,
supports_images: bool,
) -> Self {
@ -76,7 +76,7 @@ impl Model {
self.display_name.as_ref().unwrap_or(&self.name)
}
pub fn max_token_count(&self) -> usize {
pub fn max_token_count(&self) -> u64 {
self.max_tokens
}
@ -256,9 +256,9 @@ pub struct FunctionChunk {
#[derive(Serialize, Deserialize, Debug)]
pub struct Usage {
pub prompt_tokens: u32,
pub completion_tokens: u32,
pub total_tokens: u32,
pub prompt_tokens: u64,
pub completion_tokens: u64,
pub total_tokens: u64,
}
#[derive(Debug, Default, Clone, Deserialize, PartialEq)]
@ -306,8 +306,8 @@ pub struct ModelEntry {
pub compatibility_type: CompatibilityType,
pub quantization: Option<String>,
pub state: ModelState,
pub max_context_length: Option<usize>,
pub loaded_context_length: Option<usize>,
pub max_context_length: Option<u64>,
pub loaded_context_length: Option<u64>,
#[serde(default)]
pub capabilities: Capabilities,
}