batch_mode_batch_schema/
batch_success_response_body.rs

1// ---------------- [ File: src/batch_success_response_body.rs ]
2crate::ix!();
3
4#[derive(Debug,Serialize,Deserialize)]
5pub struct BatchSuccessResponseBody {
6    id:                 String,
7    object:             String,
8    created:            u64,
9    model:              String,
10    choices:            Vec<BatchChoice>,
11    usage:              BatchUsage,
12    system_fingerprint: Option<String>,
13}
14
15impl BatchSuccessResponseBody {
16
17    pub fn id(&self) -> &str {
18        &self.id
19    }
20
21    pub fn object(&self) -> &str {
22        &self.object
23    }
24
25    pub fn created(&self) -> u64 {
26        self.created
27    }
28
29    pub fn model(&self) -> &str {
30        &self.model
31    }
32
33    pub fn choices(&self) -> &[BatchChoice] {
34        &self.choices
35    }
36
37    pub fn usage(&self) -> &BatchUsage {
38        &self.usage
39    }
40
41    pub fn system_fingerprint(&self) -> Option<&str> {
42        self.system_fingerprint.as_deref()
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49    use serde_json::json;
50    use crate::BatchChoice;
51
52    #[test]
53    fn test_success_body_full_deserialization() {
54        let json_data = json!({
55            "id": "resp_456",
56            "object": "response",
57            "created": 1627891234,
58            "model": "test-model",
59            "choices": [
60                {
61                    "index": 0,
62                    "message": {
63                        "role": "assistant",
64                        "content": "Test content",
65                        "refusal": null
66                    },
67                    "logprobs": null,
68                    "finish_reason": "stop"
69                }
70            ],
71            "usage": {
72                "prompt_tokens": 100,
73                "completion_tokens": 50,
74                "total_tokens": 150
75            },
76            "system_fingerprint": "fp_abc"
77        });
78
79        let body: BatchSuccessResponseBody = serde_json::from_value(json_data).unwrap();
80        assert_eq!(body.id(), "resp_456");
81        assert_eq!(body.choices().len(), 1);
82        assert_eq!(body.usage().total_tokens(), 150);
83        assert_eq!(body.system_fingerprint(), Some("fp_abc"));
84    }
85
86    #[test]
87    fn test_missing_optional_fields() {
88        let json_data = json!({
89            "id": "resp_789",
90            "object": "response",
91            "created": 1627891234,
92            "model": "test-model",
93            "choices": [],
94            "usage": {
95                "prompt_tokens": 100,
96                "completion_tokens": 50,
97                "total_tokens": 150
98            }
99            // 'system_fingerprint' is missing
100        });
101
102        let body: BatchSuccessResponseBody = serde_json::from_value(json_data).unwrap();
103        assert_eq!(body.system_fingerprint(), None);
104    }
105
106    #[test]
107    fn test_missing_required_fields() {
108        let json_data = json!({
109            "id": "resp_000",
110            "object": "response",
111            "created": 1627891234,
112            // 'model' is missing
113            "choices": [],
114            "usage": {
115                "prompt_tokens": 100,
116                "completion_tokens": 50,
117                "total_tokens": 150
118            }
119        });
120
121        let result: Result<BatchSuccessResponseBody, _> = serde_json::from_value(json_data);
122        assert!(result.is_err(), "Deserialization should fail if required fields are missing");
123    }
124}