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