Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/voxctrl-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@ pub struct OllamaConfig {
pub mode: OllamaMode,
/// Used when mode == Custom. "{text}" is substituted.
pub custom_prompt: Option<String>,
/// Base URL of any OpenAI-API-compatible server (Ollama, LM Studio, OpenAI, etc.)
pub endpoint: String,
/// Optional bearer token, sent as `Authorization: Bearer <key>` when set.
#[serde(default)]
pub api_key: Option<String>,
pub timeout_secs: u64,
}

Expand All @@ -242,6 +246,7 @@ impl Default for OllamaConfig {
mode: OllamaMode::Clean,
custom_prompt: None,
endpoint: "http://localhost:11434".into(),
api_key: None,
timeout_secs: 30,
}
}
Expand Down
39 changes: 35 additions & 4 deletions crates/voxctrl-inference/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,25 @@ pub struct InferenceOutput {

// ── Engine ────────────────────────────────────────────────────────────────────

/// Cap on stored (role, content) pairs per binding — 10 user/assistant exchanges.
const MAX_OLLAMA_HISTORY_MESSAGES: usize = 20;

pub struct InferenceEngine {
config: Arc<AppConfig>,
backend: Box<dyn TranscriptionBackend>,
/// Running conversation per hotkey binding, for bindings with
/// `ollama_enabled` and a Q&A-style custom prompt. Keyed by binding id.
ollama_history: std::cell::RefCell<std::collections::HashMap<String, Vec<(String, String)>>>,
}

impl InferenceEngine {
pub fn new(config: Arc<AppConfig>) -> Self {
let backend = build_backend(&config);
Self { config, backend }
Self {
config,
backend,
ollama_history: std::cell::RefCell::new(std::collections::HashMap::new()),
}
}

/// Load the selected backend model. Blocks until ready.
Expand Down Expand Up @@ -211,22 +221,43 @@ impl InferenceEngine {
}

let client = voxctrl_llm::OllamaClient::new(ollama_cfg);
let history_key = req.binding_id.clone().unwrap_or_default();
let history = self
.ollama_history
.borrow()
.get(&history_key)
.cloned()
.unwrap_or_default();
let input_text = processed.clone();
let processed_res = match tokio::runtime::Handle::try_current() {
Ok(handle) => {
let c = client.clone();
let text = processed.clone();
let text = input_text.clone();
let hist = history.clone();
std::thread::spawn(move || {
handle.block_on(async { c.process(&text).await })
handle.block_on(async { c.process_with_history(&text, &hist).await })
}).join().unwrap_or(processed)
}
Err(_) => {
if let Ok(rt) = tokio::runtime::Builder::new_current_thread().enable_all().build() {
rt.block_on(async { client.process(&processed).await })
rt.block_on(async { client.process_with_history(&processed, &history).await })
} else {
processed
}
}
};

if !history_key.is_empty() {
let mut map = self.ollama_history.borrow_mut();
let entries = map.entry(history_key).or_default();
entries.push(("user".to_string(), input_text));
entries.push(("assistant".to_string(), processed_res.clone()));
let len = entries.len();
if len > MAX_OLLAMA_HISTORY_MESSAGES {
entries.drain(0..len - MAX_OLLAMA_HISTORY_MESSAGES);
}
}

processed = processed_res;
}

Expand Down
Loading
Loading