#[cfg(not(feature = "local"))]
fn main() {
println!("This example requires the 'local' feature.");
println!("Run with: cargo run --example model_lifecycle --features local");
}
#[cfg(feature = "local")]
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
use oai_sdk::ModelClient;
let client = ModelClient::builder()
.base_url("http://localhost:11434")
.build()?;
println!("Ollama Client Model Lifecycle Example");
println!("This example shows how to load and unload models into memory.\n");
let model = "llama3.1:8b";
println!("Loading model '{}' into memory...", model);
match client.load_model(model).await {
Ok(response) => {
println!("Model loaded successfully!");
println!("Response: {}", response.response);
}
Err(e) => println!("Error loading model: {}", e),
}
println!("\nChecking running models:");
match client.list_running_models().await {
Ok(models) => {
if models.is_empty() {
println!("No models currently running.");
} else {
for m in models {
println!(" - {} (expires at: {})", m.name, m.expires_at);
}
}
}
Err(e) => println!("Error listing running models: {}", e),
}
println!("\nUnloading model '{}' from memory...", model);
match client.unload_model(model).await {
Ok(response) => {
println!("Model unloaded successfully!");
println!("Response: {}", response.response);
}
Err(e) => println!("Error unloading model: {}", e),
}
println!("\nModel lifecycle management complete!");
println!("Note: Loading/unloading may take a few seconds depending on the model size.");
Ok(())
}