pub struct Client { /* private fields */ }Expand description
HTTP client for the MiMo API.
Implementations§
Source§impl Client
impl Client
Sourcepub fn new(api_key: impl Into<String>) -> Self
pub fn new(api_key: impl Into<String>) -> Self
Create a new client with the given API key.
§Example
use mimo_api::Client;
let client = Client::new("your-api-key");Sourcepub fn with_base_url(self, base_url: impl Into<String>) -> Self
pub fn with_base_url(self, base_url: impl Into<String>) -> Self
Set a custom base URL for the API.
This is useful for testing or using a custom API endpoint.
Sourcepub async fn chat(&self, request: ChatRequest) -> Result<ChatResponse>
pub async fn chat(&self, request: ChatRequest) -> Result<ChatResponse>
Send a chat completion request.
§Example
use mimo_api::{Client, ChatRequest, Message};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::from_env()?;
let request = ChatRequest::new("mimo-v2-flash")
.message(Message::user("Hello!"));
let response = client.chat(request).await?;
println!("{}", response.choices[0].message.content);
Ok(())
}Sourcepub async fn chat_stream(
&self,
request: ChatRequest,
) -> Result<BoxStream<'static, Result<StreamChunk>>>
pub async fn chat_stream( &self, request: ChatRequest, ) -> Result<BoxStream<'static, Result<StreamChunk>>>
Send a chat completion request with streaming response.
Returns a stream of StreamChunk objects.
§Example
use mimo_api::{Client, ChatRequest, Message};
use futures::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::from_env()?;
let request = ChatRequest::new("mimo-v2-flash")
.message(Message::user("Tell me a story."))
.stream(true);
let mut stream = client.chat_stream(request).await?;
while let Some(chunk) = stream.next().await {
match chunk {
Ok(chunk) => {
if let Some(content) = &chunk.choices[0].delta.content {
print!("{}", content);
}
}
Err(e) => eprintln!("Error: {}", e),
}
}
Ok(())
}Sourcepub fn tts(&self, text: impl Into<String>) -> TtsRequestBuilder
pub fn tts(&self, text: impl Into<String>) -> TtsRequestBuilder
Create a text-to-speech request builder.
This method creates a builder for synthesizing speech from text using the mimo-v2-tts model.
§Arguments
text- The text to synthesize. This text will be placed in anassistantmessage.
§Example
use mimo_api::{Client, Voice};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::from_env()?;
let response = client.tts("Hello, world!")
.voice(Voice::DefaultEn)
.send()
.await?;
let audio = response.audio()?;
let audio_bytes = audio.decode_data()?;
std::fs::write("output.wav", audio_bytes)?;
Ok(())
}Sourcepub fn tts_styled(&self, style: &str, text: &str) -> TtsRequestBuilder
pub fn tts_styled(&self, style: &str, text: &str) -> TtsRequestBuilder
Create a text-to-speech request builder with styled text.
This method allows you to apply style controls to the synthesized speech.
§Example
use mimo_api::{Client, Voice};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::from_env()?;
// Synthesize speech with "开心" (happy) style
let response = client.tts_styled("开心", "明天就是周五了,真开心!")
.voice(Voice::DefaultZh)
.send()
.await?;
let audio = response.audio()?;
let audio_bytes = audio.decode_data()?;
std::fs::write("output.wav", audio_bytes)?;
Ok(())
}Sourcepub fn tts_stream(&self, text: impl Into<String>) -> StreamingTtsRequestBuilder
pub fn tts_stream(&self, text: impl Into<String>) -> StreamingTtsRequestBuilder
Create a streaming text-to-speech request builder.
This method creates a builder for streaming speech synthesis using the mimo-v2-tts model.
Streaming TTS delivers audio data in real-time chunks.
§Arguments
text- The text to synthesize. This text will be placed in anassistantmessage.
§Example
use mimo_api::{Client, Voice};
use futures::StreamExt;
use tokio::fs::File;
use tokio::io::AsyncWriteExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::from_env()?;
let mut stream = client.tts_stream("Hello, world!")
.voice(Voice::DefaultEn)
.send()
.await?;
let mut file = File::create("output.pcm").await?;
let mut total_bytes = 0;
while let Some(chunk) = stream.next().await {
let audio_bytes = chunk?;
file.write_all(&audio_bytes).await?;
total_bytes += audio_bytes.len();
}
println!("Total bytes: {}", total_bytes);
Ok(())
}Sourcepub fn tts_styled_stream(
&self,
style: &str,
text: &str,
) -> StreamingTtsRequestBuilder
pub fn tts_styled_stream( &self, style: &str, text: &str, ) -> StreamingTtsRequestBuilder
Create a streaming text-to-speech request builder with styled text.
This method allows you to apply style controls to the streaming synthesized speech.
§Arguments
style- The style to apply (e.g., “开心”, “悲伤”, “变快”, “变慢”)text- The text to synthesize
§Example
use mimo_api::{Client, Voice};
use futures::StreamExt;
use tokio::fs::File;
use tokio::io::AsyncWriteExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::from_env()?;
// Synthesize speech with "开心" (happy) style
let mut stream = client.tts_styled_stream("开心", "明天就是周五了,真开心!")
.voice(Voice::DefaultZh)
.send()
.await?;
let mut file = File::create("output.pcm").await?;
let mut total_bytes = 0;
while let Some(chunk) = stream.next().await {
let audio_bytes = chunk?;
file.write_all(&audio_bytes).await?;
total_bytes += audio_bytes.len();
}
println!("Total bytes: {}", total_bytes);
Ok(())
}