|
1 | 1 | use std::collections::HashMap;
|
2 | 2 |
|
| 3 | +use axum::http::StatusCode; |
3 | 4 | use serde::{Deserialize, Serialize};
|
4 | 5 | use serde_json::Value;
|
5 | 6 |
|
| 7 | +use crate::AppError; |
| 8 | + |
6 | 9 | #[derive(Deserialize)]
|
7 | 10 | // https://platform.openai.com/docs/api-reference/audio/createSpeech
|
8 | 11 | pub struct SpeechOptions {
|
@@ -42,17 +45,26 @@ pub struct ProcessedSpeechOptions {
|
42 | 45 | pub provider: String,
|
43 | 46 | }
|
44 | 47 |
|
45 |
| -pub fn process_speech_options(options: SpeechOptions) -> ProcessedSpeechOptions { |
46 |
| - let vec: Vec<&str> = options.model.split('/').collect(); |
47 |
| - |
48 |
| - ProcessedSpeechOptions { |
49 |
| - input: options.input, |
50 |
| - model: vec[1].to_string(), |
51 |
| - voice: options.voice, |
52 |
| - instructions: options.instructions, |
53 |
| - response_format: options.response_format, |
54 |
| - speed: options.speed, |
55 |
| - extra: options.extra, |
56 |
| - provider: vec[0].to_string(), |
| 48 | +pub fn process_speech_options(options: SpeechOptions) -> Result<ProcessedSpeechOptions, AppError> { |
| 49 | + match options.model.split_once('/') { |
| 50 | + Some((provider, model)) => { |
| 51 | + if provider.is_empty() || model.is_empty() { |
| 52 | + Err(AppError::new(anyhow::anyhow!("Invalid model: {}", options.model), Some(StatusCode::BAD_REQUEST))) |
| 53 | + } else { |
| 54 | + Ok(ProcessedSpeechOptions { |
| 55 | + input: options.input, |
| 56 | + model: model.to_string(), |
| 57 | + voice: options.voice, |
| 58 | + instructions: options.instructions, |
| 59 | + response_format: options.response_format, |
| 60 | + speed: options.speed, |
| 61 | + extra: options.extra, |
| 62 | + provider: provider.to_string(), |
| 63 | + }) |
| 64 | + } |
| 65 | + } |
| 66 | + None => { |
| 67 | + Err(AppError::new(anyhow::anyhow!("Invalid model: {}", options.model), Some(StatusCode::BAD_REQUEST))) |
| 68 | + } |
57 | 69 | }
|
58 | 70 | }
|
0 commit comments