async_openai_alt/
audio.rs

1use bytes::Bytes;
2
3use crate::{
4    config::Config,
5    error::OpenAIError,
6    types::{
7        CreateSpeechRequest, CreateSpeechResponse, CreateTranscriptionRequest,
8        CreateTranscriptionResponseJson, CreateTranscriptionResponseVerboseJson,
9        CreateTranslationRequest, CreateTranslationResponseJson,
10        CreateTranslationResponseVerboseJson,
11    },
12    Client,
13};
14
15/// Turn audio into text or text into audio.
16/// Related guide: [Speech to text](https://platform.openai.com/docs/guides/speech-to-text)
17pub struct Audio<'c, C: Config> {
18    client: &'c Client<C>,
19}
20
21impl<'c, C: Config> Audio<'c, C> {
22    pub fn new(client: &'c Client<C>) -> Self {
23        Self { client }
24    }
25
26    /// Transcribes audio into the input language.
27    pub async fn transcribe(
28        &self,
29        request: CreateTranscriptionRequest,
30    ) -> Result<CreateTranscriptionResponseJson, OpenAIError> {
31        self.client
32            .post_form("/audio/transcriptions", request)
33            .await
34    }
35
36    /// Transcribes audio into the input language.
37    pub async fn transcribe_verbose_json(
38        &self,
39        request: CreateTranscriptionRequest,
40    ) -> Result<CreateTranscriptionResponseVerboseJson, OpenAIError> {
41        self.client
42            .post_form("/audio/transcriptions", request)
43            .await
44    }
45
46    /// Transcribes audio into the input language.
47    pub async fn transcribe_raw(
48        &self,
49        request: CreateTranscriptionRequest,
50    ) -> Result<Bytes, OpenAIError> {
51        self.client
52            .post_form_raw("/audio/transcriptions", request)
53            .await
54    }
55
56    /// Translates audio into English.
57    pub async fn translate(
58        &self,
59        request: CreateTranslationRequest,
60    ) -> Result<CreateTranslationResponseJson, OpenAIError> {
61        self.client.post_form("/audio/translations", request).await
62    }
63
64    /// Translates audio into English.
65    pub async fn translate_verbose_json(
66        &self,
67        request: CreateTranslationRequest,
68    ) -> Result<CreateTranslationResponseVerboseJson, OpenAIError> {
69        self.client.post_form("/audio/translations", request).await
70    }
71
72    /// Transcribes audio into the input language.
73    pub async fn translate_raw(
74        &self,
75        request: CreateTranslationRequest,
76    ) -> Result<Bytes, OpenAIError> {
77        self.client
78            .post_form_raw("/audio/translations", request)
79            .await
80    }
81
82    /// Generates audio from the input text.
83    pub async fn speech(
84        &self,
85        request: CreateSpeechRequest,
86    ) -> Result<CreateSpeechResponse, OpenAIError> {
87        let bytes = self.client.post_raw("/audio/speech", request).await?;
88
89        Ok(CreateSpeechResponse { bytes })
90    }
91}