Generation Generation — Rust examples backed by real provider calls. rust examples examples/generation src/examples/rust/generation example Generation

These Rust examples are real runnable files. Edit the source file first; this page is rebuilt from the checked-in example and its metadata header.

Rust Typed Generation

Runs a small typed generation program against OpenAI.

Rust
use axllm::{ax, AxResult, OpenAICompatibleClient};
use serde_json::json;
use std::env;


fn openai_client() -> AxResult<OpenAICompatibleClient> {
    let api_key = env::var("OPENAI_API_KEY").or_else(|_| env::var("OPENAI_APIKEY")).map_err(|_| axllm::AxError::runtime("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example."))?;
    let model = env::var("AX_OPENAI_MODEL").unwrap_or_else(|_| "gpt-5.4-mini".to_string());
    Ok(OpenAICompatibleClient::new(api_key, model).with_model_config(json!({"temperature": 0})))
}

fn main() -> AxResult<()> {
    let mut client = openai_client()?;
    let mut program = ax("question:string -> answer:string")?;
    let output = program.forward(&mut client, json!({"question": "In one sentence, explain Ax as a language-agnostic LLM programming library."}))?;
    println!("{}", serde_json::to_string_pretty(&output)?);
    Ok(())
}

Rust Structured Extraction

Extracts structured fields and labels from support text with OpenAI.

Rust
use axllm::{ax, AxResult, OpenAICompatibleClient};
use serde_json::json;
use std::env;


fn openai_client() -> AxResult<OpenAICompatibleClient> {
    let api_key = env::var("OPENAI_API_KEY").or_else(|_| env::var("OPENAI_APIKEY")).map_err(|_| axllm::AxError::runtime("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example."))?;
    let model = env::var("AX_OPENAI_MODEL").unwrap_or_else(|_| "gpt-5.4-mini".to_string());
    Ok(OpenAICompatibleClient::new(api_key, model).with_model_config(json!({"temperature": 0})))
}

fn main() -> AxResult<()> {
    let mut client = openai_client()?;
    let mut program = ax("ticket:string -> priority:class \"high, normal, low\", summary:string, labels:string[]")?;
    let output = program.forward(&mut client, json!({"ticket": "Checkout has failed for enterprise customers since 09:00. Support wants a concise summary and tags."}))?;
    println!("{}", serde_json::to_string_pretty(&output)?);
    Ok(())
}

Rust Signature Constraints

Builds native constrained fields and runs the signature with OpenAI.

Rust
use axllm::{ax, f, AxResult, FieldType, OpenAICompatibleClient};
use serde_json::json;
use std::env;

fn openai_client() -> AxResult<OpenAICompatibleClient> {
    let api_key = env::var("OPENAI_API_KEY")
        .or_else(|_| env::var("OPENAI_APIKEY"))
        .map_err(|_| {
            axllm::AxError::runtime("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.")
        })?;
    let model = env::var("AX_OPENAI_MODEL").unwrap_or_else(|_| "gpt-5.4-mini".to_string());
    Ok(OpenAICompatibleClient::new(api_key, model).with_model_config(json!({"temperature": 0})))
}

fn main() -> AxResult<()> {
    let mut request_type = FieldType::string();
    request_type.min_length = Some(10.0);
    request_type.max_length = Some(500.0);
    request_type.description = Some("Booking request".to_string());
    let mut email_type = FieldType::string();
    email_type.format = Some("email".to_string());
    email_type.description = Some("Contact email".to_string());
    let mut party_type = FieldType::number();
    party_type.minimum = Some(1.0);
    party_type.maximum = Some(12.0);
    party_type.description = Some("Guests".to_string());
    let mut code_type = FieldType::string();
    code_type.pattern = Some(r"^[A-Z]{3}-\d{4}$".to_string());
    code_type.pattern_description = Some("Must look like ABC-1234".to_string());

    let signature = f()
        .input("requestText", request_type)
        .input("contactEmail", email_type)
        .output("partySize", party_type)
        .output("bookingCode", code_type)
        .build();
    let mut program = ax("requestText:string -> partySize:number, bookingCode:string")?;
    program.signature = signature;
    let mut client = openai_client()?;
    let output = program.forward(
        &mut client,
        json!({
            "requestText": "Book dinner for four people under the name Ada Lovelace.",
            "contactEmail": "ada@example.com"
        }),
    )?;
    println!("{}", serde_json::to_string_pretty(&output)?);
    Ok(())
}

Centralized Usage Observer

Attributes every completed model call to a tenant, user, and request from one global observer.

Rust
use axllm::{
    set_usage_observer, AxAIClient, AxError, AxResult, AxUsageEvent, OpenAICompatibleClient,
};
use serde_json::json;
use std::env;
use std::sync::{Arc, Mutex};
use std::time::{SystemTime, UNIX_EPOCH};

fn main() -> AxResult<()> {
    let api_key = env::var("OPENAI_API_KEY")
        .or_else(|_| env::var("OPENAI_APIKEY"))
        .map_err(|_| AxError::runtime("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example."))?;
    let model = env::var("AX_OPENAI_MODEL").unwrap_or_else(|_| "gpt-5.4-mini".to_string());
    let events = Arc::new(Mutex::new(Vec::<AxUsageEvent>::new()));
    let captured = Arc::clone(&events);
    set_usage_observer(Some(Arc::new(move |event| {
        captured.lock().unwrap().push(event);
    })));

    let mut client = OpenAICompatibleClient::new(api_key, model).with_options(json!({
        "usageContext": {
            "tenantId": "tenant-42",
            "feature": "support-chat",
            "attributes": {"environment": "example"}
        }
    }));
    let request_id = format!(
        "request-{}",
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|error| AxError::runtime(error.to_string()))?
            .as_nanos()
    );
    client.chat_with_options(
        json!({
            "chat_prompt": [
                {"role": "user", "content": "Reply with one short greeting."}
            ]
        }),
        json!({
            "usageContext": {"userId": "user-7", "requestId": request_id}
        }),
    )?;
    set_usage_observer(None);
    println!("{}", serde_json::to_string_pretty(&*events.lock().unwrap())?);
    Ok(())
}

Rust Contextual Generation

Answers from supplied context and returns compact citations with OpenAI.

Rust
use axllm::{ax, AxResult, OpenAICompatibleClient};
use serde_json::json;
use std::env;


fn openai_client() -> AxResult<OpenAICompatibleClient> {
    let api_key = env::var("OPENAI_API_KEY").or_else(|_| env::var("OPENAI_APIKEY")).map_err(|_| axllm::AxError::runtime("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example."))?;
    let model = env::var("AX_OPENAI_MODEL").unwrap_or_else(|_| "gpt-5.4-mini".to_string());
    Ok(OpenAICompatibleClient::new(api_key, model).with_model_config(json!({"temperature": 0})))
}

fn main() -> AxResult<()> {
    let mut client = openai_client()?;
    let mut program = ax("context:string, question:string -> answer:string, citations:string[]")?;
    let output = program.forward(&mut client, json!({"context": "Ax uses signatures, ai(), ax(), agent(), flow(), and optimize().", "question": "How should a new developer think about Ax?"}))?;
    println!("{}", serde_json::to_string_pretty(&output)?);
    Ok(())
}

Rust Adaptive Provider Balancing

Routes equivalent chat traffic using shared reliability, latency, and cost statistics.

Rust
use std::{env, sync::{Arc, Mutex}};

use axllm::{
    AxAIClient, AxBalancer, AxBalancerAdaptiveStrategy, AxBalancerOptions,
    AxInMemoryBalancerStatsStore, AxResult, OpenAICompatibleClient,
};
use serde_json::json;

fn main() -> AxResult<()> {
    let key = env::var("OPENAI_API_KEY").or_else(|_| env::var("OPENAI_APIKEY"))
        .map_err(|_| axllm::AxError::runtime("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example."))?;
    let model = env::var("AX_OPENAI_MODEL").unwrap_or_else(|_| "gpt-5.4-mini".into());
    let clients: Vec<Box<dyn AxAIClient>> = vec![
        Box::new(OpenAICompatibleClient::new(&key, &model)),
        Box::new(OpenAICompatibleClient::new(&key, &model)),
    ];

    let store = Arc::new(AxInMemoryBalancerStatsStore::new());
    let route_keys = ["openai-primary".to_string(), "openai-backup".to_string()];
    let events = Arc::new(Mutex::new(Vec::new()));
    let event_sink = events.clone();
    let strategy = AxBalancerAdaptiveStrategy::new(6_000.0, 0.02)
        .with_expected_tokens(1_200, 300)
        .with_namespace("support-summary-v1")
        .with_store(store)
        .with_route_key(Arc::new(move |_service, index| route_keys[index].clone()))
        .with_slice(Arc::new(|context| if context["options"]["stream"] == true { "streaming".into() } else { "interactive".into() }))
        .on_routing_event(Arc::new(move |event| event_sink.lock().unwrap().push(event["type"].clone())));
    let mut balancer = AxBalancer::from_clients(clients, AxBalancerOptions { strategy: Some(strategy), ..AxBalancerOptions::default() })?;
    let response = balancer.chat(json!({"model": model, "chat_prompt": [{"role": "user", "content": "Summarize why shared routing state matters."}]}))?;
    println!("{}", serde_json::to_string_pretty(&response)?);
    println!("{:?}", events.lock().unwrap());
    Ok(())
}
Docs