use ollama_client_rs::{
types::{ChatRequest, Message},
OllamaClient,
};
use std::env;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let ollama_url =
env::var("OLLAMA_HOST").unwrap_or_else(|_| "http://127.0.0.1:11434/api".to_string());
println!("Connecting to Ollama at {}...", ollama_url);
let client = OllamaClient::new(ollama_url);
println!("=== Available Models ===");
let models = client.list_models().await?;
for model in models.iter() {
println!("- {}", model.name);
}
let target_model = if models.iter().any(|m| m.name.starts_with("gemma4")) {
"gemma4:e2b".to_string()
} else if let Some(first) = models.first() {
first.name.clone()
} else {
println!("No models found in Ollama instance.");
return Ok(());
};
println!("\n=== Target Model: {} ===", target_model);
println!("\n=== Chat Request ===");
let request = ChatRequest {
model: target_model.clone(),
messages: vec![Message {
role: "user".to_string(),
content: "Why is the sky blue?".to_string(),
name: None,
images: None,
thinking: None,
tool_calls: None,
}],
tools: None,
format: None,
options: None,
stream: Some(false),
keep_alive: None,
};
let response = client.chat(&request).await?;
println!("Response:\n{}", response.message.content);
println!("\n=== Structured JSON Request ===");
let json_request = ChatRequest {
model: target_model.clone(),
messages: vec![
Message {
role: "system".to_string(),
content: "You are a helpful culinary assistant.".to_string(),
name: None,
images: None,
thinking: None,
tool_calls: None,
},
Message {
role: "user".to_string(),
content: "Give me a recipe for chocolate chip cookies in JSON format.".to_string(),
name: None,
images: None,
thinking: None,
tool_calls: None,
},
],
tools: None,
format: Some(serde_json::json!({
"type": "object",
"properties": {
"name": { "type": "string" },
"ingredients": {
"type": "array",
"items": { "type": "string" }
},
"instructions": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["name", "ingredients", "instructions"]
})),
options: None,
stream: Some(false),
keep_alive: None,
};
let json_response = client.chat(&json_request).await?;
println!("JSON Response:\n{}", json_response.message.content);
Ok(())
}