Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(llamacpp): name the model by its id in API responses, not its file path (#9471) (#9566)

Thinh Lec1a64a78515e
diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/shim/jan_llama_shim.cpp b/src-tauri/plugins/tauri-plugin-llamacpp/shim/jan_llama_shim.cpp
index 7f28fcc72..f569af720 100644
--- a/src-tauri/plugins/tauri-plugin-llamacpp/shim/jan_llama_shim.cpp
+++ b/src-tauri/plugins/tauri-plugin-llamacpp/shim/jan_llama_shim.cpp
@@ -462,6 +462,16 @@ jan_llama_engine * jan_llama_engine_start_from_preset(const char * ini_pat
global.apply_to_params(engine->params);
it->second.apply_to_params(engine->params);
+ // The router starts every child with `--alias <section name>`
+ // (server-models.cpp, update_args), and server_context answers with the
+ // first alias as the model name. Nothing here is a router, so without
+ // this it falls back to the model *file path* -- which is what API
+ // clients read back as `model` (`\\?\C:\...\model.gguf` on Windows).
+ // Replaced rather than added to: the router does the same, and the
+ // first member of a set of aliases is the one that gets used.
+ engine->params.model_alias.clear();
+ engine->params.model_alias.insert(preset_name);
+
// common_params_parse does this at arg.cpp:886-890; apply_to_params does
// not. Without it n_threads stays 0 and ggml_threadpool_new_impl
// memsets a zero-length array -> SIGSEGV. The router never hits this
diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/tests/engine_smoke.rs b/src-tauri/plugins/tauri-plugin-llamacpp/tests/engine_smoke.rs
index 4f922576a..6bac08477 100644
--- a/src-tauri/plugins/tauri-plugin-llamacpp/tests/engine_smoke.rs
+++ b/src-tauri/plugins/tauri-plugin-llamacpp/tests/engine_smoke.rs
@@ -252,6 +252,81 @@ fn a_preset_without_parallel_gets_the_server_side_auto_default() {
);
}
+/// llama.cpp's router starts every child with `--alias <section name>`
+/// (`server-models.cpp`, `update_args`), so its responses name the model by its
+/// id. The preset path applies the section to `common_params` itself and has no
+/// router to do that, and an unset alias makes `server_context` fall back to the
+/// model *file path* -- which API clients then read back as the `model` of every
+/// response (`\\?\C:\...\model.gguf` on Windows, #9471).
+#[test]
+fn a_preset_engine_names_the_model_by_its_section_not_its_file_path() {
+ let Some(model) = model_path() else {
+ eprintln!("skipping: set JAN_TEST_GGUF to run");
+ return;
+ };
+ let dir = tempfile::tempdir().expect("tempdir");
+ let ini = dir.path().join("router.preset.ini");
+ std::fs::write(
+ &ini,
+ format!(
+ "[*]\nfit = off\nctx-size = 2048\nno-warmup = true\n\n\
+ [jan-model-id]\nmodel = {model}\nn-gpu-layers = 0\n"
+ ),
+ )
+ .expect("write ini");
+ let engine = Engine::start_from_preset(&ini.to_string_lossy(), "jan-model-id", None)
+ .expect("engine should start from the preset");
+
+ let res = engine.request(
+ Route::ChatCompletions.as_shim_name(),
+ r#"{"messages":[{"role":"user","content":"hi"}],"max_tokens":8,"temperature":0}"#,
+ );
+ assert_eq!(res.status(), 200, "body: {}", res.body());
+ let v: serde_json::Value = serde_json::from_str(&res.body()).unwrap();
+ assert_eq!(
+ v.pointer("/model").and_then(|m| m.as_str()),
+ Some("jan-model-id"),
+ "completion: {}",
+ res.body()
+ );
+
+ // A stream carries the name on every chunk, from the first.
+ let mut res = engine.request(
+ Route::ChatCompletions.as_shim_name(),
+ r#"{"messages":[{"role":"user","content":"hi"}],"max_tokens":8,"temperature":0,"stream":true}"#,
+ );
+ assert!(res.is_stream(), "expected a streaming response");
+ // The first frame may sit in the body or arrive through the generator.
+ let mut text = res.body();
+ while !text.contains("data: {") {
+ match res.next_chunk().expect("generator threw") {
+ Some(more) => text.push_str(&more),
+ None => break,
+ }
+ }
+ let frame = text
+ .lines()
+ .filter_map(|l| l.strip_prefix("data: "))
+ .find(|f| f.starts_with('{'))
+ .expect("a JSON data frame");
+ let chunk: serde_json::Value = serde_json::from_str(frame).unwrap();
+ assert_eq!(
+ chunk.pointer("/model").and_then(|m| m.as_str()),
+ Some("jan-model-id"),
+ "first chunk: {frame}"
+ );
+ drop(res);
+
+ let res = engine.request(Route::Models.as_shim_name(), "");
+ let v: serde_json::Value = serde_json::from_str(&res.body()).unwrap();
+ assert_eq!(
+ v.pointer("/data/0/id").and_then(|m| m.as_str()),
+ Some("jan-model-id"),
+ "models: {}",
+ res.body()
+ );
+}
+
/// `server_context` is the only source of real load progress: it reports it
/// through `set_state_callback` and there is nothing to poll for it. Without
/// the shim installing that callback the desktop's whole progress chain --