audio_speech/
audio_speech.rs

1use openai_api_rs::v1::api::OpenAIClient;
2use openai_api_rs::v1::audio::{self, AudioSpeechRequest, TTS_1};
3use std::env;
4
5#[tokio::main]
6async fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let api_key = env::var("OPENAI_API_KEY").unwrap().to_string();
8    let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;
9
10    let req = AudioSpeechRequest::new(
11        TTS_1.to_string(),
12        String::from("Money is not the problem, the problem is no money."),
13        audio::VOICE_ALLOY.to_string(),
14        String::from("examples/data/problem.mp3"),
15    );
16
17    let result = client.audio_speech(req).await?;
18    println!("{:?}", result);
19
20    Ok(())
21}
22
23// OPENAI_API_KEY=xxxx cargo run --package openai-api-rs --example audio_speech