Skip to content

Commit

Permalink
Add parsing for detailed token usage information (64bit#282)
Browse files Browse the repository at this point in the history
This update adds functionality to parse the detailed token usage data returned by the OpenAI API.
Reference: https://platform.openai.com/docs/api-reference/chat/object#chat/object-usage

(cherry picked from commit 6858399)
  • Loading branch information
e-max authored and ifsheldon committed Nov 23, 2024
1 parent 384a8ed commit 5c8c8c1
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions async-openai-wasm/src/types/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,32 @@ pub struct CompletionUsage {
pub completion_tokens: u32,
/// Total number of tokens used in the request (prompt + completion).
pub total_tokens: u32,
/// Breakdown of tokens used in the prompt.
pub prompt_tokens_details: PromptTokenDetails,
/// Breakdown of tokens used in a completion.
pub completion_tokens_details: CompletionTokenDetails,
}

/// Breakdown of tokens used in a completion.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
pub struct PromptTokenDetails {
/// Audio input tokens present in the prompt.
#[serde(default)]
pub audio_tokens: u32,
/// Cached tokens present in the prompt.
#[serde(default)]
pub cached_tokens: u32,
}

/// Breakdown of tokens used in a completion.
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
pub struct CompletionTokenDetails {
/// Audio input tokens generated by the model.
#[serde(default)]
audio_tokens: u32,
/// Tokens generated by the model for reasoning.
#[serde(default)]
reasoning_tokens: u32,
}

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
Expand Down

0 comments on commit 5c8c8c1

Please sign in to comment.