Build a Rust CLI to Chat with Llama 3.2 Using Ollama in Minutes
This tutorial shows how to create a Rust command‑line tool named Jarvis that can interact with Llama 3.2 via the Ollama library, covering its architecture, key commands, code snippets, and example usage to demonstrate Rust’s suitability for AI applications.
As a Rust developer eager to apply new skills, I built a practical CLI called Jarvis that chats with advanced large language models such as Llama 3.2 using the Ollama Rust library.
Jarvis CLI Structure
1. JarvisConfig struct
Defines the available commands.
Provides methods to validate commands and print help text.
2. Command handling in main()
Parses command‑line arguments.
Dispatches to the appropriate function based on the command.
3. Command functions
time – get the current time.
date – get today’s date.
hello – print a customizable greeting.
ls – list directory contents.
chat – interact with Llama 3.2 via Ollama.
Key code excerpt:
struct JarvisConfig {
commands: Vec<&'static str>,
}
impl JarvisConfig {
fn new() -> Self { /* ... */ }
fn print_help(&self) { /* ... */ }
fn is_valid_command(&self, command: &str) -> bool { /* ... */ }
}
#[tokio::main]
async fn main() {
let config = JarvisConfig::new();
let args: Vec<String> = env::args().collect();
match args[1].as_str() {
"time" => { /* ... */ }
"date" => { /* ... */ }
"hello" => { /* ... */ }
"ls" => { /* ... */ }
"chat" => {
let ollama = Ollama::default();
match ollama.generate(GenerationRequest::new(
"llama3.2".to_string(),
args[2].to_string(),
)).await {
Ok(res) => println!("{}", res.response),
Err(e) => println!("Failed to generate response: {}", e),
}
}
_ => {
println!("Unknown command: {}", args[1]);
config.print_help();
}
}
}Creating an Ollama instance is as simple as: let ollama = Ollama::default(); Example interactions:
$ jarvis hello
Hello, World!
$ jarvis hello Alice
Hello, Alice!
$ jarvis time
Current time in format (HH:mm:ss): 14:30:15
$ jarvis ls /documents
/documents/report.pdf: file
/documents/images: directory
$ jarvis chat "What is the capital of France?"
Paris is the capital and most populous city of France.While Python remains the dominant language for AI/ML, Rust’s performance, concurrency, and safety make it an attractive alternative for building AI tools, as demonstrated by this lightweight yet functional CLI.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
