Skip to main content

Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

HTTP client for the MiMo API.

Implementations§

Source§

impl Client

Source

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");
Source

pub fn from_env() -> Result<Self>

Create a new client from the XIAOMI_API_KEY environment variable.

§Errors

Returns an error if the XIAOMI_API_KEY environment variable is not set.

§Example
use mimo_api::Client;

// Assuming XIAOMI_API_KEY is set in environment
let client = Client::from_env()?;
Source

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.

Source

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(())
}
Source

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(())
}
Source

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 an assistant message.
§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(())
}
Source

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§

Source§

impl Clone for Client

Source§

fn clone(&self) -> Client

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Client

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more