Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
feat: Add support for custom environmental variables to llama.cpp (#6256)
Akarshan Biswas5c3a6fec3243
diff --git a/extensions/llamacpp-extension/settings.json b/extensions/llamacpp-extension/settings.jsonindex 46c4995ff..5f7fd1ca9 100644--- a/extensions/llamacpp-extension/settings.json+++ b/extensions/llamacpp-extension/settings.json@@ -10,7 +10,18 @@"recommended": ""}},-+ {+ "key": "llamacpp_env",+ "title": "Environmental variables",+ "description": "Environmental variables for llama.cpp(KEY=VALUE), separated by ';'",+ "controllerType": "input",+ "controllerProps": {+ "value": "none",+ "placeholder": "Eg, GGML_VK_VISIBLE_DEVICES='0,1'",+ "type": "text",+ "textAlign": "right"+ }+ },{"key": "auto_update_engine","title": "Auto update engine",diff --git a/extensions/llamacpp-extension/src/index.ts b/extensions/llamacpp-extension/src/index.tsindex eefdc44af..dc710d6e0 100644--- a/extensions/llamacpp-extension/src/index.ts+++ b/extensions/llamacpp-extension/src/index.ts@@ -41,6 +41,7 @@ type LlamacppConfig = {version_backend: stringauto_update_engine: booleanauto_unload: boolean+ llamacpp_env: stringchat_template: stringn_gpu_layers: numberoffload_mmproj: boolean@@ -153,6 +154,7 @@ const logger = {export default class llamacpp_extension extends AIEngine {provider: string = 'llamacpp'autoUnload: boolean = true+ llamacpp_env: string = ''readonly providerId: string = 'llamacpp'private config: LlamacppConfig@@ -183,6 +185,7 @@ export default class llamacpp_extension extends AIEngine {this.config = loadedConfig as LlamacppConfigthis.autoUnload = this.config.auto_unload+ this.llamacpp_env = this.config.llamacpp_env// This sets the base directory where model files for this provider are stored.this.providerPath = await joinPath([@@ -827,6 +830,8 @@ export default class llamacpp_extension extends AIEngine {closure()} else if (key === 'auto_unload') {this.autoUnload = value as boolean+ } else if (key === 'llamacpp_env') {+ this.llamacpp_env = value as string}}@@ -1253,6 +1258,27 @@ export default class llamacpp_extension extends AIEngine {}}+ private parseEnvFromString(+ target: Record<string, string>,+ envString: string+ ): void {+ envString+ .split(';')+ .filter((pair) => pair.trim())+ .forEach((pair) => {+ const [key, ...valueParts] = pair.split('=')+ const cleanKey = key?.trim()++ if (+ cleanKey &&+ valueParts.length > 0 &&+ !cleanKey.startsWith('LLAMA')+ ) {+ target[cleanKey] = valueParts.join('=').trim()+ }+ })+ }+override async load(modelId: string,overrideSettings?: Partial<LlamacppConfig>,@@ -1341,6 +1367,9 @@ export default class llamacpp_extension extends AIEngine {const api_key = await this.generateApiKey(modelId, String(port))envs['LLAMA_API_KEY'] = api_key+ // set user envs+ this.parseEnvFromString(envs, this.llamacpp_env)+// model option is required// NOTE: model_path and mmproj_path can be either relative to Jan's data folder or absolute pathconst modelPath = await joinPath([@@ -1716,6 +1745,9 @@ export default class llamacpp_extension extends AIEngine {`Invalid version/backend format: ${cfg.version_backend}. Expected format: <version>/<backend>`)}+ // set envs+ const envs: Record<string, string> = {}+ this.parseEnvFromString(envs, this.llamacpp_env)// Ensure backend is downloaded and ready before proceedingawait this.ensureBackendReady(backend, version)@@ -1726,6 +1758,7 @@ export default class llamacpp_extension extends AIEngine {const dList = await invoke<DeviceList[]>('plugin:llamacpp|get_devices', {backendPath,libraryPath,+ envs,})return dList} catch (error) {diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/src/commands.rs b/src-tauri/plugins/tauri-plugin-llamacpp/src/commands.rsindex 35dc35c5e..a25d7825f 100644--- a/src-tauri/plugins/tauri-plugin-llamacpp/src/commands.rs+++ b/src-tauri/plugins/tauri-plugin-llamacpp/src/commands.rs@@ -265,8 +265,9 @@ pub async fn unload_llama_model<R: Runtime>(pub async fn get_devices(backend_path: &str,library_path: Option<&str>,+ envs: HashMap<String, String>) -> ServerResult<Vec<DeviceInfo>> {- get_devices_from_backend(backend_path, library_path).await+ get_devices_from_backend(backend_path, library_path, envs).await}/// Generate API key using HMAC-SHA256diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/src/device.rs b/src-tauri/plugins/tauri-plugin-llamacpp/src/device.rsindex 285547209..2bb2b6f92 100644--- a/src-tauri/plugins/tauri-plugin-llamacpp/src/device.rs+++ b/src-tauri/plugins/tauri-plugin-llamacpp/src/device.rs@@ -1,4 +1,5 @@use serde::{Deserialize, Serialize};+use std::collections::HashMap;use std::process::Stdio;use std::time::Duration;use tokio::process::Command;@@ -19,6 +20,7 @@ pub struct DeviceInfo {pub async fn get_devices_from_backend(backend_path: &str,library_path: Option<&str>,+ envs: HashMap<String, String>,) -> ServerResult<Vec<DeviceInfo>> {log::info!("Getting devices from server at path: {:?}", backend_path);@@ -27,6 +29,7 @@ pub async fn get_devices_from_backend(// Configure the command to run the server with --list-deviceslet mut command = Command::new(backend_path);command.arg("--list-devices");+ command.envs(envs);// Set up library pathsetup_library_path(library_path, &mut command);