portkey_sdk/model/
batches.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5/// Request to create a batch.
6///
7/// # Example
8///
9/// ```rust,ignore
10/// use portkey::model::CreateBatchRequest;
11///
12/// let request = CreateBatchRequest::builder()
13///     .input_file_id("file-abc123")
14///     .endpoint("/v1/chat/completions")
15///     .completion_window("24h")
16///     .build()
17///     .unwrap();
18/// ```
19#[derive(Clone, Debug, Serialize, Deserialize)]
20pub struct CreateBatchRequest {
21    /// The ID of an uploaded file that contains requests for the new batch.
22    pub input_file_id: String,
23
24    /// The endpoint to be used for all requests in the batch.
25    /// Currently supported: /v1/chat/completions, /v1/embeddings, /v1/completions
26    pub endpoint: String,
27
28    /// The time frame within which the batch should be processed.
29    /// Currently only "24h" is supported.
30    pub completion_window: String,
31
32    /// Optional custom metadata for the batch.
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub metadata: Option<HashMap<String, String>>,
35}
36
37impl Default for CreateBatchRequest {
38    fn default() -> Self {
39        Self {
40            input_file_id: String::new(),
41            endpoint: String::new(),
42            completion_window: "24h".to_string(),
43            metadata: None,
44        }
45    }
46}
47
48/// The batch object.
49#[derive(Clone, Debug, Serialize, Deserialize)]
50pub struct Batch {
51    /// The batch identifier.
52    pub id: String,
53
54    /// The object type, which is always "batch".
55    pub object: String,
56
57    /// The OpenAI API endpoint used by the batch.
58    pub endpoint: String,
59
60    /// Error information for the batch.
61    #[serde(skip_serializing_if = "Option::is_none")]
62    pub errors: Option<BatchErrors>,
63
64    /// The ID of the input file for the batch.
65    pub input_file_id: String,
66
67    /// The time frame within which the batch should be processed.
68    pub completion_window: String,
69
70    /// The current status of the batch.
71    pub status: String,
72
73    /// The ID of the file containing the outputs of successfully executed requests.
74    #[serde(skip_serializing_if = "Option::is_none")]
75    pub output_file_id: Option<String>,
76
77    /// The ID of the file containing the outputs of requests with errors.
78    #[serde(skip_serializing_if = "Option::is_none")]
79    pub error_file_id: Option<String>,
80
81    /// The Unix timestamp (in seconds) for when the batch was created.
82    pub created_at: i64,
83
84    /// The Unix timestamp (in seconds) for when the batch started processing.
85    #[serde(skip_serializing_if = "Option::is_none")]
86    pub in_progress_at: Option<i64>,
87
88    /// The Unix timestamp (in seconds) for when the batch will expire.
89    #[serde(skip_serializing_if = "Option::is_none")]
90    pub expires_at: Option<i64>,
91
92    /// The Unix timestamp (in seconds) for when the batch started finalizing.
93    #[serde(skip_serializing_if = "Option::is_none")]
94    pub finalizing_at: Option<i64>,
95
96    /// The Unix timestamp (in seconds) for when the batch was completed.
97    #[serde(skip_serializing_if = "Option::is_none")]
98    pub completed_at: Option<i64>,
99
100    /// The Unix timestamp (in seconds) for when the batch failed.
101    #[serde(skip_serializing_if = "Option::is_none")]
102    pub failed_at: Option<i64>,
103
104    /// The Unix timestamp (in seconds) for when the batch expired.
105    #[serde(skip_serializing_if = "Option::is_none")]
106    pub expired_at: Option<i64>,
107
108    /// The Unix timestamp (in seconds) for when the batch started cancelling.
109    #[serde(skip_serializing_if = "Option::is_none")]
110    pub cancelling_at: Option<i64>,
111
112    /// The Unix timestamp (in seconds) for when the batch was cancelled.
113    #[serde(skip_serializing_if = "Option::is_none")]
114    pub cancelled_at: Option<i64>,
115
116    /// The request counts for different statuses within the batch.
117    #[serde(skip_serializing_if = "Option::is_none")]
118    pub request_counts: Option<BatchRequestCounts>,
119
120    /// Set of key-value pairs that you can attach to an object.
121    #[serde(skip_serializing_if = "Option::is_none")]
122    pub metadata: Option<HashMap<String, String>>,
123}
124
125/// Errors that occurred during batch processing.
126#[derive(Clone, Debug, Serialize, Deserialize)]
127pub struct BatchErrors {
128    /// The object type.
129    pub object: String,
130
131    /// List of errors.
132    pub data: Vec<BatchError>,
133}
134
135/// A single error in a batch.
136#[derive(Clone, Debug, Serialize, Deserialize)]
137pub struct BatchError {
138    /// An error code identifying the error type.
139    pub code: String,
140
141    /// A human-readable message providing more details about the error.
142    pub message: String,
143
144    /// The name of the parameter that caused the error, if applicable.
145    #[serde(skip_serializing_if = "Option::is_none")]
146    pub param: Option<String>,
147
148    /// The line number of the input file where the error occurred, if applicable.
149    #[serde(skip_serializing_if = "Option::is_none")]
150    pub line: Option<i64>,
151}
152
153/// Request counts for different statuses within the batch.
154#[derive(Clone, Debug, Serialize, Deserialize)]
155pub struct BatchRequestCounts {
156    /// Total number of requests in the batch.
157    pub total: i64,
158
159    /// Number of requests that have been completed successfully.
160    pub completed: i64,
161
162    /// Number of requests that have failed.
163    pub failed: i64,
164}
165
166/// Response containing a list of batches.
167#[derive(Clone, Debug, Serialize, Deserialize)]
168pub struct ListBatchesResponse {
169    pub object: String,
170    pub data: Vec<Batch>,
171    pub first_id: Option<String>,
172    pub last_id: Option<String>,
173    pub has_more: bool,
174}