From 706b1375ced965b41aac64e7aa39b7861a0854a3 Mon Sep 17 00:00:00 2001 From: pulsareonbot Date: Sat, 4 Jul 2026 16:39:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20llama-cli=20=E5=8A=A0=20--single-turn=20?= =?UTF-8?q?--no-warmup=20-sys,=E5=AF=B9=E9=BD=90=20spike=20=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E6=96=B9=E5=BC=8F=20(=E4=BF=AE=E5=A4=8D=E5=8D=A1?= =?UTF-8?q?=E6=AD=BB)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/ai/backend.rs | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/plugins/ai/backend.rs b/src/plugins/ai/backend.rs index 3086dc6..7d2109a 100644 --- a/src/plugins/ai/backend.rs +++ b/src/plugins/ai/backend.rs @@ -125,26 +125,32 @@ impl AiBackend for LocalCliBackend { history: &[(String, String)], max_tokens: u16, ) -> Result { - // 构造 llama-cli 对话 prompt - // 格式参考 spike: llama-cli -m -p -n -t 2 -c 1024 --temp 0.7 + // 对齐 spike (2026-07-03) 验证的调用方式: + // llama-cli --single-turn --no-warmup -sys -p -n -t 2 -c 1024 --temp 0.7 + // + // 关键参数: + // --single-turn 单轮模式,生成完即退出(否则进交互模式等待 stdin,永远不退出) + // --no-warmup 跳过预热(减少延迟) + // -sys system prompt(角色人设) + // -p 用户输入(只传当前消息,历史通过 context 另传,V1 简化) + // + // 历史上下文 V1 简化处理:拼到 -p 里(小模型 ctx 1024 有限,只保留最近 2 轮) let mut prompt = String::new(); - prompt.push_str(&format!("system\n{persona}\n")); - for (role, content) in history { - prompt.push_str(role); - prompt.push('\n'); - prompt.push_str(content); - prompt.push('\n'); + for (role, content) in history.iter().rev().take(4) { + // 逆序取最近 2 轮(4 条消息),正序拼接 + prompt.insert_str(0, &format!("{content}\n")); } - prompt.push_str("user\n"); prompt.push_str(user_message); - prompt.push('\n'); - prompt.push_str("assistant\n"); let tokens = if max_tokens == 0 { 128 } else { max_tokens as u32 }; let output = Command::new(self.llama_cli()) + .arg("--single-turn") + .arg("--no-warmup") .arg("-m") .arg(model_path) + .arg("-sys") + .arg(persona) .arg("-p") .arg(&prompt) .arg("-n") @@ -160,7 +166,8 @@ impl AiBackend for LocalCliBackend { if !output.status.success() { bail!( - "llama-cli 失败: {}", + "llama-cli 失败 (exit={}): {}", + output.status, String::from_utf8_lossy(&output.stderr) ); }