openmodex 0.1.1

Official Rust SDK for the OpenModex API
Documentation
use openmodex::{ChatCompletionRequest, ChatMessage, Error, OpenModex};

#[tokio::main]
async fn main() -> Result<(), Error> {
    // Configure the client with a fallback model chain.
    // If the primary model fails (5xx, 429, or network error),
    // the SDK automatically retries with the next model in the chain.
    let client = OpenModex::builder()
        .fallback_models(["claude-3.5-sonnet", "gpt-4o-mini", "gemini-1.5-pro"])
        .max_retries(2)
        .build()?;

    // The request targets gpt-4o, but if it fails, the SDK will
    // try claude-3.5-sonnet, then gpt-4o-mini, then gemini-1.5-pro.
    let response = client
        .chat()
        .completions()
        .create(
            ChatCompletionRequest::new("gpt-4o")
                .message(ChatMessage::user("What are the benefits of model fallbacks?"))
                .max_tokens(256),
        )
        .await?;

    let content = response.choices[0]
        .message
        .as_ref()
        .and_then(|m| m.content.as_deref())
        .unwrap_or("");
    println!("Response: {content}");

    // Check which model actually served the request.
    if let Some(meta) = &response.openmodex {
        println!("\nActual model used: {}", meta.model_used);
        println!("Provider: {}", meta.provider);
        println!("Fallback used: {}", meta.fallback_used);
    }

    Ok(())
}