async_openai/audio/
translations.rs

1use bytes::Bytes;
2
3use crate::{
4    config::Config,
5    error::OpenAIError,
6    types::audio::{
7        CreateTranslationRequest, CreateTranslationResponseJson,
8        CreateTranslationResponseVerboseJson,
9    },
10    Client, RequestOptions,
11};
12
13pub struct Translations<'c, C: Config> {
14    client: &'c Client<C>,
15    pub(crate) request_options: RequestOptions,
16}
17
18impl<'c, C: Config> Translations<'c, C> {
19    pub fn new(client: &'c Client<C>) -> Self {
20        Self {
21            client,
22            request_options: RequestOptions::new(),
23        }
24    }
25
26    /// Translates audio into English.
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: CreateTranslationRequest,
35    ) -> Result<CreateTranslationResponseJson, OpenAIError> {
36        self.client
37            .post_form("/audio/translations", request, &self.request_options)
38            .await
39    }
40
41    /// Translates audio into English.
42    #[crate::byot(
43        T0 = Clone,
44        R = serde::de::DeserializeOwned,
45        where_clause =  "reqwest::multipart::Form: crate::traits::AsyncTryFrom<T0, Error = OpenAIError>",
46    )]
47    pub async fn create_verbose_json(
48        &self,
49        request: CreateTranslationRequest,
50    ) -> Result<CreateTranslationResponseVerboseJson, OpenAIError> {
51        self.client
52            .post_form("/audio/translations", request, &self.request_options)
53            .await
54    }
55
56    /// Transcribes audio into the input language.
57    pub async fn create_raw(
58        &self,
59        request: CreateTranslationRequest,
60    ) -> Result<Bytes, OpenAIError> {
61        let (bytes, _headers) = self
62            .client
63            .post_form_raw("/audio/translations", request, &self.request_options)
64            .await?;
65        Ok(bytes)
66    }
67}