Korjattu strip_markdown_wrapper yhteensopivaksi prefill-tekniikan kanssa

Prefill lisää ``` prompttiin jolloin malli tuottaa: "rust\nfn main()...\n```"
Vanha stripperi etsi aloittavaa ```-blokkia ja palautti tyhjän.
Uusi logiikka:
1. Poistaa kielitunnisteen ensimmäiseltä riviltä (rust, python jne.)
2. Poistaa sulkevan ``` lopusta (rfind, varmistaa ettei ole koodin sisällä)
3. Poistaa johdantolauseet ja selityskommentit kuten ennenkin

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jaakko Vanhala
2026-04-05 10:07:19 +03:00
parent b9017448d8
commit 7a1352ead7
2 changed files with 55 additions and 42 deletions

View File

@@ -234,33 +234,40 @@ impl LlmEngine {
}
}
/// Poistaa mallin tuottaman markdown-wrapperin ja johdantotekstin.
/// Siivoa mallin tuottama vastaus (prefill-yhteensopiva).
fn strip_markdown_wrapper(text: &str) -> String {
let text = text.trim();
if let Some(start) = text.find("```") {
let after = &text[start + 3..];
let code_start = after.find('\n').map(|i| i + 1).unwrap_or(0);
let code = &after[code_start..];
if let Some(end) = code.find("```") {
return code[..end].trim().to_string();
}
return code.trim().to_string();
let mut result = text.trim().to_string();
// Poistetaan kielitunniste ensimmäiseltä riviltä
if let Some(nl) = result.find('\n') {
let first = result[..nl].trim();
let is_lang = !first.is_empty()
&& first.len() <= 20
&& first.chars().all(|c| c.is_alphanumeric() || c == '+' || c == '#');
if is_lang { result = result[nl + 1..].to_string(); }
}
let mut result = text.to_string();
let lower = result.to_lowercase();
// Poistetaan sulkeva ``` lopusta
if let Some(pos) = result.rfind("```") {
if result[pos + 3..].trim().is_empty() {
result = result[..pos].to_string();
}
}
// Poistetaan johdantolauseet
let lower = result.trim().to_lowercase();
for prefix in &["sure!", "here is", "here's", "certainly!", "below is"] {
if lower.starts_with(prefix) {
if let Some(nl) = result.find('\n') {
result = result[nl + 1..].to_string();
}
if let Some(nl) = result.find('\n') { result = result[nl + 1..].to_string(); }
break;
}
}
// Poistetaan selityskommentit alusta
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("#!")
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")