uusi projekti
This commit is contained in:
@@ -109,14 +109,21 @@ impl LlmEngine {
|
||||
let model = self.model.borrow().clone();
|
||||
|
||||
let default_stop: Vec<String> = vec![
|
||||
"<|im_end|>".into(), "\n###".into(), "\nExplanation".into(),
|
||||
"\nNote:".into(), "\nPlease note".into(), "\nThis is".into(),
|
||||
"\n```\n\n".into(), "\n// Example".into(), "\n# Example".into(),
|
||||
"<|im_end|>".into(),
|
||||
];
|
||||
|
||||
let mut body = serde_json::json!({
|
||||
// Rakennetaan messages-lista (chat API)
|
||||
let mut messages = Vec::new();
|
||||
if let Some(ref sp) = opts.system_prompt {
|
||||
if !sp.is_empty() {
|
||||
messages.push(serde_json::json!({"role": "system", "content": sp}));
|
||||
}
|
||||
}
|
||||
messages.push(serde_json::json!({"role": "user", "content": prompt}));
|
||||
|
||||
let body = serde_json::json!({
|
||||
"model": model,
|
||||
"prompt": prompt,
|
||||
"messages": messages,
|
||||
"stream": false,
|
||||
"options": {
|
||||
"num_predict": opts.max_tokens,
|
||||
@@ -126,16 +133,13 @@ impl LlmEngine {
|
||||
"stop": opts.stop.as_ref().unwrap_or(&default_stop),
|
||||
}
|
||||
});
|
||||
if let Some(ref sp) = opts.system_prompt {
|
||||
body.as_object_mut().unwrap().insert("system".to_string(), serde_json::json!(sp));
|
||||
}
|
||||
|
||||
let start = Instant::now();
|
||||
let resp = self.client.post(format!("{}/api/generate", self.ollama_url))
|
||||
let resp = self.client.post(format!("{}/api/chat", self.ollama_url))
|
||||
.json(&body)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| format!("Ollama generate: {}", e))?;
|
||||
.map_err(|e| format!("Ollama chat: {}", e))?;
|
||||
|
||||
if !resp.status().is_success() {
|
||||
return Err(format!("Ollama HTTP {}", resp.status()));
|
||||
@@ -144,7 +148,7 @@ impl LlmEngine {
|
||||
let body: serde_json::Value = resp.json().await
|
||||
.map_err(|e| format!("Ollama JSON: {}", e))?;
|
||||
|
||||
let text = body["response"].as_str().unwrap_or("").to_string();
|
||||
let text = body["message"]["content"].as_str().unwrap_or("").to_string();
|
||||
let _total_duration_ns = body["total_duration"].as_u64().unwrap_or(0);
|
||||
let eval_count = body["eval_count"].as_u64().unwrap_or(0) as usize;
|
||||
let eval_duration_ns = body["eval_duration"].as_u64().unwrap_or(1);
|
||||
@@ -163,40 +167,15 @@ impl LlmEngine {
|
||||
}
|
||||
}
|
||||
|
||||
/// Siivoa markdown-koodiblokki-merkit ja selitystekstit
|
||||
/// Siivoa markdown-koodiblokki-merkit vastauksesta
|
||||
fn strip_code_fences(text: &str) -> String {
|
||||
// Poistetaan kaikki ```-rivit ja kielitunnisteet (```python, ```rust jne.)
|
||||
let lines: Vec<&str> = text.lines().collect();
|
||||
let filtered: Vec<&str> = lines.into_iter().filter(|line| {
|
||||
let trimmed = line.trim();
|
||||
// Poista rivit jotka ovat pelkkiä ``` tai ```kielitunniste
|
||||
if trimmed.starts_with("```") {
|
||||
return false;
|
||||
}
|
||||
true
|
||||
trimmed != "```" && !(trimmed.starts_with("```") && !trimmed[3..].contains('`'))
|
||||
}).collect();
|
||||
let mut result = filtered.join("\n").trim().to_string();
|
||||
|
||||
// Poista selitysteksti lopusta (kaikki rivin "\nPlease note" jälkeen jne.)
|
||||
let lower = result.to_lowercase();
|
||||
for stop in &["\nplease note", "\nthis is a basic", "\nthis code", "\nnote that", "\nremember to", "\nyou can", "\nto run"] {
|
||||
if let Some(pos) = lower.find(stop) {
|
||||
result = result[..pos].trim_end().to_string();
|
||||
}
|
||||
}
|
||||
|
||||
// Poista johdantolauseet alusta
|
||||
let lower = result.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();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
result.trim().to_string()
|
||||
filtered.join("\n").trim().to_string()
|
||||
}
|
||||
|
||||
pub struct GenerateResult {
|
||||
|
||||
Reference in New Issue
Block a user