stabilität und .env variable

This commit is contained in:
Lars committed 2025-12-12 19:36:33 +01:00
1 parent fc4c868eaa
commit 16af07dd21
2 files changed
+50 -49

No files matched your search

+18 -7
View File
@@ -1,6 +1,6 @@
"""
app/services/llm_service.py — LLM Client
Version: 2.7.0 (Clean Architecture: Explicit Priority Queues)
Version: 2.8.0 (Configurable Concurrency Limit)
"""
import httpx
@@ -19,17 +19,28 @@ class Settings:
LLM_MODEL = os.getenv("MINDNET_LLM_MODEL", "phi3:mini")
PROMPTS_PATH = os.getenv("MINDNET_PROMPTS_PATH", "./config/prompts.yaml")
# NEU: Konfigurierbares Limit für Hintergrund-Last
# Default auf 2 (konservativ), kann in .env erhöht werden.
BACKGROUND_LIMIT = int(os.getenv("MINDNET_LLM_BACKGROUND_LIMIT", "2"))
def get_settings():
return Settings()
class LLMService:
# GLOBALER SEMAPHOR (Drosselung für Hintergrund-Prozesse)
_background_semaphore = asyncio.Semaphore(2)
# GLOBALER SEMAPHOR (Lazy Initialization)
# Wir initialisieren ihn erst, wenn wir die Settings kennen.
_background_semaphore = None
def __init__(self):
self.settings = get_settings()
self.prompts = self._load_prompts()
# Initialisiere Semaphore einmalig auf Klassen-Ebene basierend auf Config
if LLMService._background_semaphore is None:
limit = self.settings.BACKGROUND_LIMIT
logger.info(f"🚦 LLMService: Initializing Background Semaphore with limit: {limit}")
LLMService._background_semaphore = asyncio.Semaphore(limit)
self.timeout = httpx.Timeout(self.settings.LLM_TIMEOUT, connect=10.0)
self.client = httpx.AsyncClient(
@@ -53,7 +64,7 @@ class LLMService:
force_json: bool = False,
max_retries: int = 0,
base_delay: float = 2.0,
priority: Literal["realtime", "background"] = "realtime" # <--- NEU & EXPLIZIT
priority: Literal["realtime", "background"] = "realtime"
) -> str:
"""
Führt einen LLM Call aus.
@@ -61,13 +72,13 @@ class LLMService:
priority="background": Import/Analyse (Gedrosselt durch Semaphore).
"""
# Entscheidung basierend auf explizitem Parameter, nicht Format!
use_semaphore = (priority == "background")
if use_semaphore:
if use_semaphore and LLMService._background_semaphore:
async with LLMService._background_semaphore:
return await self._execute_request(prompt, system, force_json, max_retries, base_delay)
else:
# Realtime oder Fallback (falls Semaphore Init fehlschlug)
return await self._execute_request(prompt, system, force_json, max_retries, base_delay)
async def _execute_request(self, prompt, system, force_json, max_retries, base_delay):
@@ -123,7 +134,7 @@ class LLMService:
system=system_prompt,
max_retries=0,
force_json=False,
priority="realtime" # <--- Standard
priority="realtime"
)
async def close(self):