mod private
{
use crate::
{
client ::Client,
error ::Result,
environment ::{ OpenaiEnvironment, EnvironmentInterface },
};
use crate::components::audio::
{
CreateSpeechRequest,
CreateTranscriptionRequest,
CreateTranscriptionResponseJson,
CreateTranslationRequest,
CreateTranslationResponseJson,
};
use reqwest::multipart::{ Form, Part };
#[ derive( Debug, Clone ) ]
pub struct Audio< 'client, E >
where
E : OpenaiEnvironment + EnvironmentInterface + Send + Sync + 'static,
{
client : &'client Client< E >,
}
impl< 'client, E > Audio< 'client, E >
where
E : OpenaiEnvironment + EnvironmentInterface + Send + Sync + 'static,
{
#[ inline ]
pub(crate) fn new( client : &'client Client< E > ) -> Self
{
Self { client }
}
#[ inline ]
pub async fn speech( &self, request : CreateSpeechRequest ) -> Result< Vec< u8 > >
{
let path = "/audio/speech";
self.client.post_binary( path, &request ).await
}
#[ inline ]
pub async fn transcribe( &self, request : CreateTranscriptionRequest ) -> Result< CreateTranscriptionResponseJson >
{
let file_part = Part::bytes( request.file )
.file_name( request.filename )
.mime_str( "audio/*" )
.map_err( | e | crate::error::OpenAIError::Internal( format!( "Failed to create file part : {e}" ) ) )?;
let mut form = Form::new()
.part( "file", file_part )
.text( "model", request.model );
if let Some( language ) = request.language
{
form = form.text( "language", language );
}
if let Some( prompt ) = request.prompt
{
form = form.text( "prompt", prompt );
}
if let Some( response_format ) = request.response_format
{
form = form.text( "response_format", serde_json::to_string( &response_format )
.map_err( | e | crate::error::OpenAIError::Internal( format!( "Failed to serialize response_format : {e}" ) ) )? );
}
if let Some( temperature ) = request.temperature
{
form = form.text( "temperature", temperature.to_string() );
}
if let Some( timestamp_granularities ) = request.timestamp_granularities
{
let granularities_json = serde_json::to_string( ×tamp_granularities )
.map_err( | e | crate::error::OpenAIError::Internal( format!( "Failed to serialize timestamp_granularities : {e}" ) ) )?;
form = form.text( "timestamp_granularities", granularities_json );
}
let path = "/audio/transcriptions";
self.client.post_multipart( path, form ).await
}
#[ inline ]
pub async fn translate( &self, request : CreateTranslationRequest ) -> Result< CreateTranslationResponseJson >
{
let file_part = Part::bytes( request.file )
.file_name( request.filename )
.mime_str( "audio/*" )
.map_err( | e | crate::error::OpenAIError::Internal( format!( "Failed to create file part : {e}" ) ) )?;
let mut form = Form::new()
.part( "file", file_part )
.text( "model", request.model );
if let Some( prompt ) = request.prompt
{
form = form.text( "prompt", prompt );
}
if let Some( response_format ) = request.response_format
{
form = form.text( "response_format", serde_json::to_string( &response_format )
.map_err( | e | crate::error::OpenAIError::Internal( format!( "Failed to serialize response_format : {e}" ) ) )? );
}
if let Some( temperature ) = request.temperature
{
form = form.text( "temperature", temperature.to_string() );
}
let path = "/audio/translations";
self.client.post_multipart( path, form ).await
}
}
}
crate ::mod_interface!
{
exposed use
{
Audio,
};
}