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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! Audio API builders.
//!
//! This module provides ergonomic builders for working with the `OpenAI` audio
//! endpoints, covering text-to-speech, transcription, and translation
//! workflows. Builders perform lightweight validation and produce values that
//! can be passed directly to `openai-client-base` request functions.

use std::path::{Path, PathBuf};

use openai_client_base::models::transcription_chunking_strategy::TranscriptionChunkingStrategyTextVariantEnum;
use openai_client_base::models::{
    create_speech_request::{
        ResponseFormat as SpeechResponseFormat, StreamFormat as SpeechStreamFormat,
    },
    AudioResponseFormat, CreateSpeechRequest, TranscriptionChunkingStrategy, TranscriptionInclude,
    VadConfig,
};

use crate::{Builder, Error, Result};

/// Builder for text-to-speech requests.
#[derive(Debug, Clone)]
pub struct SpeechBuilder {
    model: String,
    input: String,
    voice: String,
    instructions: Option<String>,
    response_format: Option<SpeechResponseFormat>,
    speed: Option<f64>,
    stream_format: Option<SpeechStreamFormat>,
}

impl SpeechBuilder {
    /// Create a new speech builder with the required model, input text, and voice.
    #[must_use]
    pub fn new(
        model: impl Into<String>,
        input: impl Into<String>,
        voice: impl Into<String>,
    ) -> Self {
        Self {
            model: model.into(),
            input: input.into(),
            voice: voice.into(),
            instructions: None,
            response_format: None,
            speed: None,
            stream_format: None,
        }
    }

    /// Add additional voice instructions (ignored for legacy TTS models).
    #[must_use]
    pub fn instructions(mut self, instructions: impl Into<String>) -> Self {
        self.instructions = Some(instructions.into());
        self
    }

    /// Choose the audio response format (default is `mp3`).
    #[must_use]
    pub fn response_format(mut self, format: SpeechResponseFormat) -> Self {
        self.response_format = Some(format);
        self
    }

    /// Set the playback speed. Must be between `0.25` and `4.0` inclusive.
    #[must_use]
    pub fn speed(mut self, speed: f64) -> Self {
        self.speed = Some(speed);
        self
    }

    /// Configure streaming output. Unsupported for some legacy models.
    #[must_use]
    pub fn stream_format(mut self, format: SpeechStreamFormat) -> Self {
        self.stream_format = Some(format);
        self
    }

    /// Access the configured model.
    #[must_use]
    pub fn model(&self) -> &str {
        &self.model
    }

    /// Access the configured input text.
    #[must_use]
    pub fn input(&self) -> &str {
        &self.input
    }

    /// Access the configured voice.
    #[must_use]
    pub fn voice(&self) -> &str {
        &self.voice
    }

    /// Access the configured response format, if any.
    #[must_use]
    pub fn response_format_ref(&self) -> Option<SpeechResponseFormat> {
        self.response_format
    }

    /// Access the configured stream format, if any.
    #[must_use]
    pub fn stream_format_ref(&self) -> Option<SpeechStreamFormat> {
        self.stream_format
    }
}

impl Builder<CreateSpeechRequest> for SpeechBuilder {
    fn build(self) -> Result<CreateSpeechRequest> {
        if let Some(speed) = self.speed {
            if !(0.25..=4.0).contains(&speed) {
                return Err(Error::InvalidRequest(format!(
                    "Speech speed {speed} is outside the supported range 0.25–4.0"
                )));
            }
        }

        Ok(CreateSpeechRequest {
            model: self.model,
            input: self.input,
            instructions: self.instructions,
            voice: self.voice,
            response_format: self.response_format,
            speed: self.speed,
            stream_format: self.stream_format,
        })
    }
}

/// Granularity options for transcription timestamps.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum TimestampGranularity {
    /// Include timestamps at the segment level.
    Segment,
    /// Include timestamps at the word level (where supported).
    Word,
}

impl TimestampGranularity {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::Segment => "segment",
            Self::Word => "word",
        }
    }
}

/// Builder for audio transcription requests.
#[derive(Debug, Clone)]
pub struct TranscriptionBuilder {
    file: PathBuf,
    model: String,
    language: Option<String>,
    prompt: Option<String>,
    response_format: Option<AudioResponseFormat>,
    temperature: Option<f64>,
    stream: Option<bool>,
    chunking_strategy: Option<TranscriptionChunkingStrategy>,
    timestamp_granularities: Vec<TimestampGranularity>,
    include: Vec<TranscriptionInclude>,
}

impl TranscriptionBuilder {
    /// Create a new transcription builder for the given audio file and model.
    #[must_use]
    pub fn new(file: impl AsRef<Path>, model: impl Into<String>) -> Self {
        Self {
            file: file.as_ref().to_path_buf(),
            model: model.into(),
            language: None,
            prompt: None,
            response_format: None,
            temperature: None,
            stream: None,
            chunking_strategy: None,
            timestamp_granularities: Vec::new(),
            include: Vec::new(),
        }
    }

    /// Provide the input language to improve accuracy.
    #[must_use]
    pub fn language(mut self, language: impl Into<String>) -> Self {
        self.language = Some(language.into());
        self
    }

    /// Supply a prompt to guide the transcription style.
    #[must_use]
    pub fn prompt(mut self, prompt: impl Into<String>) -> Self {
        self.prompt = Some(prompt.into());
        self
    }

    /// Set the desired response format (`json`, `text`, `srt`, `verbose_json`, `vtt`).
    #[must_use]
    pub fn response_format(mut self, format: AudioResponseFormat) -> Self {
        self.response_format = Some(format);
        self
    }

    /// Control randomness (0.0–1.0). `0.0` yields deterministic output.
    #[must_use]
    pub fn temperature(mut self, temperature: f64) -> Self {
        self.temperature = Some(temperature);
        self
    }

    /// Enable or disable server-side streaming for partial results.
    #[must_use]
    pub fn stream(mut self, stream: bool) -> Self {
        self.stream = Some(stream);
        self
    }

    /// Use the default automatic chunking strategy.
    #[must_use]
    pub fn chunking_strategy_auto(mut self) -> Self {
        self.chunking_strategy = Some(TranscriptionChunkingStrategy::TextVariant(
            TranscriptionChunkingStrategyTextVariantEnum::Auto,
        ));
        self
    }

    /// Provide a custom VAD configuration for chunking.
    #[must_use]
    pub fn chunking_strategy_vad(mut self, config: VadConfig) -> Self {
        self.chunking_strategy = Some(TranscriptionChunkingStrategy::Vadconfig(config));
        self
    }

    /// Disable chunking hints and fall back to API defaults.
    #[must_use]
    pub fn clear_chunking_strategy(mut self) -> Self {
        self.chunking_strategy = None;
        self
    }

    /// Request specific timestamp granularities.
    #[must_use]
    pub fn timestamp_granularities(
        mut self,
        granularities: impl IntoIterator<Item = TimestampGranularity>,
    ) -> Self {
        self.timestamp_granularities = granularities.into_iter().collect();
        self
    }

    /// Append a timestamp granularity option.
    #[must_use]
    pub fn add_timestamp_granularity(mut self, granularity: TimestampGranularity) -> Self {
        if !self.timestamp_granularities.contains(&granularity) {
            self.timestamp_granularities.push(granularity);
        }
        self
    }

    /// Include additional metadata (e.g., log probabilities).
    #[must_use]
    pub fn include(mut self, include: TranscriptionInclude) -> Self {
        if !self.include.contains(&include) {
            self.include.push(include);
        }
        self
    }

    /// Access the source file path.
    #[must_use]
    pub fn file(&self) -> &Path {
        &self.file
    }

    /// Access the target model.
    #[must_use]
    pub fn model(&self) -> &str {
        &self.model
    }

    /// Access the selected language.
    #[must_use]
    pub fn language_ref(&self) -> Option<&str> {
        self.language.as_deref()
    }

    /// Access the selected response format.
    #[must_use]
    pub fn response_format_ref(&self) -> Option<AudioResponseFormat> {
        self.response_format
    }
}

/// Fully prepared transcription request data.
#[derive(Debug, Clone)]
pub struct TranscriptionRequest {
    /// Audio file to upload for transcription.
    pub file: PathBuf,
    /// Model identifier to use (e.g., `gpt-4o-mini-transcribe`).
    pub model: String,
    /// Optional language hint.
    pub language: Option<String>,
    /// Optional style/context prompt.
    pub prompt: Option<String>,
    /// Desired response format.
    pub response_format: Option<AudioResponseFormat>,
    /// Randomness control (0.0–1.0).
    pub temperature: Option<f64>,
    /// Enable partial streaming responses.
    pub stream: Option<bool>,
    /// Chunking strategy configuration.
    pub chunking_strategy: Option<TranscriptionChunkingStrategy>,
    /// Requested timestamp granularities.
    pub timestamp_granularities: Option<Vec<TimestampGranularity>>,
    /// Additional metadata to include in the response.
    pub include: Option<Vec<TranscriptionInclude>>,
}

impl Builder<TranscriptionRequest> for TranscriptionBuilder {
    fn build(self) -> Result<TranscriptionRequest> {
        if let Some(temperature) = self.temperature {
            if !(0.0..=1.0).contains(&temperature) {
                return Err(Error::InvalidRequest(format!(
                    "Transcription temperature {temperature} is outside the supported range 0.0–1.0"
                )));
            }
        }

        Ok(TranscriptionRequest {
            file: self.file,
            model: self.model,
            language: self.language,
            prompt: self.prompt,
            response_format: self.response_format,
            temperature: self.temperature,
            stream: self.stream,
            chunking_strategy: self.chunking_strategy,
            timestamp_granularities: if self.timestamp_granularities.is_empty() {
                None
            } else {
                Some(self.timestamp_granularities)
            },
            include: if self.include.is_empty() {
                None
            } else {
                Some(self.include)
            },
        })
    }
}

/// Builder for audio translation (audio → English text).
#[derive(Debug, Clone)]
pub struct TranslationBuilder {
    file: PathBuf,
    model: String,
    prompt: Option<String>,
    response_format: Option<AudioResponseFormat>,
    temperature: Option<f64>,
}

impl TranslationBuilder {
    /// Create a new translation builder for the given audio file and model.
    #[must_use]
    pub fn new(file: impl AsRef<Path>, model: impl Into<String>) -> Self {
        Self {
            file: file.as_ref().to_path_buf(),
            model: model.into(),
            prompt: None,
            response_format: None,
            temperature: None,
        }
    }

    /// Provide an optional prompt to guide translation tone.
    #[must_use]
    pub fn prompt(mut self, prompt: impl Into<String>) -> Self {
        self.prompt = Some(prompt.into());
        self
    }

    /// Select the output format (defaults to JSON).
    #[must_use]
    pub fn response_format(mut self, format: AudioResponseFormat) -> Self {
        self.response_format = Some(format);
        self
    }

    /// Control randomness (0.0–1.0).
    #[must_use]
    pub fn temperature(mut self, temperature: f64) -> Self {
        self.temperature = Some(temperature);
        self
    }

    /// Access the configured model.
    #[must_use]
    pub fn model(&self) -> &str {
        &self.model
    }

    /// Access the configured file path.
    #[must_use]
    pub fn file(&self) -> &Path {
        &self.file
    }
}

/// Fully prepared translation request data.
#[derive(Debug, Clone)]
pub struct TranslationRequest {
    /// Audio file to translate.
    pub file: PathBuf,
    /// Model to use for translation.
    pub model: String,
    /// Optional prompt for style control.
    pub prompt: Option<String>,
    /// Desired output format.
    pub response_format: Option<AudioResponseFormat>,
    /// Randomness control.
    pub temperature: Option<f64>,
}

impl Builder<TranslationRequest> for TranslationBuilder {
    fn build(self) -> Result<TranslationRequest> {
        if let Some(temperature) = self.temperature {
            if !(0.0..=1.0).contains(&temperature) {
                return Err(Error::InvalidRequest(format!(
                    "Translation temperature {temperature} is outside the supported range 0.0–1.0"
                )));
            }
        }

        Ok(TranslationRequest {
            file: self.file,
            model: self.model,
            prompt: self.prompt,
            response_format: self.response_format,
            temperature: self.temperature,
        })
    }
}

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

    #[test]
    fn builds_speech_request() {
        let request = SpeechBuilder::new("gpt-4o-mini-tts", "Hello world", "alloy")
            .instructions("Speak calmly")
            .response_format(SpeechResponseFormat::Wav)
            .speed(1.25)
            .stream_format(SpeechStreamFormat::Audio)
            .build()
            .expect("valid speech builder");

        assert_eq!(request.model, "gpt-4o-mini-tts");
        assert_eq!(request.input, "Hello world");
        assert_eq!(request.voice, "alloy");
        assert_eq!(request.response_format, Some(SpeechResponseFormat::Wav));
        assert_eq!(request.speed, Some(1.25));
        assert_eq!(request.stream_format, Some(SpeechStreamFormat::Audio));
    }

    #[test]
    fn speech_speed_validation() {
        let err = SpeechBuilder::new("gpt-4o-mini-tts", "Hi", "alloy")
            .speed(5.0)
            .build()
            .expect_err("speed outside supported range");
        assert!(matches!(err, Error::InvalidRequest(_)));
    }

    #[test]
    fn builds_transcription_request() {
        let request = TranscriptionBuilder::new("audio.wav", "gpt-4o-mini-transcribe")
            .language("en")
            .prompt("Friendly tone")
            .response_format(AudioResponseFormat::VerboseJson)
            .temperature(0.2)
            .stream(true)
            .chunking_strategy_auto()
            .timestamp_granularities([TimestampGranularity::Segment, TimestampGranularity::Word])
            .include(TranscriptionInclude::Logprobs)
            .build()
            .expect("valid transcription builder");

        assert_eq!(request.model, "gpt-4o-mini-transcribe");
        assert_eq!(request.language.as_deref(), Some("en"));
        assert!(matches!(
            request.timestamp_granularities,
            Some(grans) if grans.contains(&TimestampGranularity::Word)
        ));
        assert!(matches!(
            request.chunking_strategy,
            Some(TranscriptionChunkingStrategy::TextVariant(_))
        ));
        assert!(matches!(
            request.include,
            Some(values) if values.contains(&TranscriptionInclude::Logprobs)
        ));
    }

    #[test]
    fn transcription_temperature_validation() {
        let err = TranscriptionBuilder::new("audio.wav", "gpt-4o-mini-transcribe")
            .temperature(1.2)
            .build()
            .expect_err("temperature outside range");
        assert!(matches!(err, Error::InvalidRequest(_)));
    }

    #[test]
    fn builds_translation_request() {
        let request = TranslationBuilder::new("clip.mp3", "gpt-4o-mini-transcribe")
            .prompt("Keep humour")
            .response_format(AudioResponseFormat::Text)
            .temperature(0.3)
            .build()
            .expect("valid translation builder");

        assert_eq!(request.model, "gpt-4o-mini-transcribe");
        assert_eq!(request.response_format, Some(AudioResponseFormat::Text));
    }

    #[test]
    fn translation_temperature_validation() {
        let err = TranslationBuilder::new("clip.mp3", "gpt-4o-mini-transcribe")
            .temperature(1.5)
            .build()
            .expect_err("temperature outside range");
        assert!(matches!(err, Error::InvalidRequest(_)));
    }
}