# src/neuro/tools/llm_agent/main.py from __future__ import annotations import shutil import subprocess import threading import time def _print(msg: str): print(f"[llm] {msg}", flush=True) def ensure_ollama(): if shutil.which("ollama"): _print("Ollama encontrado.") return _print("Ollama não encontrado. A instalar...") subprocess.run( ["bash", "-c", "curl -fsSL https://ollama.com/install.sh | sh"], check=True ) def _ollama_running(): try: subprocess.run( ["ollama", "list"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=2, ) return True except Exception: return False def ensure_ollama_serve(): if _ollama_running(): _print("Ollama serve já ativo.") return _print("A iniciar ollama serve...") def _serve(): subprocess.run(["ollama", "serve"]) t = threading.Thread(target=_serve, daemon=True) t.start() time.sleep(2)