async_openai/audio/
transcriptions.rs

1use bytes::Bytes;
2
3use crate::{
4    config::Config,
5    error::OpenAIError,
6    types::audio::{
7        CreateTranscriptionRequest, CreateTranscriptionResponseDiarizedJson,
8        CreateTranscriptionResponseJson, CreateTranscriptionResponseVerboseJson,
9    },
10    Client, RequestOptions,
11};
12
13#[cfg(not(target_family = "wasm"))]
14use crate::types::audio::TranscriptionResponseStream;
15
16pub struct Transcriptions<'c, C: Config> {
17    client: &'c Client<C>,
18    pub(crate) request_options: RequestOptions,
19}
20
21impl<'c, C: Config> Transcriptions<'c, C> {
22    pub fn new(client: &'c Client<C>) -> Self {
23        Self {
24            client,
25            request_options: RequestOptions::new(),
26        }
27    }
28
29    /// Transcribes audio into the input language.
30    #[crate::byot(
31        T0 = Clone,
32        R = serde::de::DeserializeOwned,
33        where_clause =  "reqwest::multipart::Form: crate::traits::AsyncTryFrom<T0, Error = OpenAIError>",
34    )]
35    pub async fn create(
36        &self,
37        request: CreateTranscriptionRequest,
38    ) -> Result<CreateTranscriptionResponseJson, OpenAIError> {
39        self.client
40            .post_form("/audio/transcriptions", request, &self.request_options)
41            .await
42    }
43
44    #[cfg(not(target_family = "wasm"))]
45    #[crate::byot(
46        T0 = Clone,
47        R = serde::de::DeserializeOwned,
48        stream = "true",
49        where_clause = "R: std::marker::Send + 'static, reqwest::multipart::Form: crate::traits::AsyncTryFrom<T0, Error = OpenAIError>"
50    )]
51    #[allow(unused_mut)]
52    pub async fn create_stream(
53        &self,
54        mut request: CreateTranscriptionRequest,
55    ) -> Result<TranscriptionResponseStream, OpenAIError> {
56        #[cfg(not(feature = "byot"))]
57        {
58            if let Some(stream) = request.stream {
59                if !stream {
60                    return Err(OpenAIError::InvalidArgument(
61                        "When stream is not true, use Audio::transcribe".into(),
62                    ));
63                }
64            }
65            request.stream = Some(true);
66        }
67
68        self.client
69            .post_form_stream("/audio/transcriptions", request, &self.request_options)
70            .await
71    }
72
73    /// Transcribes audio into the input language.
74    #[crate::byot(
75        T0 = Clone,
76        R = serde::de::DeserializeOwned,
77        where_clause =  "reqwest::multipart::Form: crate::traits::AsyncTryFrom<T0, Error = OpenAIError>",
78    )]
79    pub async fn create_verbose_json(
80        &self,
81        request: CreateTranscriptionRequest,
82    ) -> Result<CreateTranscriptionResponseVerboseJson, OpenAIError> {
83        self.client
84            .post_form("/audio/transcriptions", request, &self.request_options)
85            .await
86    }
87
88    /// Transcribes audio into the input language.
89    #[crate::byot(
90        T0 = Clone,
91        R = serde::de::DeserializeOwned,
92        where_clause =  "reqwest::multipart::Form: crate::traits::AsyncTryFrom<T0, Error = OpenAIError>",
93    )]
94    pub async fn create_diarized_json(
95        &self,
96        request: CreateTranscriptionRequest,
97    ) -> Result<CreateTranscriptionResponseDiarizedJson, OpenAIError> {
98        self.client
99            .post_form("/audio/transcriptions", request, &self.request_options)
100            .await
101    }
102
103    /// Transcribes audio into the input language.
104    pub async fn create_raw(
105        &self,
106        request: CreateTranscriptionRequest,
107    ) -> Result<Bytes, OpenAIError> {
108        let (bytes, _headers) = self
109            .client
110            .post_form_raw("/audio/transcriptions", request, &self.request_options)
111            .await?;
112        Ok(bytes)
113    }
114}