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