grok_api 0.1.71

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

    // ── Default model (grok-4-1-fast-reasoning) ──────────────────────────────
    // ── New flagship: grok-4.3 ──────────────────────────────────────────────────────
    println!("── Using grok-4.3 (new flagship, recommended default) ──");
    let response = client
        .chat(
            "What is the Rust programming language in two sentences?",
            Some("grok-4.3"),
        )
        .await?;
    println!("Response:\n{}\n", response);

    // ── Grok 4.20 fast non-reasoning ───────────────────────────────────────────────
    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.3 for code (replaces retired grok-code-fast-1) ──────────────────
    println!("── Using grok-4.3 for code (replaces retired grok-code-fast-1) ──");
    let response = client
        .chat(
            "Write a one-liner Rust function that returns the factorial of n using recursion.",
            Some("grok-4.3"),
        )
        .await?;
    println!("Response:\n{}\n", response);

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