feat: tools bei Modellen ohne Werkzeug-Vorlage klar ablehnen, nicht bestandenen Werkzeug-Test anzeigen (#1268)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 12:28:21 +01:00
parent 21d419aab1
commit 2ad3121c7c
9 changed files with 168 additions and 3 deletions

View File

@ -85,6 +85,12 @@ pub trait Engine: Send + Sync {
/// Adresse, unter der die Instanz erreichbar ist.
fn endpoint(&self, instance: &InstanceHandle) -> Endpoint;
/// Kann das geladene Modell Werkzeuge (`tools`) entgegennehmen? `None`: Die Engine sagt dazu nichts,
/// die Anfrage geht unverändert weiter.
async fn tool_support(&self, _instance: &InstanceHandle) -> Option<bool> {
None
}
/// Misst den echten Ressourcenverbrauch.
async fn measure(&self, instance: &InstanceHandle) -> Usage;
}

View File

@ -77,6 +77,8 @@ pub(crate) struct CommandLine {
struct Managed {
child: Child,
port: u16,
/// Laut `/props` kennt die Chat-Vorlage Werkzeuge (`None`: keine Auskunft).
tools: Option<bool>,
}
pub struct LlamaCppEngine {
@ -265,10 +267,17 @@ impl LlamaCppEngine {
}
let pid = child.id();
let tools = template_tool_support(port).await;
if tools == Some(false) {
tracing::info!(
instance_id = id,
"Die Chat-Vorlage des Modells kennt keine Werkzeuge"
);
}
self.children
.lock()
.await
.insert(id, Managed { child, port });
.insert(id, Managed { child, port, tools });
Ok(InstanceHandle { id, pid, port })
}
@ -464,6 +473,14 @@ impl Engine for LlamaCppEngine {
}
}
async fn tool_support(&self, instance: &InstanceHandle) -> Option<bool> {
self.children
.lock()
.await
.get(&instance.id)
.and_then(|m| m.tools)
}
async fn measure(&self, instance: &InstanceHandle) -> Usage {
let Some(pid) = instance.pid else {
return Usage::default();
@ -477,6 +494,24 @@ impl Engine for LlamaCppEngine {
}
}
/// Fragt `GET /props`, ob die Chat-Vorlage Werkzeuge kennt (`chat_template_caps.supports_tools`). Kennt sie
/// keine, lässt llama-server die Definitionen ohne Meldung weg (→ #1268). Ältere Versionen und Fehler: `None`.
async fn template_tool_support(port: u16) -> Option<bool> {
let response = reqwest::Client::new()
.get(format!("http://127.0.0.1:{port}/props"))
.timeout(Duration::from_secs(5))
.send()
.await
.ok()?;
if !response.status().is_success() {
return None;
}
let props: serde_json::Value = serde_json::from_slice(&response.bytes().await.ok()?).ok()?;
props
.pointer("/chat_template_caps/supports_tools")?
.as_bool()
}
/// Liest aus der Ausgabe von `nvidia-smi --query-compute-apps=pid,used_memory
/// --format=csv,noheader,nounits` den VRAM (MiB) eines Prozesses; mehrere
/// Zeilen (mehrere GPUs) werden addiert.

View File

@ -65,6 +65,12 @@ class Handler(BaseHTTPRequestHandler):
self._send_json(200, {"status": "ok"})
else:
self._send_json(503, {"error": {"message": "Loading model"}})
elif self.path == "/props":
kann = os.environ.get("FAKE_LLAMA_TOOLS")
if kann is None:
self._send_json(404, {})
else:
self._send_json(200, {"chat_template_caps": {"supports_tools": kann == "1"}})
elif self.path == "/v1/models":
alias = opt("--alias") or "modell"
self._send_json(200, {"object": "list", "data": [{"id": alias, "object": "model", "created": 0, "owned_by": "fake"}]})

View File

@ -111,6 +111,27 @@ async fn start_bereit_log_und_stopp() {
engine.stop(&inst, Duration::from_secs(1)).await.unwrap();
}
#[tokio::test]
async fn auskunft_der_chat_vorlage_zu_werkzeugen() {
for (wert, erwartet) in [
(Some("0"), Some(false)),
(Some("1"), Some(true)),
(None, None),
] {
let mut f = fixture("ok", 20);
if let Some(wert) = wert {
f.config
.env
.insert("FAKE_LLAMA_TOOLS".to_string(), wert.to_string());
}
let engine = LlamaCppEngine::new(f.config.clone());
let inst = engine.start(&profile(&f)).await.expect("Start");
assert_eq!(engine.tool_support(&inst).await, erwartet, "{wert:?}");
engine.stop(&inst, Duration::from_secs(5)).await.unwrap();
assert_eq!(engine.tool_support(&inst).await, None);
}
}
#[tokio::test]
async fn von_aussen_beendeter_prozess_wird_als_gone_erkannt() {
let f = fixture("ok", 20);