From 3b1cb85ba27cedef5f140a986da7e7b923a1eecf Mon Sep 17 00:00:00 2001 From: Antigravity Date: Wed, 7 Oct 2026 15:30:36 +0300 Subject: [PATCH] fix(ai): configure OLLAMA_KEEP_ALIVE=5m to automatically release RAM after inactivity --- app/ai.py | 8 ++++---- app/config.py | 1 + docker-compose.yml | 3 ++- requirements.txt | 1 + 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/ai.py b/app/ai.py index e596f52..bc3b7a8 100644 --- a/app/ai.py +++ b/app/ai.py @@ -251,7 +251,7 @@ async def translate_sentence_ai( "prompt": prompt, "stream": False, "format": "json", - "keep_alive": "24h", + "keep_alive": config.OLLAMA_KEEP_ALIVE, "options": { "temperature": 0.2, "num_predict": 300, @@ -367,10 +367,10 @@ async def warmup_ollama_model() -> bool: client = get_ollama_client() resp = await client.post( f"{config.OLLAMA_BASE_URL}/api/generate", - json={"model": config.OLLAMA_MODEL, "keep_alive": "24h"}, + json={"model": config.OLLAMA_MODEL, "keep_alive": config.OLLAMA_KEEP_ALIVE}, ) if resp.status_code == 200: - logger.info(f"Ollama model '{config.OLLAMA_MODEL}' warmed up successfully (keep_alive=24h).") + logger.info(f"Ollama model '{config.OLLAMA_MODEL}' warmed up successfully (keep_alive={config.OLLAMA_KEEP_ALIVE}).") return True else: logger.warning(f"Ollama warmup returned HTTP {resp.status_code}: {resp.text}") @@ -567,7 +567,7 @@ async def generate_lesson_ai_debrief( "prompt": prompt, "stream": False, "format": "json", - "keep_alive": "24h", + "keep_alive": config.OLLAMA_KEEP_ALIVE, "options": { "temperature": 0.3, "num_predict": 450, diff --git a/app/config.py b/app/config.py index f588f0f..77afa91 100644 --- a/app/config.py +++ b/app/config.py @@ -43,4 +43,5 @@ DB_PATH.parent.mkdir(parents=True, exist_ok=True) OLLAMA_BASE_URL = os.getenv("OLLAMA_BASE_URL", "http://127.0.0.1:11434").rstrip("/") OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "qwen2.5:3b") +OLLAMA_KEEP_ALIVE = os.getenv("OLLAMA_KEEP_ALIVE", "5m") diff --git a/docker-compose.yml b/docker-compose.yml index d9001b1..5cf2d17 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,6 +18,7 @@ services: - DB_PATH=/app/data/notes.db - OLLAMA_BASE_URL=http://ollama:11434 - OLLAMA_MODEL=qwen2.5:3b + - OLLAMA_KEEP_ALIVE=5m depends_on: - ollama @@ -30,4 +31,4 @@ services: volumes: - ./data/ollama:/root/.ollama environment: - - OLLAMA_KEEP_ALIVE=24h + - OLLAMA_KEEP_ALIVE=5m diff --git a/requirements.txt b/requirements.txt index 6eb78c4..f78c4ad 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,3 +16,4 @@ typing-inspection==0.4.4 typing_extensions==4.16.0 uvicorn==0.53.0 pypdf>=5.0.0 +pytest>=8.0.0