openai-tools 1.1.0

Tools 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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
//! OpenAI Audio API Request Module
//!
//! This module provides the functionality to interact with the OpenAI Audio API.
//! It supports text-to-speech (TTS), transcription, and translation.
//!
//! # Key Features
//!
//! - **Text-to-Speech**: Convert text to natural-sounding audio
//! - **Transcription**: Convert audio to text (speech-to-text)
//! - **Translation**: Translate audio to English text
//!
//! # Quick Start
//!
//! ```rust,no_run
//! use openai_tools::audio::request::{Audio, TtsOptions, Voice};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let audio = Audio::new()?;
//!
//!     // Generate speech from text
//!     let options = TtsOptions::default();
//!     let audio_bytes = audio.text_to_speech("Hello, world!", options).await?;
//!     std::fs::write("output.mp3", audio_bytes)?;
//!
//!     Ok(())
//! }
//! ```

use crate::audio::response::TranscriptionResponse;
use crate::common::auth::AuthProvider;
use crate::common::client::create_http_client;
use crate::common::errors::{ErrorResponse, OpenAIToolError, Result};
use request::multipart::{Form, Part};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::time::Duration;

/// Default API path for Audio
const AUDIO_PATH: &str = "audio";

/// Text-to-speech models.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum TtsModel {
    /// Standard quality TTS model
    #[serde(rename = "tts-1")]
    #[default]
    Tts1,
    /// High definition TTS model
    #[serde(rename = "tts-1-hd")]
    Tts1Hd,
    /// GPT-4o Mini TTS model
    #[serde(rename = "gpt-4o-mini-tts")]
    Gpt4oMiniTts,
}

impl TtsModel {
    /// Returns the model identifier string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Tts1 => "tts-1",
            Self::Tts1Hd => "tts-1-hd",
            Self::Gpt4oMiniTts => "gpt-4o-mini-tts",
        }
    }

    /// Checks if this model supports the `instructions` parameter.
    ///
    /// Only `gpt-4o-mini-tts` supports the instructions parameter for
    /// controlling voice characteristics like tone, emotion, and pacing.
    ///
    /// # Example
    ///
    /// ```rust
    /// use openai_tools::audio::request::TtsModel;
    ///
    /// assert!(TtsModel::Gpt4oMiniTts.supports_instructions());
    /// assert!(!TtsModel::Tts1.supports_instructions());
    /// assert!(!TtsModel::Tts1Hd.supports_instructions());
    /// ```
    pub fn supports_instructions(&self) -> bool {
        matches!(self, Self::Gpt4oMiniTts)
    }
}

impl std::fmt::Display for TtsModel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Voice options for text-to-speech.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum Voice {
    /// Alloy voice
    #[default]
    Alloy,
    /// Ash voice
    Ash,
    /// Ballad voice
    Ballad,
    /// Cedar voice (recommended for quality)
    Cedar,
    /// Coral voice
    Coral,
    /// Echo voice
    Echo,
    /// Fable voice
    Fable,
    /// Marin voice (recommended for quality)
    Marin,
    /// Nova voice
    Nova,
    /// Onyx voice
    Onyx,
    /// Sage voice
    Sage,
    /// Shimmer voice
    Shimmer,
    /// Verse voice
    Verse,
}

impl Voice {
    /// Returns the voice identifier string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Alloy => "alloy",
            Self::Ash => "ash",
            Self::Ballad => "ballad",
            Self::Cedar => "cedar",
            Self::Coral => "coral",
            Self::Echo => "echo",
            Self::Fable => "fable",
            Self::Marin => "marin",
            Self::Nova => "nova",
            Self::Onyx => "onyx",
            Self::Sage => "sage",
            Self::Shimmer => "shimmer",
            Self::Verse => "verse",
        }
    }
}

impl std::fmt::Display for Voice {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Audio output formats for TTS.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum AudioFormat {
    /// MP3 format (default)
    #[default]
    Mp3,
    /// Opus format
    Opus,
    /// AAC format
    Aac,
    /// FLAC format
    Flac,
    /// WAV format
    Wav,
    /// PCM format
    Pcm,
}

impl AudioFormat {
    /// Returns the format string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Mp3 => "mp3",
            Self::Opus => "opus",
            Self::Aac => "aac",
            Self::Flac => "flac",
            Self::Wav => "wav",
            Self::Pcm => "pcm",
        }
    }

    /// Returns the file extension for this format.
    pub fn file_extension(&self) -> &'static str {
        self.as_str()
    }
}

impl std::fmt::Display for AudioFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Speech-to-text models for transcription and translation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum SttModel {
    /// Whisper v1 model
    #[serde(rename = "whisper-1")]
    #[default]
    Whisper1,
    /// GPT-4o Transcribe model
    #[serde(rename = "gpt-4o-transcribe")]
    Gpt4oTranscribe,
}

impl SttModel {
    /// Returns the model identifier string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Whisper1 => "whisper-1",
            Self::Gpt4oTranscribe => "gpt-4o-transcribe",
        }
    }
}

impl std::fmt::Display for SttModel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Transcription response formats.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum TranscriptionFormat {
    /// JSON format
    #[default]
    Json,
    /// Plain text format
    Text,
    /// SRT subtitle format
    Srt,
    /// Verbose JSON with timestamps
    VerboseJson,
    /// VTT subtitle format
    Vtt,
}

impl TranscriptionFormat {
    /// Returns the format string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Json => "json",
            Self::Text => "text",
            Self::Srt => "srt",
            Self::VerboseJson => "verbose_json",
            Self::Vtt => "vtt",
        }
    }
}

/// Timestamp granularity options.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TimestampGranularity {
    /// Word-level timestamps
    Word,
    /// Segment-level timestamps
    Segment,
}

impl TimestampGranularity {
    /// Returns the granularity string.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Word => "word",
            Self::Segment => "segment",
        }
    }
}

/// Options for text-to-speech generation.
#[derive(Debug, Clone, Default)]
pub struct TtsOptions {
    /// The model to use (defaults to tts-1)
    pub model: TtsModel,
    /// The voice to use (defaults to alloy)
    pub voice: Voice,
    /// The output audio format (defaults to mp3)
    pub response_format: AudioFormat,
    /// Speech speed (0.25 to 4.0, defaults to 1.0)
    pub speed: Option<f32>,
    /// Instructions for controlling voice characteristics.
    ///
    /// Only supported by `gpt-4o-mini-tts` model.
    /// Use natural language to control tone, emotion, and pacing.
    ///
    /// # Examples
    ///
    /// - `"Speak in a cheerful and positive tone."`
    /// - `"Use a calm and soothing voice."`
    /// - `"Speak with enthusiasm and energy."`
    ///
    /// If set with an unsupported model (`tts-1` or `tts-1-hd`),
    /// this parameter will be ignored and a warning will be logged.
    pub instructions: Option<String>,
}

/// Options for audio transcription.
#[derive(Debug, Clone, Default)]
pub struct TranscribeOptions {
    /// The model to use (defaults to whisper-1)
    pub model: Option<SttModel>,
    /// The language of the input audio (ISO-639-1 code)
    pub language: Option<String>,
    /// Optional prompt to guide the model's style
    pub prompt: Option<String>,
    /// Response format (defaults to json)
    pub response_format: Option<TranscriptionFormat>,
    /// Temperature for sampling (0.0 to 1.0)
    pub temperature: Option<f32>,
    /// Timestamp granularities to include
    pub timestamp_granularities: Option<Vec<TimestampGranularity>>,
}

/// Options for audio translation.
#[derive(Debug, Clone, Default)]
pub struct TranslateOptions {
    /// The model to use (only whisper-1 is supported)
    pub model: Option<SttModel>,
    /// Optional prompt to guide the model's style
    pub prompt: Option<String>,
    /// Response format (defaults to json)
    pub response_format: Option<TranscriptionFormat>,
    /// Temperature for sampling (0.0 to 1.0)
    pub temperature: Option<f32>,
}

/// Request payload for TTS.
#[derive(Debug, Clone, Serialize)]
struct TtsRequest {
    model: String,
    input: String,
    voice: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    response_format: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    speed: Option<f32>,
    /// Instructions for voice control (only for gpt-4o-mini-tts).
    #[serde(skip_serializing_if = "Option::is_none")]
    instructions: Option<String>,
}

/// Client for interacting with the OpenAI Audio API.
///
/// This struct provides methods for text-to-speech, transcription, and translation.
/// Use [`Audio::new()`] to create a new instance.
///
/// # Example
///
/// ```rust,no_run
/// use openai_tools::audio::request::{Audio, TtsOptions, Voice, AudioFormat};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let audio = Audio::new()?;
///
///     let options = TtsOptions {
///         voice: Voice::Nova,
///         response_format: AudioFormat::Mp3,
///         ..Default::default()
///     };
///
///     let bytes = audio.text_to_speech("Welcome to our app!", options).await?;
///     std::fs::write("welcome.mp3", bytes)?;
///
///     Ok(())
/// }
/// ```
pub struct Audio {
    /// Authentication provider (OpenAI or Azure)
    auth: AuthProvider,
    /// Optional request timeout duration
    timeout: Option<Duration>,
}

impl Audio {
    /// Creates a new Audio client for OpenAI API.
    ///
    /// Initializes the client by loading the OpenAI API key from
    /// the environment variable `OPENAI_API_KEY`. Supports `.env` file loading
    /// via dotenvy.
    ///
    /// # Returns
    ///
    /// * `Ok(Audio)` - A new Audio client ready for use
    /// * `Err(OpenAIToolError)` - If the API key is not found in the environment
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openai_tools::audio::request::Audio;
    ///
    /// let audio = Audio::new().expect("API key should be set");
    /// ```
    pub fn new() -> Result<Self> {
        let auth = AuthProvider::openai_from_env()?;
        Ok(Self { auth, timeout: None })
    }

    /// Creates a new Audio client with a custom authentication provider
    pub fn with_auth(auth: AuthProvider) -> Self {
        Self { auth, timeout: None }
    }

    /// Creates a new Audio client for Azure OpenAI API
    pub fn azure() -> Result<Self> {
        let auth = AuthProvider::azure_from_env()?;
        Ok(Self { auth, timeout: None })
    }

    /// Creates a new Audio client by auto-detecting the provider
    pub fn detect_provider() -> Result<Self> {
        let auth = AuthProvider::from_env()?;
        Ok(Self { auth, timeout: None })
    }

    /// Creates a new Audio client with URL-based provider detection
    pub fn with_url<S: Into<String>>(base_url: S, api_key: S) -> Self {
        let auth = AuthProvider::from_url_with_key(base_url, api_key);
        Self { auth, timeout: None }
    }

    /// Creates a new Audio client from URL using environment variables
    pub fn from_url<S: Into<String>>(url: S) -> Result<Self> {
        let auth = AuthProvider::from_url(url)?;
        Ok(Self { auth, timeout: None })
    }

    /// Returns the authentication provider
    pub fn auth(&self) -> &AuthProvider {
        &self.auth
    }

    /// Sets the request timeout duration.
    ///
    /// # Arguments
    ///
    /// * `timeout` - The maximum time to wait for a response
    ///
    /// # Returns
    ///
    /// A mutable reference to self for method chaining
    pub fn timeout(&mut self, timeout: Duration) -> &mut Self {
        self.timeout = Some(timeout);
        self
    }

    /// Creates the HTTP client with default headers.
    fn create_client(&self) -> Result<(request::Client, request::header::HeaderMap)> {
        let client = create_http_client(self.timeout)?;
        let mut headers = request::header::HeaderMap::new();
        self.auth.apply_headers(&mut headers)?;
        headers.insert("User-Agent", request::header::HeaderValue::from_static("openai-tools-rust"));
        Ok((client, headers))
    }

    /// Converts text to speech.
    ///
    /// Returns audio bytes in the specified format.
    ///
    /// # Arguments
    ///
    /// * `text` - The text to convert to speech (max 4096 characters)
    /// * `options` - TTS options (model, voice, format, speed)
    ///
    /// # Returns
    ///
    /// * `Ok(Vec<u8>)` - The audio data as bytes
    /// * `Err(OpenAIToolError)` - If the request fails
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openai_tools::audio::request::{Audio, TtsOptions, TtsModel, Voice};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let audio = Audio::new()?;
    ///
    ///     let options = TtsOptions {
    ///         model: TtsModel::Tts1Hd,
    ///         voice: Voice::Shimmer,
    ///         speed: Some(1.2),
    ///         ..Default::default()
    ///     };
    ///
    ///     let bytes = audio.text_to_speech("Hello, this is a test.", options).await?;
    ///     std::fs::write("speech.mp3", bytes)?;
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn text_to_speech(&self, text: &str, options: TtsOptions) -> Result<Vec<u8>> {
        let (client, mut headers) = self.create_client()?;
        headers.insert("Content-Type", request::header::HeaderValue::from_static("application/json"));

        // Check if instructions parameter is supported by the model
        let instructions = if options.instructions.is_some() {
            if options.model.supports_instructions() {
                options.instructions
            } else {
                tracing::warn!("Model '{}' does not support instructions parameter. Ignoring instructions.", options.model);
                None
            }
        } else {
            None
        };

        let request_body = TtsRequest {
            model: options.model.as_str().to_string(),
            input: text.to_string(),
            voice: options.voice.as_str().to_string(),
            response_format: Some(options.response_format.as_str().to_string()),
            speed: options.speed,
            instructions,
        };

        let body = serde_json::to_string(&request_body).map_err(OpenAIToolError::SerdeJsonError)?;

        let url = format!("{}/speech", self.auth.endpoint(AUDIO_PATH));

        let response = client.post(&url).headers(headers).body(body).send().await.map_err(OpenAIToolError::RequestError)?;

        let bytes = response.bytes().await.map_err(OpenAIToolError::RequestError)?;

        Ok(bytes.to_vec())
    }

    /// Transcribes audio from a file path.
    ///
    /// # Arguments
    ///
    /// * `audio_path` - Path to the audio file
    /// * `options` - Transcription options
    ///
    /// # Returns
    ///
    /// * `Ok(TranscriptionResponse)` - The transcription result
    /// * `Err(OpenAIToolError)` - If the request fails
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openai_tools::audio::request::{Audio, TranscribeOptions};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let audio = Audio::new()?;
    ///
    ///     let options = TranscribeOptions {
    ///         language: Some("en".to_string()),
    ///         ..Default::default()
    ///     };
    ///
    ///     let response = audio.transcribe("audio.mp3", options).await?;
    ///     println!("Transcription: {}", response.text);
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn transcribe(&self, audio_path: &str, options: TranscribeOptions) -> Result<TranscriptionResponse> {
        let audio_content = tokio::fs::read(audio_path).await.map_err(|e| OpenAIToolError::Error(format!("Failed to read audio file: {}", e)))?;

        let filename = Path::new(audio_path).file_name().and_then(|n| n.to_str()).unwrap_or("audio.mp3").to_string();

        self.transcribe_bytes(&audio_content, &filename, options).await
    }

    /// Transcribes audio from bytes.
    ///
    /// # Arguments
    ///
    /// * `audio_data` - The audio data as bytes
    /// * `filename` - The filename with extension (e.g., "audio.mp3")
    /// * `options` - Transcription options
    ///
    /// # Returns
    ///
    /// * `Ok(TranscriptionResponse)` - The transcription result
    /// * `Err(OpenAIToolError)` - If the request fails
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openai_tools::audio::request::{Audio, TranscribeOptions, SttModel};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let audio = Audio::new()?;
    ///
    ///     let audio_data = std::fs::read("recording.mp3")?;
    ///     let options = TranscribeOptions {
    ///         model: Some(SttModel::Whisper1),
    ///         ..Default::default()
    ///     };
    ///
    ///     let response = audio.transcribe_bytes(&audio_data, "recording.mp3", options).await?;
    ///     println!("Transcription: {}", response.text);
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn transcribe_bytes(&self, audio_data: &[u8], filename: &str, options: TranscribeOptions) -> Result<TranscriptionResponse> {
        let (client, headers) = self.create_client()?;

        let audio_part = Part::bytes(audio_data.to_vec())
            .file_name(filename.to_string())
            .mime_str("audio/mpeg")
            .map_err(|e| OpenAIToolError::Error(format!("Failed to set MIME type: {}", e)))?;

        let mut form = Form::new().part("file", audio_part);

        // Add model
        let model = options.model.unwrap_or_default();
        form = form.text("model", model.as_str().to_string());

        // Add optional parameters
        if let Some(language) = options.language {
            form = form.text("language", language);
        }
        if let Some(prompt) = options.prompt {
            form = form.text("prompt", prompt);
        }
        if let Some(response_format) = options.response_format {
            form = form.text("response_format", response_format.as_str().to_string());
        }
        if let Some(temperature) = options.temperature {
            form = form.text("temperature", temperature.to_string());
        }
        if let Some(granularities) = options.timestamp_granularities {
            for g in granularities {
                form = form.text("timestamp_granularities[]", g.as_str().to_string());
            }
        }

        let url = format!("{}/transcriptions", self.auth.endpoint(AUDIO_PATH));

        let response = client.post(&url).headers(headers).multipart(form).send().await.map_err(OpenAIToolError::RequestError)?;

        let status = response.status();
        let content = response.text().await.map_err(OpenAIToolError::RequestError)?;

        if cfg!(test) {
            tracing::info!("Response content: {}", content);
        }

        if !status.is_success() {
            if let Ok(error_resp) = serde_json::from_str::<ErrorResponse>(&content) {
                return Err(OpenAIToolError::Error(error_resp.error.message.unwrap_or_default()));
            }
            return Err(OpenAIToolError::Error(format!("API error ({}): {}", status, content)));
        }

        serde_json::from_str::<TranscriptionResponse>(&content).map_err(OpenAIToolError::SerdeJsonError)
    }

    /// Translates audio to English text.
    ///
    /// Only supports translation to English using the whisper-1 model.
    ///
    /// # Arguments
    ///
    /// * `audio_path` - Path to the audio file
    /// * `options` - Translation options
    ///
    /// # Returns
    ///
    /// * `Ok(TranscriptionResponse)` - The translation result
    /// * `Err(OpenAIToolError)` - If the request fails
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use openai_tools::audio::request::{Audio, TranslateOptions};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let audio = Audio::new()?;
    ///
    ///     let options = TranslateOptions::default();
    ///     let response = audio.translate("french_audio.mp3", options).await?;
    ///     println!("English translation: {}", response.text);
    ///
    ///     Ok(())
    /// }
    /// ```
    pub async fn translate(&self, audio_path: &str, options: TranslateOptions) -> Result<TranscriptionResponse> {
        let audio_content = tokio::fs::read(audio_path).await.map_err(|e| OpenAIToolError::Error(format!("Failed to read audio file: {}", e)))?;

        let filename = Path::new(audio_path).file_name().and_then(|n| n.to_str()).unwrap_or("audio.mp3").to_string();

        self.translate_bytes(&audio_content, &filename, options).await
    }

    /// Translates audio from bytes to English text.
    ///
    /// # Arguments
    ///
    /// * `audio_data` - The audio data as bytes
    /// * `filename` - The filename with extension (e.g., "audio.mp3")
    /// * `options` - Translation options
    ///
    /// # Returns
    ///
    /// * `Ok(TranscriptionResponse)` - The translation result
    /// * `Err(OpenAIToolError)` - If the request fails
    pub async fn translate_bytes(&self, audio_data: &[u8], filename: &str, options: TranslateOptions) -> Result<TranscriptionResponse> {
        let (client, headers) = self.create_client()?;

        let audio_part = Part::bytes(audio_data.to_vec())
            .file_name(filename.to_string())
            .mime_str("audio/mpeg")
            .map_err(|e| OpenAIToolError::Error(format!("Failed to set MIME type: {}", e)))?;

        let mut form = Form::new().part("file", audio_part);

        // Add model (whisper-1 is the only supported model for translation)
        let model = options.model.unwrap_or(SttModel::Whisper1);
        form = form.text("model", model.as_str().to_string());

        // Add optional parameters
        if let Some(prompt) = options.prompt {
            form = form.text("prompt", prompt);
        }
        if let Some(response_format) = options.response_format {
            form = form.text("response_format", response_format.as_str().to_string());
        }
        if let Some(temperature) = options.temperature {
            form = form.text("temperature", temperature.to_string());
        }

        let url = format!("{}/translations", self.auth.endpoint(AUDIO_PATH));

        let response = client.post(&url).headers(headers).multipart(form).send().await.map_err(OpenAIToolError::RequestError)?;

        let status = response.status();
        let content = response.text().await.map_err(OpenAIToolError::RequestError)?;

        if cfg!(test) {
            tracing::info!("Response content: {}", content);
        }

        if !status.is_success() {
            if let Ok(error_resp) = serde_json::from_str::<ErrorResponse>(&content) {
                return Err(OpenAIToolError::Error(error_resp.error.message.unwrap_or_default()));
            }
            return Err(OpenAIToolError::Error(format!("API error ({}): {}", status, content)));
        }

        serde_json::from_str::<TranscriptionResponse>(&content).map_err(OpenAIToolError::SerdeJsonError)
    }
}

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

    // =========================================================================
    // TtsModel Tests
    // =========================================================================

    #[test]
    fn test_tts_model_as_str() {
        assert_eq!(TtsModel::Tts1.as_str(), "tts-1");
        assert_eq!(TtsModel::Tts1Hd.as_str(), "tts-1-hd");
        assert_eq!(TtsModel::Gpt4oMiniTts.as_str(), "gpt-4o-mini-tts");
    }

    #[test]
    fn test_tts_model_supports_instructions() {
        // Only gpt-4o-mini-tts supports instructions
        assert!(TtsModel::Gpt4oMiniTts.supports_instructions());
        assert!(!TtsModel::Tts1.supports_instructions());
        assert!(!TtsModel::Tts1Hd.supports_instructions());
    }

    #[test]
    fn test_tts_model_default() {
        let model = TtsModel::default();
        assert_eq!(model, TtsModel::Tts1);
    }

    #[test]
    fn test_tts_model_display() {
        assert_eq!(format!("{}", TtsModel::Gpt4oMiniTts), "gpt-4o-mini-tts");
    }

    // =========================================================================
    // Voice Tests
    // =========================================================================

    #[test]
    fn test_voice_as_str_all_voices() {
        assert_eq!(Voice::Alloy.as_str(), "alloy");
        assert_eq!(Voice::Ash.as_str(), "ash");
        assert_eq!(Voice::Ballad.as_str(), "ballad");
        assert_eq!(Voice::Cedar.as_str(), "cedar");
        assert_eq!(Voice::Coral.as_str(), "coral");
        assert_eq!(Voice::Echo.as_str(), "echo");
        assert_eq!(Voice::Fable.as_str(), "fable");
        assert_eq!(Voice::Marin.as_str(), "marin");
        assert_eq!(Voice::Nova.as_str(), "nova");
        assert_eq!(Voice::Onyx.as_str(), "onyx");
        assert_eq!(Voice::Sage.as_str(), "sage");
        assert_eq!(Voice::Shimmer.as_str(), "shimmer");
        assert_eq!(Voice::Verse.as_str(), "verse");
    }

    #[test]
    fn test_voice_new_voices() {
        // Test the newly added voices
        assert_eq!(Voice::Ballad.as_str(), "ballad");
        assert_eq!(Voice::Cedar.as_str(), "cedar");
        assert_eq!(Voice::Marin.as_str(), "marin");
        assert_eq!(Voice::Verse.as_str(), "verse");
    }

    #[test]
    fn test_voice_default() {
        let voice = Voice::default();
        assert_eq!(voice, Voice::Alloy);
    }

    #[test]
    fn test_voice_serialization() {
        let voice = Voice::Coral;
        let json = serde_json::to_string(&voice).unwrap();
        assert_eq!(json, "\"coral\"");

        // Test new voices
        let ballad = Voice::Ballad;
        let json = serde_json::to_string(&ballad).unwrap();
        assert_eq!(json, "\"ballad\"");
    }

    #[test]
    fn test_voice_deserialization() {
        let voice: Voice = serde_json::from_str("\"coral\"").unwrap();
        assert_eq!(voice, Voice::Coral);

        // Test new voices
        let cedar: Voice = serde_json::from_str("\"cedar\"").unwrap();
        assert_eq!(cedar, Voice::Cedar);

        let marin: Voice = serde_json::from_str("\"marin\"").unwrap();
        assert_eq!(marin, Voice::Marin);
    }

    // =========================================================================
    // TtsOptions Tests
    // =========================================================================

    #[test]
    fn test_tts_options_default() {
        let options = TtsOptions::default();
        assert_eq!(options.model, TtsModel::Tts1);
        assert_eq!(options.voice, Voice::Alloy);
        assert_eq!(options.response_format, AudioFormat::Mp3);
        assert!(options.speed.is_none());
        assert!(options.instructions.is_none());
    }

    #[test]
    fn test_tts_options_with_instructions() {
        let options = TtsOptions {
            model: TtsModel::Gpt4oMiniTts,
            voice: Voice::Coral,
            instructions: Some("Speak in a cheerful tone.".to_string()),
            ..Default::default()
        };
        assert_eq!(options.model, TtsModel::Gpt4oMiniTts);
        assert_eq!(options.instructions, Some("Speak in a cheerful tone.".to_string()));
    }

    // =========================================================================
    // TtsRequest Tests
    // =========================================================================

    #[test]
    fn test_tts_request_serialization_with_instructions() {
        let request = TtsRequest {
            model: "gpt-4o-mini-tts".to_string(),
            input: "Hello, world!".to_string(),
            voice: "coral".to_string(),
            response_format: Some("mp3".to_string()),
            speed: None,
            instructions: Some("Speak cheerfully.".to_string()),
        };
        let json = serde_json::to_value(&request).unwrap();

        assert_eq!(json["model"], "gpt-4o-mini-tts");
        assert_eq!(json["input"], "Hello, world!");
        assert_eq!(json["voice"], "coral");
        assert_eq!(json["response_format"], "mp3");
        assert_eq!(json["instructions"], "Speak cheerfully.");
        assert!(json.get("speed").is_none());
    }

    #[test]
    fn test_tts_request_serialization_without_instructions() {
        let request = TtsRequest {
            model: "tts-1".to_string(),
            input: "Hello".to_string(),
            voice: "alloy".to_string(),
            response_format: Some("mp3".to_string()),
            speed: Some(1.0),
            instructions: None,
        };
        let json = serde_json::to_value(&request).unwrap();

        assert_eq!(json["model"], "tts-1");
        assert_eq!(json["speed"], 1.0);
        // instructions should be omitted when None
        assert!(json.get("instructions").is_none());
    }

    #[test]
    fn test_tts_request_skip_serializing_none_fields() {
        let request = TtsRequest {
            model: "tts-1".to_string(),
            input: "Test".to_string(),
            voice: "echo".to_string(),
            response_format: None,
            speed: None,
            instructions: None,
        };
        let json = serde_json::to_value(&request).unwrap();

        // Required fields are present
        assert!(json.get("model").is_some());
        assert!(json.get("input").is_some());
        assert!(json.get("voice").is_some());

        // Optional fields with None are omitted
        assert!(json.get("response_format").is_none());
        assert!(json.get("speed").is_none());
        assert!(json.get("instructions").is_none());
    }

    // =========================================================================
    // AudioFormat Tests
    // =========================================================================

    #[test]
    fn test_audio_format_as_str() {
        assert_eq!(AudioFormat::Mp3.as_str(), "mp3");
        assert_eq!(AudioFormat::Opus.as_str(), "opus");
        assert_eq!(AudioFormat::Aac.as_str(), "aac");
        assert_eq!(AudioFormat::Flac.as_str(), "flac");
        assert_eq!(AudioFormat::Wav.as_str(), "wav");
        assert_eq!(AudioFormat::Pcm.as_str(), "pcm");
    }

    #[test]
    fn test_audio_format_file_extension() {
        assert_eq!(AudioFormat::Mp3.file_extension(), "mp3");
        assert_eq!(AudioFormat::Wav.file_extension(), "wav");
    }

    // =========================================================================
    // SttModel Tests
    // =========================================================================

    #[test]
    fn test_stt_model_as_str() {
        assert_eq!(SttModel::Whisper1.as_str(), "whisper-1");
        assert_eq!(SttModel::Gpt4oTranscribe.as_str(), "gpt-4o-transcribe");
    }

    // =========================================================================
    // TranscriptionFormat Tests
    // =========================================================================

    #[test]
    fn test_transcription_format_as_str() {
        assert_eq!(TranscriptionFormat::Json.as_str(), "json");
        assert_eq!(TranscriptionFormat::Text.as_str(), "text");
        assert_eq!(TranscriptionFormat::Srt.as_str(), "srt");
        assert_eq!(TranscriptionFormat::VerboseJson.as_str(), "verbose_json");
        assert_eq!(TranscriptionFormat::Vtt.as_str(), "vtt");
    }

    // =========================================================================
    // TimestampGranularity Tests
    // =========================================================================

    #[test]
    fn test_timestamp_granularity_as_str() {
        assert_eq!(TimestampGranularity::Word.as_str(), "word");
        assert_eq!(TimestampGranularity::Segment.as_str(), "segment");
    }
}