From b48eeb6f5fc6ff50ef52e69780a09f9cabd5ea54 Mon Sep 17 00:00:00 2001 From: Jaakko Vanhala Date: Sun, 5 Apr 2026 09:26:26 +0300 Subject: [PATCH] Poistetaan selityskommentit LLM-vastauksista: "# This is a simple program..." -tyyppiset rivit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Malli tuottaa toisinaan selityskommentin koodin alkuun ilman markdown-wrapperia. Stripperi tunnistaa ja poistaa nämä avainsanojen perusteella (this is, simple, program that, jne.) mutta säilyttää oikeat koodikommentit ja shebangin. Co-Authored-By: Claude Opus 4.6 (1M context) --- network-poc/native-node/src/inference.rs | 15 ++++++++++++++- network-poc/node/src/qwen_coder.rs | 23 +++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/network-poc/native-node/src/inference.rs b/network-poc/native-node/src/inference.rs index fac39b1..91795d8 100644 --- a/network-poc/native-node/src/inference.rs +++ b/network-poc/native-node/src/inference.rs @@ -255,7 +255,20 @@ fn strip_markdown_wrapper(text: &str) -> String { break; } } - result.trim().to_string() + let mut lines: Vec<&str> = result.trim().lines().collect(); + while !lines.is_empty() { + let first = lines[0].trim(); + let is_preamble = first.starts_with("# ") + && !first.starts_with("#!") + && (first.to_lowercase().contains("this is") + || first.to_lowercase().contains("simple") + || first.to_lowercase().contains("program that") + || first.to_lowercase().contains("here is") + || first.to_lowercase().contains("the following") + || first.to_lowercase().contains("below")); + if is_preamble { lines.remove(0); } else { break; } + } + lines.join("\n").trim().to_string() } pub struct GenerateResult { diff --git a/network-poc/node/src/qwen_coder.rs b/network-poc/node/src/qwen_coder.rs index ca1007d..31bf791 100644 --- a/network-poc/node/src/qwen_coder.rs +++ b/network-poc/node/src/qwen_coder.rs @@ -44,7 +44,7 @@ fn strip_markdown_wrapper(text: &str) -> String { // Ei sulkevaa ``` — ota kaikki loput return code.trim().to_string(); } - // Ei koodiblokkia — poista yleiset johdantolauseet alusta + // Ei koodiblokkia — poista yleiset johdantolauseet ja selityskommentit alusta let mut result = text.to_string(); let lower = result.to_lowercase(); for prefix in &["sure!", "here is", "here's", "certainly!", "below is"] { @@ -55,7 +55,26 @@ fn strip_markdown_wrapper(text: &str) -> String { break; } } - result.trim().to_string() + // Poistetaan alun selityskommentit: "# This is a simple..." -tyyppiset rivit + // jotka eivät ole osa varsinaista koodia (esim. shebangia #! pidetään) + let mut lines: Vec<&str> = result.trim().lines().collect(); + while !lines.is_empty() { + let first = lines[0].trim(); + let is_preamble_comment = first.starts_with("# ") + && !first.starts_with("#!") + && (first.to_lowercase().contains("this is") + || first.to_lowercase().contains("simple") + || first.to_lowercase().contains("program that") + || first.to_lowercase().contains("here is") + || first.to_lowercase().contains("the following") + || first.to_lowercase().contains("below")); + if is_preamble_comment { + lines.remove(0); + } else { + break; + } + } + lines.join("\n").trim().to_string() } thread_local! {