batch_mode_batch_schema/
batch_usage.rs1crate::ix!();
3
4#[derive(Debug,Serialize,Deserialize)]
5pub struct BatchUsage {
6 prompt_tokens: u32,
7 completion_tokens: u32,
8 total_tokens: u32,
9 prompt_tokens_details: Option<BatchTokenDetails>,
10 completion_tokens_details: Option<BatchTokenDetails>,
11}
12
13impl BatchUsage {
14
15 pub fn prompt_tokens(&self) -> u32 {
16 self.prompt_tokens
17 }
18
19 pub fn completion_tokens(&self) -> u32 {
20 self.completion_tokens
21 }
22
23 pub fn total_tokens(&self) -> u32 {
24 self.total_tokens
25 }
26
27 }
29
30#[cfg(test)]
31mod tests {
32 use super::*;
33 use serde_json::json;
34
35 #[test]
36 fn test_usage_deserialization() {
37 let json_data = json!({
38 "prompt_tokens": 100,
39 "completion_tokens": 50,
40 "total_tokens": 150
41 });
42
43 let usage: BatchUsage = serde_json::from_value(json_data).unwrap();
44 assert_eq!(usage.prompt_tokens(), 100);
45 assert_eq!(usage.completion_tokens(), 50);
46 assert_eq!(usage.total_tokens(), 150);
47 }
48
49 #[test]
50 fn test_usage_missing_fields() {
51 let json_data = json!({
52 "prompt_tokens": 100,
53 "total_tokens": 150
54 });
56
57 let result: Result<BatchUsage, _> = serde_json::from_value(json_data);
58 assert!(result.is_err(), "Deserialization should fail if fields are missing");
59 }
60}