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