grok_api 0.2.0

Rust client library for the Grok AI API (xAI)
Documentation
//! Simple chat example — demonstrates basic usage and model selection.
//!
//! Run with:
//!   cargo run --example simple_chat

use grok_api::{GrokClient, Result};

#[tokio::main]
async fn main() -> Result<()> {
    tracing_subscriber::fmt::init();

    let api_key = std::env::var("GROK_API_KEY").expect("GROK_API_KEY environment variable not set");

    let client = GrokClient::new(&api_key)?;

    println!("🤖 Grok API — Simple Chat\n");

    // ── Current flagship: grok-4.5 (chat, code, and agentic tasks) ─────────────
    println!("── Using grok-4.5 (flagship — recommended for everything) ──");
    let response = client
        .chat(
            "What is the Rust programming language in two sentences?",
            Some("grok-4.5"),
        )
        .await?;
    println!("Response:\n{}\n", response);

    // ── Grok 4.20 fast non-reasoning (high-throughput lower-cost option) ─────────
    println!("── Using grok-4.20-non-reasoning (fast, high-throughput) ──");
    let response = client
        .chat(
            "Name three reasons Rust is memory-safe.",
            Some("grok-4.20-non-reasoning"),
        )
        .await?;
    println!("Response:\n{}\n", response);

    // ── grok-4.5 for code ────────────────────────────────────────────────────
    println!("── Using grok-4.5 for code (also the default coding model) ──");
    let response = client
        .chat(
            "Write a one-liner Rust function that returns the factorial of n using recursion.",
            Some("grok-4.5"),
        )
        .await?;
    println!("Response:\n{}\n", response);

    println!("✨ Done!");
    Ok(())
}