diff --git a/crates/jarvis-cli/Cargo.toml b/crates/jarvis-cli/Cargo.toml index 64644a9..72fba9d 100644 --- a/crates/jarvis-cli/Cargo.toml +++ b/crates/jarvis-cli/Cargo.toml @@ -7,7 +7,7 @@ repository.workspace = true edition.workspace = true [dependencies] -jarvis-core = { path = "../jarvis-core", default-features = false, features = ["intent"]} +jarvis-core = { path = "../jarvis-core", default-features = false, features = ["intent", "llm"]} tokio = { version = "1", features = ["rt-multi-thread", "sync", "macros"] } log.workspace = true env_logger = "0.11" diff --git a/crates/jarvis-cli/src/main.rs b/crates/jarvis-cli/src/main.rs index d161b5d..376b141 100644 --- a/crates/jarvis-cli/src/main.rs +++ b/crates/jarvis-cli/src/main.rs @@ -1,7 +1,10 @@ use std::io::{self, Write}; +use jarvis_core::llm::{ChatMessage, LlmClient}; use jarvis_core::{COMMANDS_LIST, DB, JCommandsList, commands, config, db, intent}; +const ASK_MAX_TOKENS: u32 = 512; + fn print_help() { println!(" --## Jarvis CLI - Testing Tool ##-- @@ -9,6 +12,7 @@ fn print_help() { Commands: classify - Test intent classification execute - Simulate voice input and execute command + ask - Send prompt to Groq LLM and print reply list - List all loaded commands phrases - List all training phrases hash - Show commands hash @@ -92,13 +96,45 @@ async fn execute_text(commands: &[JCommandsList], text: &str) { } } +fn ask_llm(prompt: &str) -> i32 { + let client = match LlmClient::from_env() { + Ok(c) => c, + Err(e) => { + eprintln!("LLM not configured: {}", e); + eprintln!("Set GROQ_TOKEN (and optionally GROQ_BASE_URL, GROQ_MODEL)."); + return 2; + } + }; + let messages = [ChatMessage::user(prompt)]; + match client.complete(&messages, ASK_MAX_TOKENS) { + Ok(reply) => { + println!("{}", reply); + 0 + } + Err(e) => { + eprintln!("LLM request failed: {}", e); + 1 + } + } +} + #[tokio::main] async fn main() -> Result<(), Box> { + let argv: Vec = std::env::args().collect(); + if argv.len() >= 2 && argv[1] == "ask" { + if argv.len() < 3 { + eprintln!("Usage: jarvis-cli ask "); + std::process::exit(2); + } + let prompt = argv[2..].join(" "); + std::process::exit(ask_llm(&prompt)); + } + // init logging env_logger::Builder::from_env( env_logger::Env::default().default_filter_or("info") ).init(); - + println!("Jarvis CLI v{}", config::APP_VERSION.unwrap_or("unknown")); // init dirs @@ -189,6 +225,13 @@ async fn main() -> Result<(), Box> { execute_text(COMMANDS_LIST.get().unwrap(), arg).await; } } + "ask" | "a" => { + if arg.is_empty() { + println!(" Usage: ask "); + } else { + ask_llm(arg); + } + } "reload" => { println!(" Note: Reload requires app restart (statics can't be reset)"); }