openai-ergonomic 0.5.2

Ergonomic Rust wrapper for OpenAI API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Batch API builders.
//!
//! This module provides ergonomic builders for `OpenAI` Batch API operations,
//! which allow you to send asynchronous groups of requests with 24-hour turnaround
//! and 50% cost reduction compared to synchronous API calls.

use std::collections::HashMap;

/// Builder for creating batch jobs.
///
/// Batch jobs allow you to process multiple requests asynchronously at a lower cost
/// with longer processing time (up to 24 hours).
#[derive(Debug, Clone)]
pub struct BatchJobBuilder {
    input_file_id: String,
    endpoint: BatchEndpoint,
    completion_window: BatchCompletionWindow,
    metadata: HashMap<String, String>,
}

/// Supported endpoints for batch processing.
#[derive(Debug, Clone)]
pub enum BatchEndpoint {
    /// Chat completions endpoint
    ChatCompletions,
    /// Embeddings endpoint
    Embeddings,
    /// Completions endpoint (legacy)
    Completions,
}

/// Completion window for batch jobs.
#[derive(Debug, Clone)]
pub enum BatchCompletionWindow {
    /// Complete within 24 hours
    Hours24,
}

impl BatchJobBuilder {
    /// Create a new batch job builder.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use openai_ergonomic::builders::batch::{BatchJobBuilder, BatchEndpoint};
    ///
    /// let builder = BatchJobBuilder::new("file-batch-input", BatchEndpoint::ChatCompletions);
    /// ```
    #[must_use]
    pub fn new(input_file_id: impl Into<String>, endpoint: BatchEndpoint) -> Self {
        Self {
            input_file_id: input_file_id.into(),
            endpoint,
            completion_window: BatchCompletionWindow::Hours24,
            metadata: HashMap::new(),
        }
    }

    /// Set the completion window for the batch job.
    #[must_use]
    pub fn completion_window(mut self, window: BatchCompletionWindow) -> Self {
        self.completion_window = window;
        self
    }

    /// Add metadata to the batch job.
    #[must_use]
    pub fn metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Get the input file ID.
    #[must_use]
    pub fn input_file_id(&self) -> &str {
        &self.input_file_id
    }

    /// Get the endpoint for this batch job.
    #[must_use]
    pub fn endpoint(&self) -> &BatchEndpoint {
        &self.endpoint
    }

    /// Get the completion window.
    #[must_use]
    pub fn completion_window_ref(&self) -> &BatchCompletionWindow {
        &self.completion_window
    }

    /// Get the metadata.
    #[must_use]
    pub fn metadata_ref(&self) -> &HashMap<String, String> {
        &self.metadata
    }

    /// Check if metadata is empty.
    #[must_use]
    pub fn has_metadata(&self) -> bool {
        !self.metadata.is_empty()
    }
}

/// Builder for listing batch jobs.
#[derive(Debug, Clone, Default)]
pub struct BatchJobListBuilder {
    after: Option<String>,
    limit: Option<i32>,
}

impl BatchJobListBuilder {
    /// Create a new batch job list builder.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the cursor for pagination.
    #[must_use]
    pub fn after(mut self, cursor: impl Into<String>) -> Self {
        self.after = Some(cursor.into());
        self
    }

    /// Set the maximum number of jobs to return.
    #[must_use]
    pub fn limit(mut self, limit: i32) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Get the pagination cursor.
    #[must_use]
    pub fn after_ref(&self) -> Option<&str> {
        self.after.as_deref()
    }

    /// Get the limit.
    #[must_use]
    pub fn limit_ref(&self) -> Option<i32> {
        self.limit
    }
}

/// Builder for retrieving batch job details.
#[derive(Debug, Clone)]
pub struct BatchJobRetrievalBuilder {
    batch_id: String,
}

impl BatchJobRetrievalBuilder {
    /// Create a new batch job retrieval builder.
    #[must_use]
    pub fn new(batch_id: impl Into<String>) -> Self {
        Self {
            batch_id: batch_id.into(),
        }
    }

    /// Get the batch ID.
    #[must_use]
    pub fn batch_id(&self) -> &str {
        &self.batch_id
    }
}

/// Builder for cancelling batch jobs.
#[derive(Debug, Clone)]
pub struct BatchJobCancelBuilder {
    batch_id: String,
}

impl BatchJobCancelBuilder {
    /// Create a new batch job cancel builder.
    #[must_use]
    pub fn new(batch_id: impl Into<String>) -> Self {
        Self {
            batch_id: batch_id.into(),
        }
    }

    /// Get the batch ID.
    #[must_use]
    pub fn batch_id(&self) -> &str {
        &self.batch_id
    }
}

/// Helper function to create a chat completions batch job.
#[must_use]
pub fn batch_chat_completions(input_file_id: impl Into<String>) -> BatchJobBuilder {
    BatchJobBuilder::new(input_file_id, BatchEndpoint::ChatCompletions)
}

/// Helper function to create an embeddings batch job.
#[must_use]
pub fn batch_embeddings(input_file_id: impl Into<String>) -> BatchJobBuilder {
    BatchJobBuilder::new(input_file_id, BatchEndpoint::Embeddings)
}

/// Helper function to create a completions batch job.
#[must_use]
pub fn batch_completions(input_file_id: impl Into<String>) -> BatchJobBuilder {
    BatchJobBuilder::new(input_file_id, BatchEndpoint::Completions)
}

/// Helper function to create a batch job with metadata.
#[must_use]
#[allow(clippy::implicit_hasher)]
pub fn batch_job_with_metadata(
    input_file_id: impl Into<String>,
    endpoint: BatchEndpoint,
    metadata: HashMap<String, String>,
) -> BatchJobBuilder {
    let mut builder = BatchJobBuilder::new(input_file_id, endpoint);
    for (key, value) in metadata {
        builder = builder.metadata(key, value);
    }
    builder
}

/// Helper function to list batch jobs.
#[must_use]
pub fn list_batch_jobs() -> BatchJobListBuilder {
    BatchJobListBuilder::new()
}

/// Helper function to retrieve a specific batch job.
#[must_use]
pub fn get_batch_job(batch_id: impl Into<String>) -> BatchJobRetrievalBuilder {
    BatchJobRetrievalBuilder::new(batch_id)
}

/// Helper function to cancel a batch job.
#[must_use]
pub fn cancel_batch_job(batch_id: impl Into<String>) -> BatchJobCancelBuilder {
    BatchJobCancelBuilder::new(batch_id)
}

impl std::fmt::Display for BatchEndpoint {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BatchEndpoint::ChatCompletions => write!(f, "/v1/chat/completions"),
            BatchEndpoint::Embeddings => write!(f, "/v1/embeddings"),
            BatchEndpoint::Completions => write!(f, "/v1/completions"),
        }
    }
}

impl std::fmt::Display for BatchCompletionWindow {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BatchCompletionWindow::Hours24 => write!(f, "24h"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_batch_job_builder_new() {
        let builder = BatchJobBuilder::new("file-input", BatchEndpoint::ChatCompletions);

        assert_eq!(builder.input_file_id(), "file-input");
        match builder.endpoint() {
            BatchEndpoint::ChatCompletions => {}
            _ => panic!("Expected ChatCompletions endpoint"),
        }
        assert!(!builder.has_metadata());
    }

    #[test]
    fn test_batch_job_builder_with_metadata() {
        let builder = BatchJobBuilder::new("file-input", BatchEndpoint::Embeddings)
            .metadata("project", "test-project")
            .metadata("version", "v1");

        assert!(builder.has_metadata());
        assert_eq!(builder.metadata_ref().len(), 2);
        assert_eq!(
            builder.metadata_ref().get("project"),
            Some(&"test-project".to_string())
        );
        assert_eq!(
            builder.metadata_ref().get("version"),
            Some(&"v1".to_string())
        );
    }

    #[test]
    fn test_batch_job_builder_completion_window() {
        let builder = BatchJobBuilder::new("file-input", BatchEndpoint::ChatCompletions)
            .completion_window(BatchCompletionWindow::Hours24);

        match builder.completion_window_ref() {
            BatchCompletionWindow::Hours24 => {}
        }
    }

    #[test]
    fn test_batch_job_list_builder() {
        let builder = BatchJobListBuilder::new().after("batch-123").limit(10);

        assert_eq!(builder.after_ref(), Some("batch-123"));
        assert_eq!(builder.limit_ref(), Some(10));
    }

    #[test]
    fn test_batch_job_retrieval_builder() {
        let builder = BatchJobRetrievalBuilder::new("batch-456");
        assert_eq!(builder.batch_id(), "batch-456");
    }

    #[test]
    fn test_batch_job_cancel_builder() {
        let builder = BatchJobCancelBuilder::new("batch-789");
        assert_eq!(builder.batch_id(), "batch-789");
    }

    #[test]
    fn test_batch_chat_completions_helper() {
        let builder = batch_chat_completions("file-input");
        assert_eq!(builder.input_file_id(), "file-input");
        match builder.endpoint() {
            BatchEndpoint::ChatCompletions => {}
            _ => panic!("Expected ChatCompletions endpoint"),
        }
    }

    #[test]
    fn test_batch_embeddings_helper() {
        let builder = batch_embeddings("file-input");
        match builder.endpoint() {
            BatchEndpoint::Embeddings => {}
            _ => panic!("Expected Embeddings endpoint"),
        }
    }

    #[test]
    fn test_batch_completions_helper() {
        let builder = batch_completions("file-input");
        match builder.endpoint() {
            BatchEndpoint::Completions => {}
            _ => panic!("Expected Completions endpoint"),
        }
    }

    #[test]
    fn test_batch_job_with_metadata_helper() {
        let mut metadata = HashMap::new();
        metadata.insert("key1".to_string(), "value1".to_string());
        metadata.insert("key2".to_string(), "value2".to_string());

        let builder =
            batch_job_with_metadata("file-input", BatchEndpoint::ChatCompletions, metadata);

        assert!(builder.has_metadata());
        assert_eq!(builder.metadata_ref().len(), 2);
    }

    #[test]
    fn test_list_batch_jobs_helper() {
        let builder = list_batch_jobs();
        assert!(builder.after_ref().is_none());
        assert!(builder.limit_ref().is_none());
    }

    #[test]
    fn test_get_batch_job_helper() {
        let builder = get_batch_job("batch-123");
        assert_eq!(builder.batch_id(), "batch-123");
    }

    #[test]
    fn test_cancel_batch_job_helper() {
        let builder = cancel_batch_job("batch-456");
        assert_eq!(builder.batch_id(), "batch-456");
    }

    #[test]
    fn test_batch_endpoint_display() {
        assert_eq!(
            BatchEndpoint::ChatCompletions.to_string(),
            "/v1/chat/completions"
        );
        assert_eq!(BatchEndpoint::Embeddings.to_string(), "/v1/embeddings");
        assert_eq!(BatchEndpoint::Completions.to_string(), "/v1/completions");
    }

    #[test]
    fn test_batch_completion_window_display() {
        assert_eq!(BatchCompletionWindow::Hours24.to_string(), "24h");
    }

    #[test]
    fn test_batch_job_list_builder_default() {
        let builder = BatchJobListBuilder::default();
        assert!(builder.after_ref().is_none());
        assert!(builder.limit_ref().is_none());
    }
}