batch_mode_batch_schema/
batch_response_record.rs

1// ---------------- [ File: src/batch_response_record.rs ]
2crate::ix!();
3
4#[derive(Debug,Serialize,Deserialize)]
5pub struct BatchResponseRecord {
6    id:        BatchRequestId,
7    custom_id: CustomRequestId,
8    response:  BatchResponseContent,
9    error:     Option<serde_json::Value>, // Assuming it's always null or can be ignored
10}
11
12impl BatchResponseRecord {
13
14    pub fn id(&self) -> &BatchRequestId {
15        &self.id
16    }
17
18    pub fn custom_id(&self) -> &CustomRequestId {
19        &self.custom_id
20    }
21
22    pub fn response(&self) -> &BatchResponseContent {
23        &self.response
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30    use serde_json::json;
31
32    // Additional test to deserialize the provided batch line
33    #[test]
34    fn test_full_batch_deserialization() {
35        let json = r#"
36        {
37            "id": "batch_req_673d5e5fc66481908be3f82f25681838",
38            "custom_id": "request-0",
39            "response": {
40                "status_code": 200,
41                "request_id": "7b003085175d218b0ceb2b79d7f60bca",
42                "body": {
43                    "id": "chatcmpl-AVW7Z2Dd49g7Zq5eVExww6dlKA8T9",
44                    "object": "chat.completion",
45                    "created": 1732075005,
46                    "model": "gpt-4o-2024-08-06",
47                    "choices": [{
48                        "index": 0,
49                        "message": {
50                            "role": "assistant",
51                            "content": "Response content here.",
52                            "refusal": null
53                        },
54                        "logprobs": null,
55                        "finish_reason": "stop"
56                    }],
57                    "usage": {
58                        "prompt_tokens": 1528,
59                        "completion_tokens": 2891,
60                        "total_tokens": 4419
61                    }
62                }
63            },
64            "error": null
65        }
66        "#;
67
68        let batch_response: BatchResponseRecord = serde_json::from_str(json).unwrap();
69        assert_eq!(batch_response.id, BatchRequestId::new("batch_req_673d5e5fc66481908be3f82f25681838"));
70        assert_eq!(batch_response.custom_id, CustomRequestId::new("request-0"));
71        assert!(batch_response.error.is_none());
72
73        let response = batch_response.response;
74        assert_eq!(response.status_code(), 200);
75        assert_eq!(response.request_id(), "7b003085175d218b0ceb2b79d7f60bca");
76
77        let body = response.body();
78        assert_eq!(body.id(), Some("chatcmpl-AVW7Z2Dd49g7Zq5eVExww6dlKA8T9"));
79        assert_eq!(body.object(), Some("chat.completion"));
80        assert_eq!(body.model(), Some("gpt-4o-2024-08-06"));
81
82        let choices = body.choices();
83
84        assert!(choices.is_some());
85        let choices = choices.unwrap();
86
87        assert_eq!(choices.len(), 1);
88
89        let choice = &choices[0];
90        assert_eq!(choice.index(), 0);
91        assert_eq!(choice.finish_reason(), &FinishReason::Stop);
92        assert_eq!(choice.message().role(), &MessageRole::Assistant);
93        assert_eq!(choice.message().content(), "Response content here.");
94    }
95}