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(())
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnsafeUnpin for Client
impl !UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more