|
| 1 | +use crate::{ |
| 2 | + error::OpenAIError, |
| 3 | + traits::AsyncTryFrom, |
| 4 | + types::audio::{ |
| 5 | + CreateTranscriptionRequest, CreateTranslationRequest, TranscriptionChunkingStrategy, |
| 6 | + }, |
| 7 | + util::create_file_part, |
| 8 | +}; |
| 9 | + |
| 10 | +impl AsyncTryFrom<CreateTranscriptionRequest> for reqwest::multipart::Form { |
| 11 | + type Error = OpenAIError; |
| 12 | + |
| 13 | + async fn try_from(request: CreateTranscriptionRequest) -> Result<Self, Self::Error> { |
| 14 | + let audio_part = create_file_part(request.file.source).await?; |
| 15 | + |
| 16 | + let mut form = reqwest::multipart::Form::new() |
| 17 | + .part("file", audio_part) |
| 18 | + .text("model", request.model); |
| 19 | + |
| 20 | + if let Some(language) = request.language { |
| 21 | + form = form.text("language", language); |
| 22 | + } |
| 23 | + |
| 24 | + if let Some(prompt) = request.prompt { |
| 25 | + form = form.text("prompt", prompt); |
| 26 | + } |
| 27 | + |
| 28 | + if let Some(response_format) = request.response_format { |
| 29 | + form = form.text("response_format", response_format.to_string()) |
| 30 | + } |
| 31 | + |
| 32 | + if let Some(temperature) = request.temperature { |
| 33 | + form = form.text("temperature", temperature.to_string()) |
| 34 | + } |
| 35 | + |
| 36 | + if let Some(include) = request.include { |
| 37 | + for inc in include { |
| 38 | + form = form.text("include[]", inc.to_string()); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if let Some(timestamp_granularities) = request.timestamp_granularities { |
| 43 | + for tg in timestamp_granularities { |
| 44 | + form = form.text("timestamp_granularities[]", tg.to_string()); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if let Some(stream) = request.stream { |
| 49 | + form = form.text("stream", stream.to_string()); |
| 50 | + } |
| 51 | + |
| 52 | + if let Some(chunking_strategy) = request.chunking_strategy { |
| 53 | + match chunking_strategy { |
| 54 | + TranscriptionChunkingStrategy::Auto => { |
| 55 | + form = form.text("chunking_strategy", "auto"); |
| 56 | + } |
| 57 | + TranscriptionChunkingStrategy::ServerVad(vad_config) => { |
| 58 | + form = form.text( |
| 59 | + "chunking_strategy", |
| 60 | + serde_json::to_string(&vad_config).unwrap().to_string(), |
| 61 | + ); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if let Some(known_speaker_names) = request.known_speaker_names { |
| 67 | + for kn in known_speaker_names { |
| 68 | + form = form.text("known_speaker_names[]", kn.to_string()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if let Some(known_speaker_references) = request.known_speaker_references { |
| 73 | + for kn in known_speaker_references { |
| 74 | + form = form.text("known_speaker_references[]", kn.to_string()); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + Ok(form) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl AsyncTryFrom<CreateTranslationRequest> for reqwest::multipart::Form { |
| 83 | + type Error = OpenAIError; |
| 84 | + |
| 85 | + async fn try_from(request: CreateTranslationRequest) -> Result<Self, Self::Error> { |
| 86 | + let audio_part = create_file_part(request.file.source).await?; |
| 87 | + |
| 88 | + let mut form = reqwest::multipart::Form::new() |
| 89 | + .part("file", audio_part) |
| 90 | + .text("model", request.model); |
| 91 | + |
| 92 | + if let Some(prompt) = request.prompt { |
| 93 | + form = form.text("prompt", prompt); |
| 94 | + } |
| 95 | + |
| 96 | + if let Some(response_format) = request.response_format { |
| 97 | + form = form.text("response_format", response_format.to_string()) |
| 98 | + } |
| 99 | + |
| 100 | + if let Some(temperature) = request.temperature { |
| 101 | + form = form.text("temperature", temperature.to_string()) |
| 102 | + } |
| 103 | + Ok(form) |
| 104 | + } |
| 105 | +} |
0 commit comments