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
/*
* OpenAI API
*
* The OpenAI REST API. Please see https://platform.openai.com/docs/api-reference for more details.
*
* The version of the OpenAPI document: 2.3.0
*
* Generated by: https://openapi-generator.tech
*/
use crate::models;
use serde::{Deserialize, Serialize};
#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize, bon::Builder)]
pub struct CreateSpeechRequest {
/// One of the available [TTS models](https://platform.openai.com/docs/models#tts): `tts-1`, `tts-1-hd` or `gpt-4o-mini-tts`.
#[serde(rename = "model")]
pub model: String,
/// The text to generate audio for. The maximum length is 4096 characters.
#[serde(rename = "input")]
pub input: String,
/// Control the voice of your generated audio with additional instructions. Does not work with `tts-1` or `tts-1-hd`.
#[serde(rename = "instructions", skip_serializing_if = "Option::is_none")]
pub instructions: Option<String>,
/// The voice to use when generating the audio. Supported voices are `alloy`, `ash`, `ballad`, `coral`, `echo`, `fable`, `onyx`, `nova`, `sage`, `shimmer`, and `verse`. Previews of the voices are available in the [Text to speech guide](https://platform.openai.com/docs/guides/text-to-speech#voice-options).
#[serde(rename = "voice")]
pub voice: String,
/// The format to audio in. Supported formats are `mp3`, `opus`, `aac`, `flac`, `wav`, and `pcm`.
#[serde(rename = "response_format", skip_serializing_if = "Option::is_none")]
pub response_format: Option<ResponseFormat>,
/// The speed of the generated audio. Select a value from `0.25` to `4.0`. `1.0` is the default.
#[serde(rename = "speed", skip_serializing_if = "Option::is_none")]
pub speed: Option<f64>,
/// The format to stream the audio in. Supported formats are `sse` and `audio`. `sse` is not supported for `tts-1` or `tts-1-hd`.
#[serde(rename = "stream_format", skip_serializing_if = "Option::is_none")]
pub stream_format: Option<StreamFormat>,
}
impl CreateSpeechRequest {
pub fn new(model: String, input: String, voice: String) -> CreateSpeechRequest {
CreateSpeechRequest {
model,
input,
instructions: None,
voice,
response_format: None,
speed: None,
stream_format: None,
}
}
}
/// The format to audio in. Supported formats are `mp3`, `opus`, `aac`, `flac`, `wav`, and `pcm`.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum ResponseFormat {
#[serde(rename = "mp3")]
Mp3,
#[serde(rename = "opus")]
Opus,
#[serde(rename = "aac")]
Aac,
#[serde(rename = "flac")]
Flac,
#[serde(rename = "wav")]
Wav,
#[serde(rename = "pcm")]
Pcm,
}
impl Default for ResponseFormat {
fn default() -> ResponseFormat {
Self::Mp3
}
}
/// The format to stream the audio in. Supported formats are `sse` and `audio`. `sse` is not supported for `tts-1` or `tts-1-hd`.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum StreamFormat {
#[serde(rename = "sse")]
Sse,
#[serde(rename = "audio")]
Audio,
}
impl Default for StreamFormat {
fn default() -> StreamFormat {
Self::Sse
}
}
impl std::fmt::Display for CreateSpeechRequest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match serde_json::to_string(self) {
Ok(s) => write!(f, "{}", s),
Err(_) => Err(std::fmt::Error),
}
}
}