Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions backend/app/api/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ def progress_callback(stage, progress, message):
except Exception as e:
logger.error(f"Report generation failed: {str(e)}")
task_manager.fail_task(task_id, str(e))
finally:
# Release the model once the report is done, so it stops
# occupying memory another model may need. `finally` so a
# failed report frees it too. No-op unless the provider is
# Ollama; disable with LLM_UNLOAD_AFTER_USE=0.
try:
from ..utils.llm_client import LLMClient
if LLMClient().unload():
logger.info("LLM unloaded from Ollama")
except Exception as unload_err:
logger.warning(f"LLM unload skipped: {unload_err}")

thread = threading.Thread(target=run_generate, daemon=True)
thread.start()
Expand Down
11 changes: 11 additions & 0 deletions backend/app/api/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,17 @@ def generate_profiles():
"error": str(e),
"traceback": traceback.format_exc()
}), 500
finally:
# Release the model once profile generation ends, so it stops occupying
# memory another model may need. `finally` so a failure frees it too.
# No-op unless the provider is Ollama; disable with
# LLM_UNLOAD_AFTER_USE=0.
try:
from ..utils.llm_client import LLMClient
if LLMClient().unload():
logger.info("LLM unloaded from Ollama")
except Exception as unload_err:
logger.warning(f"LLM unload skipped: {unload_err}")


# ============== Simulation execution control interface ==============
Expand Down
14 changes: 13 additions & 1 deletion backend/app/services/simulation_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,19 @@ def profile_progress(current, total, msg):
state.error = str(e)
self._save_simulation_state(state)
raise

finally:
# Profile generation (phase 2) and config generation (phase 3) both
# drive the LLM; release the model once preparation ends so it stops
# occupying memory another model may need. `finally` so a failed
# preparation frees it too. No-op unless the provider is Ollama;
# disable with LLM_UNLOAD_AFTER_USE=0.
try:
from ..utils.llm_client import LLMClient
if LLMClient().unload():
logger.info("LLM unloaded from Ollama")
except Exception as unload_err:
logger.warning(f"LLM unload skipped: {unload_err}")

def get_simulation(self, simulation_id: str) -> Optional[SimulationState]:
"""Get simulation state"""
return self._load_simulation_state(simulation_id)
Expand Down
41 changes: 41 additions & 0 deletions backend/app/utils/llm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,47 @@ def _is_ollama(self) -> bool:
"""Check if we're talking to an Ollama server."""
return '11434' in (self.base_url or '')

def unload(self) -> bool:
"""Ask Ollama to release this model from memory. Best-effort.

Ollama keeps a model resident after a request for OLLAMA_KEEP_ALIVE
(5 minutes by default, indefinitely when set to -1). MiroFish's models
are large — granite4:small-h is ~19 GB — so holding one after a run can
block every other model sharing that Ollama instance.

The obvious fix does not work: Ollama's OpenAI-compatible
/v1/chat/completions accepts a `keep_alive` field and silently ignores
it (verified on Ollama 0.32.1 — the model still reports "Forever"),
while the native API honours it. So this posts keep_alive=0 to the
native /api/generate, which is what `ollama stop <model>` does.

No-op for non-Ollama providers. Set LLM_UNLOAD_AFTER_USE=0 to keep the
old behaviour of leaving the model resident between runs.

Returns True if the unload request was sent successfully.
"""
if not self._is_ollama():
return False
if os.environ.get('LLM_UNLOAD_AFTER_USE', '1').lower() in ('0', 'false', 'no'):
return False
try:
import urllib.request
root = self.base_url.rstrip('/')
if root.endswith('/v1'):
root = root[:-3]
payload = json.dumps({"model": self.model, "keep_alive": 0}).encode()
req = urllib.request.Request(
f"{root}/api/generate", data=payload,
headers={"Content-Type": "application/json"}, method="POST",
)
with urllib.request.urlopen(req, timeout=30):
pass
return True
except Exception:
# Freeing memory is an optimisation, never a correctness
# requirement — a failure here must not affect the caller.
return False

def chat(
self,
messages: List[Dict[str, str]],
Expand Down