fix(ai): increase timeout to 60s, keep model resident in RAM for 24h, and warm up on startup
This commit is contained in:
@@ -223,6 +223,7 @@ async def translate_sentence_ai(
|
||||
"prompt": prompt,
|
||||
"stream": False,
|
||||
"format": "json",
|
||||
"keep_alive": "24h",
|
||||
"options": {
|
||||
"temperature": 0.2,
|
||||
"num_predict": 300,
|
||||
@@ -234,7 +235,7 @@ async def translate_sentence_ai(
|
||||
source = f"ollama:{config.OLLAMA_MODEL}"
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=35.0) as client:
|
||||
async with httpx.AsyncClient(timeout=60.0) as client:
|
||||
resp = await client.post(f"{config.OLLAMA_BASE_URL}/api/generate", json=payload)
|
||||
if resp.status_code == 200:
|
||||
data = resp.json()
|
||||
@@ -282,7 +283,8 @@ async def translate_sentence_ai(
|
||||
else:
|
||||
logger.warning(f"Ollama returned HTTP {resp.status_code}: {resp.text}")
|
||||
except Exception as e:
|
||||
logger.warning(f"Ollama connection error: {e}")
|
||||
err_msg = f"{type(e).__name__}: {e}" if str(e) else type(e).__name__
|
||||
logger.warning(f"Ollama connection error ({err_msg})")
|
||||
|
||||
# 4. Graceful Fallback if Ollama is unreachable
|
||||
if not sentence_ru:
|
||||
@@ -329,3 +331,22 @@ async def get_ollama_status() -> Dict[str, Any]:
|
||||
"models": [],
|
||||
"active_model": config.OLLAMA_MODEL,
|
||||
}
|
||||
|
||||
|
||||
async def warmup_ollama_model() -> bool:
|
||||
"""Preloads the configured Ollama model into RAM with keep_alive to eliminate cold-start latency."""
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=60.0) as client:
|
||||
resp = await client.post(
|
||||
f"{config.OLLAMA_BASE_URL}/api/generate",
|
||||
json={"model": config.OLLAMA_MODEL, "keep_alive": "24h"},
|
||||
)
|
||||
if resp.status_code == 200:
|
||||
logger.info(f"Ollama model '{config.OLLAMA_MODEL}' warmed up successfully (keep_alive=24h).")
|
||||
return True
|
||||
else:
|
||||
logger.warning(f"Ollama warmup returned HTTP {resp.status_code}: {resp.text}")
|
||||
except Exception as e:
|
||||
err_msg = f"{type(e).__name__}: {e}" if str(e) else type(e).__name__
|
||||
logger.warning(f"Ollama warmup deferred ({err_msg})")
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user