batch_mode_batch_schema/
batch_success_response_body.rs

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