Native-node: automaattinen Ollama-haistelu käynnistyksessä
Jos OLLAMA_URL ei ole asetettu, kokeillaan järjestyksessä: 1. localhost:11434 (paikallinen Ollama) 2. 127.0.0.1:11434 3. ollama:11434 (Docker-verkko) 4. host.docker.internal:11434 (Docker-kontti → isäntä) Ensimmäinen joka vastaa /api/version-kutsuun valitaan. Timeout 2s per kokeilu. Jos OLLAMA_URL on asetettu, sitä käytetään suoraan. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -8,17 +8,47 @@ pub struct LlmEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl LlmEngine {
|
impl LlmEngine {
|
||||||
pub fn load() -> Result<Self, String> {
|
pub async fn load() -> Result<Self, String> {
|
||||||
let ollama_url = std::env::var("OLLAMA_URL").unwrap_or_else(|_| "http://localhost:11434".to_string());
|
|
||||||
let model = std::env::var("OLLAMA_MODEL").unwrap_or_else(|_| "qwen2.5-coder:7b".to_string());
|
let model = std::env::var("OLLAMA_MODEL").unwrap_or_else(|_| "qwen2.5-coder:7b".to_string());
|
||||||
|
|
||||||
tracing::info!("Ollama backend: {} | malli: {}", ollama_url, model);
|
|
||||||
|
|
||||||
let client = reqwest::Client::builder()
|
let client = reqwest::Client::builder()
|
||||||
.timeout(std::time::Duration::from_secs(600))
|
.timeout(std::time::Duration::from_secs(600))
|
||||||
|
.connect_timeout(std::time::Duration::from_secs(3))
|
||||||
.build()
|
.build()
|
||||||
.map_err(|e| format!("HTTP client: {}", e))?;
|
.map_err(|e| format!("HTTP client: {}", e))?;
|
||||||
|
|
||||||
|
// Jos OLLAMA_URL on asetettu, käytetään sitä suoraan
|
||||||
|
let ollama_url = if let Ok(url) = std::env::var("OLLAMA_URL") {
|
||||||
|
tracing::info!("Ollama backend (env): {}", url);
|
||||||
|
url
|
||||||
|
} else {
|
||||||
|
// Haistellaan Ollamaa tunnetuista osoitteista
|
||||||
|
let candidates = [
|
||||||
|
"http://localhost:11434",
|
||||||
|
"http://127.0.0.1:11434",
|
||||||
|
"http://ollama:11434",
|
||||||
|
"http://host.docker.internal:11434",
|
||||||
|
];
|
||||||
|
let mut found = None;
|
||||||
|
for url in &candidates {
|
||||||
|
let probe = reqwest::Client::builder()
|
||||||
|
.connect_timeout(std::time::Duration::from_secs(2))
|
||||||
|
.build().unwrap_or(client.clone());
|
||||||
|
if let Ok(resp) = probe.get(format!("{}/api/version", url)).send().await {
|
||||||
|
if resp.status().is_success() {
|
||||||
|
tracing::info!("Ollama löytyi osoitteesta: {}", url);
|
||||||
|
found = Some(url.to_string());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
found.unwrap_or_else(|| {
|
||||||
|
tracing::warn!("Ollamaa ei löytynyt — käytetään oletusta http://localhost:11434");
|
||||||
|
"http://localhost:11434".to_string()
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
tracing::info!("Ollama backend: {} | malli: {}", ollama_url, model);
|
||||||
Ok(LlmEngine { ollama_url, model: RefCell::new(model), client })
|
Ok(LlmEngine { ollama_url, model: RefCell::new(model), client })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -287,7 +287,7 @@ async fn main() {
|
|||||||
|
|
||||||
// Ollama-backend
|
// Ollama-backend
|
||||||
tracing::info!("Alustetaan Ollama-yhteyttä...");
|
tracing::info!("Alustetaan Ollama-yhteyttä...");
|
||||||
let llm = match inference::LlmEngine::load() {
|
let llm = match inference::LlmEngine::load().await {
|
||||||
Ok(engine) => {
|
Ok(engine) => {
|
||||||
// Varmistetaan malli (ollama pull) — odotetaan kunnes valmis
|
// Varmistetaan malli (ollama pull) — odotetaan kunnes valmis
|
||||||
match engine.ensure_model().await {
|
match engine.ensure_model().await {
|
||||||
|
|||||||
Reference in New Issue
Block a user