Skip to main content

outfox_openai/audio/
transcriptions.rs

1use bytes::Bytes;
2
3use crate::config::Config;
4use crate::error::OpenAIError;
5#[cfg(not(target_family = "wasm"))]
6use crate::spec::audio::TranscriptionResponseStream;
7use crate::spec::audio::{
8    CreateTranscriptionRequest, CreateTranscriptionResponseDiarizedJson,
9    CreateTranscriptionResponseJson, CreateTranscriptionResponseVerboseJson,
10};
11use crate::{Client, RequestOptions};
12
13pub struct Transcriptions<'c, C: Config> {
14    client: &'c Client<C>,
15    pub(crate) request_options: RequestOptions,
16}
17
18impl<'c, C: Config> Transcriptions<'c, C> {
19    pub fn new(client: &'c Client<C>) -> Self {
20        Self {
21            client,
22            request_options: RequestOptions::new(),
23        }
24    }
25
26    /// Transcribes audio into the input language.
27    #[crate::byot(
28        T0 = Clone,
29        R = serde::de::DeserializeOwned,
30        where_clause =  "reqwest::multipart::Form: crate::traits::AsyncTryFrom<T0, Error = OpenAIError>",
31    )]
32    pub async fn create(
33        &self,
34        request: CreateTranscriptionRequest,
35    ) -> Result<CreateTranscriptionResponseJson, OpenAIError> {
36        self.client
37            .post_form("/audio/transcriptions", request, &self.request_options)
38            .await
39    }
40
41    #[cfg(not(target_family = "wasm"))]
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}