feat: add thoughts, cached content, and tool use token counts to UsageMetadata (#9) #14
@@ -315,11 +315,20 @@ pub struct SafetyRating {
|
|||||||
pub severity_score: Option<f32>,
|
pub severity_score: Option<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct UsageMetadata {
|
pub struct UsageMetadata {
|
||||||
pub candidates_token_count: Option<u32>,
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub prompt_token_count: Option<u32>,
|
pub prompt_token_count: Option<u32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub cached_content_token_count: Option<u32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub candidates_token_count: Option<u32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub tool_use_prompt_token_count: Option<u32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub thoughts_token_count: Option<u32>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub total_token_count: Option<u32>,
|
pub total_token_count: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -379,7 +388,9 @@ impl GenerateContentResponse {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
use super::{FunctionDeclaration, GenerateContentResponse, GenerateContentResponseResult};
|
use super::{
|
||||||
|
FunctionDeclaration, GenerateContentResponse, GenerateContentResponseResult, UsageMetadata,
|
||||||
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn serializes_function_declaration_with_only_the_set_schema_field() {
|
fn serializes_function_declaration_with_only_the_set_schema_field() {
|
||||||
@@ -412,6 +423,47 @@ mod tests {
|
|||||||
serde_json::from_str::<GenerateContentResponseResult>(input).unwrap();
|
serde_json::from_str::<GenerateContentResponseResult>(input).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parses_usage_metadata_with_all_token_counts() {
|
||||||
|
let input = r#"{
|
||||||
|
"promptTokenCount": 100,
|
||||||
|
"cachedContentTokenCount": 50,
|
||||||
|
"candidatesTokenCount": 200,
|
||||||
|
"toolUsePromptTokenCount": 25,
|
||||||
|
"thoughtsTokenCount": 75,
|
||||||
|
"totalTokenCount": 375
|
||||||
|
}"#;
|
||||||
|
let metadata: UsageMetadata = serde_json::from_str(input).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
metadata,
|
||||||
|
UsageMetadata {
|
||||||
|
prompt_token_count: Some(100),
|
||||||
|
cached_content_token_count: Some(50),
|
||||||
|
candidates_token_count: Some(200),
|
||||||
|
tool_use_prompt_token_count: Some(25),
|
||||||
|
thoughts_token_count: Some(75),
|
||||||
|
total_token_count: Some(375),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serializes_usage_metadata_skipping_none_fields() {
|
||||||
|
let metadata = UsageMetadata {
|
||||||
|
prompt_token_count: Some(10),
|
||||||
|
total_token_count: Some(10),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let val = serde_json::to_value(&metadata).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
val,
|
||||||
|
json!({
|
||||||
|
"promptTokenCount": 10,
|
||||||
|
"totalTokenCount": 10
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn parses_max_tokens_response() {
|
pub fn parses_max_tokens_response() {
|
||||||
let input = r#"{
|
let input = r#"{
|
||||||
|
|||||||
Reference in New Issue
Block a user