1use crate::{
2 config::Config,
3 error::OpenAIError,
4 types::audio::{CreateSpeechRequest, CreateSpeechResponse, SpeechResponseStream},
5 Client,
6};
7
8pub struct Speech<'c, C: Config> {
9 client: &'c Client<C>,
10}
11
12impl<'c, C: Config> Speech<'c, C> {
13 pub fn new(client: &'c Client<C>) -> Self {
14 Self { client }
15 }
16
17 pub async fn create(
19 &self,
20 request: CreateSpeechRequest,
21 ) -> Result<CreateSpeechResponse, OpenAIError> {
22 let bytes = self.client.post_raw("/audio/speech", request).await?;
23
24 Ok(CreateSpeechResponse { bytes })
25 }
26
27 #[crate::byot(
29 T0 = serde::Serialize,
30 R = serde::de::DeserializeOwned,
31 stream = "true",
32 where_clause = "R: std::marker::Send + 'static"
33 )]
34 #[allow(unused_mut)]
35 pub async fn create_stream(
36 &self,
37 mut request: CreateSpeechRequest,
38 ) -> Result<SpeechResponseStream, OpenAIError> {
39 #[cfg(not(feature = "byot"))]
40 {
41 use crate::types::audio::StreamFormat;
42 if let Some(stream_format) = request.stream_format {
43 if stream_format != StreamFormat::SSE {
44 return Err(OpenAIError::InvalidArgument(
45 "When stream_format is not SSE, use Audio::speech".into(),
46 ));
47 }
48 }
49
50 request.stream_format = Some(StreamFormat::SSE);
51 }
52 Ok(self.client.post_stream("/audio/speech", request).await)
53 }
54}