grok_api 0.1.6

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) ──────────────────────────────
    println!("── Using default model (grok-4-1-fast-reasoning) ──");
    let response = client
        .chat(
            "What is the Rust programming language in two sentences?",
            None,
        )
        .await?;
    println!("Response:\n{}\n", response);

    // ── Flagship fast model ───────────────────────────────────────────────────
    println!("── Using grok-4.20-0309-non-reasoning (flagship fast) ──");
    let response = client
        .chat(
            "Name three reasons Rust is memory-safe.",
            Some("grok-4.20-0309-non-reasoning"),
        )
        .await?;
    println!("Response:\n{}\n", response);

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

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