feat: Ollama LLM backend + 5 utility packs (daily briefing, pomodoro, currency convert, stocks, habits, translate clipboard)
Local-LLM support so J.A.R.V.I.S. works offline without GROQ_TOKEN.
LLM multi-backend (crates/jarvis-core/src/llm/client.rs)
- LlmBackend enum {Groq, Ollama}. Both use OpenAI-compatible /v1/chat/completions
(Ollama exposes one natively at :11434/v1).
- LlmClient::from_env() now dispatches on JARVIS_LLM=groq|ollama. Auto-detect:
Groq if GROQ_TOKEN set, else Ollama.
- New helpers: LlmClient::groq() (was: from_env), LlmClient::ollama().
- Reqwest client now has a 60s timeout (was: unbounded — Groq freezes could hang).
- api_key skipped when empty (Ollama doesn't need auth).
- Defaults: OLLAMA_BASE_URL=http://localhost:11434/v1, OLLAMA_MODEL=qwen2.5:3b.
Override via OLLAMA_BASE_URL / OLLAMA_MODEL env.
- llm_fallback + llm_router log the active backend on init.
- Tests: 32/32 pass. Renamed `from_env_fails` → `groq_fails_when_token_missing`,
added `ollama_works_without_token`.
Daily briefing pack (resources/commands/daily_briefing/, 3 commands)
- setup.lua: "настрой утренний брифинг на 9:00" → adds a daily scheduled task
that runs now.lua via the scheduler's Lua-action support.
- now.lua: greeting + time + active profile + scheduled task count + memory recap
+ LLM motivational nudge. Voice-triggered ("утренний брифинг") works the same.
- off.lua: removes the scheduled task.
Pomodoro pack (resources/commands/pomodoro/, 2 commands)
- start.lua / stop.lua + tick.lua (re-scheduled by itself). State stored in
jarvis.state — phase alternates work (25 min) / break (5 min) until stop.
Currency conversion (resources/commands/currency/convert.lua)
- "сколько будет 1000 долларов в рублях" — fetches CBR daily rates from
cbr-xml-daily.ru, normalises by Nominal, prints both directions.
- Recognises USD/EUR/CNY/GBP/JPY/RUB by substring. Pluralises russian units.
Stocks pack (resources/commands/stocks/, 1 command)
- "сколько Сбер" → MOEX ISS /iss/engines/stock/markets/shares/securities/<TKR>.
- Mapping of 22 popular Russian tickers to common names (Сбер→SBER, Газпром→GAZP,
Лукойл→LKOH, Яндекс→YDEX, Т-банк→T, etc).
- Picks TQBR (main board) row, reports LAST + LASTTOPREVPRICE%.
Habit nudges (resources/commands/habit_nudge/, 4 commands)
- "напоминай мне пить воду" → every 2 hours
- "напоминай мне размяться" → every 50 minutes
- "напоминай отдыхать глазам" → every 20 minutes (20-20-20 rule)
- "отключи все привычки" → stops all three by id.
Translate clipboard (resources/commands/translate/clipboard.lua)
- "переведи буфер на английский" → grabs clipboard, sends to LLM, speaks first
sentence, replaces clipboard with full translation, fires notification.
Build: cargo build --release -p jarvis-app -p jarvis-gui both green. 32/32 tests.
To use Ollama:
1. Install + run Ollama (https://ollama.com).
2. `ollama pull qwen2.5:3b` (or any chat model).
3. Optional: `set JARVIS_LLM=ollama` (auto-picked if GROQ_TOKEN unset).
This commit is contained in:
parent
12b1ed4ccb
commit
45243c3e3c
22 changed files with 895 additions and 16 deletions
|
|
@ -2,12 +2,22 @@ use serde::{Deserialize, Serialize};
|
|||
use std::env;
|
||||
use thiserror::Error;
|
||||
|
||||
// Groq (cloud, fast, free tier).
|
||||
pub const DEFAULT_BASE_URL: &str = "https://api.groq.com/openai/v1";
|
||||
pub const DEFAULT_MODEL: &str = "llama-3.3-70b-versatile";
|
||||
pub const ENV_TOKEN: &str = "GROQ_TOKEN";
|
||||
pub const ENV_BASE_URL: &str = "GROQ_BASE_URL";
|
||||
pub const ENV_MODEL: &str = "GROQ_MODEL";
|
||||
|
||||
// Ollama (local, offline, privacy). Uses OpenAI-compatible /v1 endpoint.
|
||||
pub const OLLAMA_DEFAULT_BASE_URL: &str = "http://localhost:11434/v1";
|
||||
pub const OLLAMA_DEFAULT_MODEL: &str = "qwen2.5:3b";
|
||||
pub const ENV_OLLAMA_BASE_URL: &str = "OLLAMA_BASE_URL";
|
||||
pub const ENV_OLLAMA_MODEL: &str = "OLLAMA_MODEL";
|
||||
|
||||
// Top-level switch: JARVIS_LLM=groq|ollama (default: groq if GROQ_TOKEN set, else ollama).
|
||||
pub const ENV_LLM_BACKEND: &str = "JARVIS_LLM";
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ConfigError {
|
||||
#[error("missing environment variable: {0}")]
|
||||
|
|
@ -70,9 +80,25 @@ struct ResponseMessage {
|
|||
content: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum LlmBackend {
|
||||
Groq,
|
||||
Ollama,
|
||||
}
|
||||
|
||||
impl LlmBackend {
|
||||
pub fn name(&self) -> &'static str {
|
||||
match self {
|
||||
LlmBackend::Groq => "groq",
|
||||
LlmBackend::Ollama => "ollama",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LlmClient {
|
||||
backend: LlmBackend,
|
||||
base_url: String,
|
||||
api_key: String,
|
||||
api_key: String, // empty for Ollama
|
||||
model: String,
|
||||
http: reqwest::blocking::Client,
|
||||
}
|
||||
|
|
@ -82,20 +108,64 @@ impl LlmClient {
|
|||
base_url: impl Into<String>,
|
||||
api_key: impl Into<String>,
|
||||
model: impl Into<String>,
|
||||
) -> Self {
|
||||
Self::new_with_backend(LlmBackend::Groq, base_url, api_key, model)
|
||||
}
|
||||
|
||||
pub fn new_with_backend(
|
||||
backend: LlmBackend,
|
||||
base_url: impl Into<String>,
|
||||
api_key: impl Into<String>,
|
||||
model: impl Into<String>,
|
||||
) -> Self {
|
||||
Self {
|
||||
backend,
|
||||
base_url: base_url.into(),
|
||||
api_key: api_key.into(),
|
||||
model: model.into(),
|
||||
http: reqwest::blocking::Client::new(),
|
||||
http: reqwest::blocking::Client::builder()
|
||||
.timeout(std::time::Duration::from_secs(60))
|
||||
.build()
|
||||
.unwrap_or_else(|_| reqwest::blocking::Client::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_env() -> Result<Self, ConfigError> {
|
||||
pub fn groq() -> Result<Self, ConfigError> {
|
||||
let api_key = env::var(ENV_TOKEN).map_err(|_| ConfigError::MissingEnv(ENV_TOKEN))?;
|
||||
let base_url = env::var(ENV_BASE_URL).unwrap_or_else(|_| DEFAULT_BASE_URL.to_string());
|
||||
let model = env::var(ENV_MODEL).unwrap_or_else(|_| DEFAULT_MODEL.to_string());
|
||||
Ok(Self::new(base_url, api_key, model))
|
||||
Ok(Self::new_with_backend(LlmBackend::Groq, base_url, api_key, model))
|
||||
}
|
||||
|
||||
pub fn ollama() -> Self {
|
||||
let base_url = env::var(ENV_OLLAMA_BASE_URL).unwrap_or_else(|_| OLLAMA_DEFAULT_BASE_URL.to_string());
|
||||
let model = env::var(ENV_OLLAMA_MODEL).unwrap_or_else(|_| OLLAMA_DEFAULT_MODEL.to_string());
|
||||
Self::new_with_backend(LlmBackend::Ollama, base_url, "", model)
|
||||
}
|
||||
|
||||
/// Pick a backend based on `JARVIS_LLM` env var; auto-detect if unset.
|
||||
/// Default order: explicit JARVIS_LLM, else Groq if GROQ_TOKEN present, else Ollama.
|
||||
pub fn from_env() -> Result<Self, ConfigError> {
|
||||
match env::var(ENV_LLM_BACKEND).ok().map(|s| s.trim().to_lowercase()).as_deref() {
|
||||
Some("ollama") => Ok(Self::ollama()),
|
||||
Some("groq") => Self::groq(),
|
||||
Some("") | None => {
|
||||
// Auto: Groq if token present, else Ollama
|
||||
if env::var(ENV_TOKEN).is_ok() {
|
||||
Self::groq()
|
||||
} else {
|
||||
Ok(Self::ollama())
|
||||
}
|
||||
}
|
||||
Some(other) => {
|
||||
log::warn!("Unknown JARVIS_LLM='{}'; falling back to Groq", other);
|
||||
Self::groq()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn backend(&self) -> LlmBackend {
|
||||
self.backend
|
||||
}
|
||||
|
||||
pub fn model(&self) -> &str {
|
||||
|
|
@ -122,12 +192,11 @@ impl LlmClient {
|
|||
top_p,
|
||||
};
|
||||
|
||||
let resp = self
|
||||
.http
|
||||
.post(&url)
|
||||
.bearer_auth(&self.api_key)
|
||||
.json(&body)
|
||||
.send()?;
|
||||
let mut req = self.http.post(&url).json(&body);
|
||||
if !self.api_key.is_empty() {
|
||||
req = req.bearer_auth(&self.api_key);
|
||||
}
|
||||
let resp = req.send()?;
|
||||
|
||||
let status = resp.status();
|
||||
let text = resp.text()?;
|
||||
|
|
@ -173,10 +242,10 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn from_env_fails_when_token_missing() {
|
||||
fn groq_fails_when_token_missing() {
|
||||
let prev = env::var(ENV_TOKEN).ok();
|
||||
unsafe { env::remove_var(ENV_TOKEN); }
|
||||
let result = LlmClient::from_env();
|
||||
let result = LlmClient::groq();
|
||||
let err = match result {
|
||||
Ok(_) => panic!("expected ConfigError when {ENV_TOKEN} is unset"),
|
||||
Err(e) => e,
|
||||
|
|
@ -189,6 +258,13 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ollama_works_without_token() {
|
||||
let client = LlmClient::ollama();
|
||||
assert_eq!(client.backend(), LlmBackend::Ollama);
|
||||
// Default model unless OLLAMA_MODEL overrides.
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chat_message_helpers_set_role() {
|
||||
assert_eq!(ChatMessage::user("hi").role, "user");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue