batch_mode_batch_schema/
batch_response_content.rs1crate::ix!();
3
4#[derive(Debug,Serialize,Deserialize)]
5pub struct BatchResponseContent {
6 status_code: u16,
7 request_id: ResponseRequestId,
8 body: BatchResponseBody,
9}
10
11impl BatchResponseContent {
12
13 pub fn status_code(&self) -> u16 {
14 self.status_code
15 }
16
17 pub fn request_id(&self) -> &ResponseRequestId {
18 &self.request_id
19 }
20
21 pub fn body(&self) -> &BatchResponseBody {
22 &self.body
23 }
24
25 pub fn is_success(&self) -> bool {
27 self.status_code == 200
28 }
29
30 pub fn success_body(&self) -> Option<&BatchSuccessResponseBody> {
32 match &self.body {
33 BatchResponseBody::Success(body) => Some(body),
34 _ => None,
35 }
36 }
37
38 pub fn error_body(&self) -> Option<&BatchErrorResponseBody> {
40 match &self.body {
41 BatchResponseBody::Error(body) => Some(body),
42 _ => None,
43 }
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50 use serde_json::json;
51
52 #[test]
53 fn test_success_response_content() {
54 let json_data = json!({
55 "status_code": 200,
56 "request_id": "req_123",
57 "body": {
58 "id": "resp_456",
59 "object": "response",
60 "created": 1627891234,
61 "model": "test-model",
62 "choices": [],
63 "usage": {
64 "prompt_tokens": 100,
65 "completion_tokens": 50,
66 "total_tokens": 150
67 },
68 "system_fingerprint": "fp_abc"
69 }
70 });
71
72 let response: BatchResponseContent = serde_json::from_value(json_data).unwrap();
73 assert_eq!(response.status_code(), 200);
74 assert!(response.is_success());
75 assert_eq!(response.request_id().as_str(), "req_123");
76 assert!(response.success_body().is_some());
77 assert!(response.error_body().is_none());
78 }
79
80 #[test]
81 fn test_error_response_content() {
82 let json_data = json!({
83 "status_code": 400,
84 "request_id": "req_789",
85 "body": {
86 "error": {
87 "message": "Invalid request",
88 "type": "invalid_request_error",
89 "param": "prompt",
90 "code": "invalid_prompt"
91 }
92 }
93 });
94
95 let response: BatchResponseContent = serde_json::from_value(json_data).unwrap();
96 assert_eq!(response.status_code(), 400);
97 assert!(!response.is_success());
98 assert_eq!(response.request_id().as_str(), "req_789");
99 assert!(response.success_body().is_none());
100 assert!(response.error_body().is_some());
101 }
102
103 #[test]
104 fn test_missing_body() {
105 let json_data = json!({
106 "status_code": 500,
107 "request_id": "req_000",
108 "body": {}
109 });
110
111 let result: Result<BatchResponseContent, _> = serde_json::from_value(json_data);
112 assert!(result.is_err(), "Deserialization should fail if body is invalid");
113 }
114
115 #[test]
116 fn test_full_batch_deserialization() {
117 let json = r#"
118 {
119 "id": "batch_req_673d5e5fc66481908be3f82f25681838",
120 "custom_id": "request-0",
121 "response": {
122 "status_code": 200,
123 "request_id": "7b003085175d218b0ceb2b79d7f60bca",
124 "body": {
125 "id": "chatcmpl-AVW7Z2Dd49g7Zq5eVExww6dlKA8T9",
126 "object": "chat.completion",
127 "created": 1732075005,
128 "model": "gpt-4o-2024-08-06",
129 "choices": [{
130 "index": 0,
131 "message": {
132 "role": "assistant",
133 "content": "Response content here.",
134 "refusal": null
135 },
136 "logprobs": null,
137 "finish_reason": "stop"
138 }],
139 "usage": {
140 "prompt_tokens": 1528,
141 "completion_tokens": 2891,
142 "total_tokens": 4419
143 },
144 "system_fingerprint": "fp_7f6be3efb0"
145 }
146 },
147 "error": null
148 }
149 "#;
150
151 let batch_response: BatchResponseRecord = serde_json::from_str(json).unwrap();
153 let response = batch_response.response();
154
155 let body = response.body();
157
158 assert_eq!(body.id(), Some("chatcmpl-AVW7Z2Dd49g7Zq5eVExww6dlKA8T9"));
159 assert_eq!(body.object(), Some("chat.completion"));
160 assert_eq!(body.model(), Some("gpt-4o-2024-08-06"));
161 assert_eq!(body.system_fingerprint(), Some("fp_7f6be3efb0"));
162
163 if let Some(choices) = body.choices() {
165 assert_eq!(choices.len(), 1);
166 let choice = &choices[0];
167 assert_eq!(choice.index(), 0);
168 assert_eq!(choice.finish_reason(), &FinishReason::Stop);
169 assert_eq!(choice.message().content(), "Response content here.");
170 } else {
171 panic!("Expected choices in the response body");
172 }
173 }
174}