feat(cli): jarvis-cli ask <prompt> via Groq
One-shot invocation: jarvis-cli ask "..." reads GROQ_TOKEN, calls LlmClient::complete and prints the reply to stdout. Exits 2 when the env var is missing, 1 on API/HTTP failure. Also available as an 'ask' command inside the interactive REPL.
This commit is contained in:
parent
02f5829aaa
commit
3d9a95a2db
2 changed files with 45 additions and 2 deletions
|
|
@ -7,7 +7,7 @@ repository.workspace = true
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[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"] }
|
tokio = { version = "1", features = ["rt-multi-thread", "sync", "macros"] }
|
||||||
log.workspace = true
|
log.workspace = true
|
||||||
env_logger = "0.11"
|
env_logger = "0.11"
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,10 @@
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
|
|
||||||
|
use jarvis_core::llm::{ChatMessage, LlmClient};
|
||||||
use jarvis_core::{COMMANDS_LIST, DB, JCommandsList, commands, config, db, intent};
|
use jarvis_core::{COMMANDS_LIST, DB, JCommandsList, commands, config, db, intent};
|
||||||
|
|
||||||
|
const ASK_MAX_TOKENS: u32 = 512;
|
||||||
|
|
||||||
fn print_help() {
|
fn print_help() {
|
||||||
println!("
|
println!("
|
||||||
--## Jarvis CLI - Testing Tool ##--
|
--## Jarvis CLI - Testing Tool ##--
|
||||||
|
|
@ -9,6 +12,7 @@ fn print_help() {
|
||||||
Commands:
|
Commands:
|
||||||
classify <text> - Test intent classification
|
classify <text> - Test intent classification
|
||||||
execute <text> - Simulate voice input and execute command
|
execute <text> - Simulate voice input and execute command
|
||||||
|
ask <prompt> - Send prompt to Groq LLM and print reply
|
||||||
list - List all loaded commands
|
list - List all loaded commands
|
||||||
phrases - List all training phrases
|
phrases - List all training phrases
|
||||||
hash - Show commands hash
|
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]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let argv: Vec<String> = std::env::args().collect();
|
||||||
|
if argv.len() >= 2 && argv[1] == "ask" {
|
||||||
|
if argv.len() < 3 {
|
||||||
|
eprintln!("Usage: jarvis-cli ask <prompt>");
|
||||||
|
std::process::exit(2);
|
||||||
|
}
|
||||||
|
let prompt = argv[2..].join(" ");
|
||||||
|
std::process::exit(ask_llm(&prompt));
|
||||||
|
}
|
||||||
|
|
||||||
// init logging
|
// init logging
|
||||||
env_logger::Builder::from_env(
|
env_logger::Builder::from_env(
|
||||||
env_logger::Env::default().default_filter_or("info")
|
env_logger::Env::default().default_filter_or("info")
|
||||||
).init();
|
).init();
|
||||||
|
|
||||||
println!("Jarvis CLI v{}", config::APP_VERSION.unwrap_or("unknown"));
|
println!("Jarvis CLI v{}", config::APP_VERSION.unwrap_or("unknown"));
|
||||||
|
|
||||||
// init dirs
|
// init dirs
|
||||||
|
|
@ -189,6 +225,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
execute_text(COMMANDS_LIST.get().unwrap(), arg).await;
|
execute_text(COMMANDS_LIST.get().unwrap(), arg).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
"ask" | "a" => {
|
||||||
|
if arg.is_empty() {
|
||||||
|
println!(" Usage: ask <prompt>");
|
||||||
|
} else {
|
||||||
|
ask_llm(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
"reload" => {
|
"reload" => {
|
||||||
println!(" Note: Reload requires app restart (statics can't be reset)");
|
println!(" Note: Reload requires app restart (statics can't be reset)");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue