fix: LLM-Timeout 15s — qwen2.5:7b auf CPU braucht 9s, 3s war zu kurz

This commit is contained in:
Bernd (Hermes)
2026-07-27 17:38:06 +02:00
parent d265624f1c
commit 407014db98
4 changed files with 37 additions and 37 deletions
+2 -2
View File
@@ -3123,10 +3123,10 @@ class RocketChatAdapter(BasePlatformAdapter):
try: try:
decision = await asyncio.wait_for( decision = await asyncio.wait_for(
self.archivar.classify_and_route(routing_text, origin_room_id=room_id, is_dm=_is_dm), self.archivar.classify_and_route(routing_text, origin_room_id=room_id, is_dm=_is_dm),
timeout=4.0, timeout=20.0,
) )
except asyncio.TimeoutError: except asyncio.TimeoutError:
logger.warning("[rocketchat] Archivar classify_and_route() timed out (4s) — STAY fallback") logger.warning("[rocketchat] Archivar classify_and_route() timed out (20s) — STAY fallback")
decision = RoutingDecision( decision = RoutingDecision(
category='direct_question', category='direct_question',
target_room=room_id, target_room=room_id,
+14 -14
View File
@@ -551,15 +551,15 @@ class IntentClassifier:
async def _llm_classify(self, text: str, scores: Dict[str, float]) -> Optional[Tuple[str, float]]: async def _llm_classify(self, text: str, scores: Dict[str, float]) -> Optional[Tuple[str, float]]:
import asyncio import asyncio
try: try:
# P0-CRITICAL: asyncio.wait_for(3s) — muss KLEINER sein als # P0-CRITICAL: asyncio.wait_for(15s) — muss KLEINER sein als
# der äußere 4s-Timeout in adapter.py, damit die Timeout-Hierarchie # der äußere 20s-Timeout in adapter.py, damit die Timeout-Hierarchie
# korrekt ist: 4s (außen) > 3s (innen). # korrekt ist: 20s (außen) > 15s (innen).
return await asyncio.wait_for( return await asyncio.wait_for(
asyncio.to_thread(self._llm_classify_sync, text, scores), asyncio.to_thread(self._llm_classify_sync, text, scores),
timeout=3.0, timeout=15.0,
) )
except asyncio.TimeoutError: except asyncio.TimeoutError:
logger.warning("LLM classify timed out (3s) — returning None") logger.warning("LLM classify timed out (15s) — returning None")
return None return None
def _llm_classify_sync(self, text: str, scores: Dict[str, float]) -> Optional[Tuple[str, float]]: def _llm_classify_sync(self, text: str, scores: Dict[str, float]) -> Optional[Tuple[str, float]]:
@@ -591,16 +591,16 @@ class IntentClassifier:
try: try:
if not self._llm_client or not self._llm_enabled: if not self._llm_client or not self._llm_enabled:
return None return None
# P0-CRITICAL: asyncio.wait_for(3s) — muss KLEINER sein als # P0-CRITICAL: asyncio.wait_for(15s) — muss KLEINER sein als
# der äußere 4s-Timeout in adapter.py, damit die Timeout-Hierarchie # der äußere 20s-Timeout in adapter.py, damit die Timeout-Hierarchie
# korrekt ist: 4s (außen) > 3s (innen). # korrekt ist: 20s (außen) > 15s (innen).
# Bei Timeout: return None → Router wählt STAY-Fallback (router.py Z.1583). # Bei Timeout: return None → Router wählt STAY-Fallback (router.py Z.1583).
return await asyncio.wait_for( return await asyncio.wait_for(
asyncio.to_thread(self._llm_client.generate, prompt), asyncio.to_thread(self._llm_client.generate, prompt),
timeout=3.0, timeout=15.0,
) )
except asyncio.TimeoutError: except asyncio.TimeoutError:
logger.warning("LLM raw call timed out (3s) — returning None for STAY fallback") logger.warning("LLM raw call timed out (15s) — returning None for STAY fallback")
return None return None
except Exception as e: except Exception as e:
logger.warning("LLM raw call failed: %s", e) logger.warning("LLM raw call failed: %s", e)
@@ -733,16 +733,16 @@ class IntentClassifier:
best_score >= CONFIDENCE_THRESHOLDS["CLARIFY_NEEDED"] and best_score >= CONFIDENCE_THRESHOLDS["CLARIFY_NEEDED"] and
best_score < CONFIDENCE_THRESHOLDS["AUTO_ROUTE"] best_score < CONFIDENCE_THRESHOLDS["AUTO_ROUTE"]
): ):
# P0-CRITICAL: Timeout 3s — muss KLEINER sein als der äußere # P0-CRITICAL: Timeout 15s — muss KLEINER sein als der äußere
# 4s-Timeout in adapter.py. Verhindert ThreadPool-Saturation # 20s-Timeout in adapter.py. Verhindert ThreadPool-Saturation
# durch hängende verify_confidence-Calls. # durch hängende verify_confidence-Calls.
import concurrent.futures import concurrent.futures
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as _exec: with concurrent.futures.ThreadPoolExecutor(max_workers=1) as _exec:
_future = _exec.submit(self._llm_classify_sync, text, scores) _future = _exec.submit(self._llm_classify_sync, text, scores)
try: try:
llm_result = _future.result(timeout=3.0) llm_result = _future.result(timeout=15.0)
except concurrent.futures.TimeoutError: except concurrent.futures.TimeoutError:
logger.warning("LLM verify_confidence timed out (3s) — skipping verification") logger.warning("LLM verify_confidence timed out (15s) — skipping verification")
llm_result = None llm_result = None
if llm_result: if llm_result:
final_category, final_score = llm_result final_category, final_score = llm_result
+3 -3
View File
@@ -17,7 +17,7 @@ OLLAMA_CONFIG: dict = {
"base_url": os.getenv("ARCHIVAR_EMBEDDING_BASE_URL", "http://localhost:11434"), "base_url": os.getenv("ARCHIVAR_EMBEDDING_BASE_URL", "http://localhost:11434"),
"model": "nomic-embed-text", "model": "nomic-embed-text",
"timeout": 3.0, # Sekunden — HARD deadline via ThreadPoolExecutor "timeout": 3.0, # Sekunden — HARD deadline via ThreadPoolExecutor
# Muss KLEINER sein als äußerer 4s-Timeout in adapter.py # Muss KLEINER sein als äußerer 20s-Timeout in adapter.py
"embedding_dim": 768, # nomic-embed-text produziert 768-dimensionale Vektoren "embedding_dim": 768, # nomic-embed-text produziert 768-dimensionale Vektoren
} }
@@ -157,8 +157,8 @@ wird sie entsprechend klassifiziert. Alles andere → FAKT.
LLM_CONFIG = { LLM_CONFIG = {
"enabled": os.getenv("ARCHIVAR_ENABLE_LLM", "").strip().lower() in ("1", "true", "yes"), "enabled": os.getenv("ARCHIVAR_ENABLE_LLM", "").strip().lower() in ("1", "true", "yes"),
"model": "qwen2.5:7b", "model": "qwen2.5:7b",
"timeout": 3.0, # HARD deadline via ThreadPoolExecutor.result(timeout=X) "timeout": 15.0, # HARD deadline via ThreadPoolExecutor.result(timeout=X)
# Muss KLEINER sein als äußerer 4s-Timeout in adapter.py # Muss KLEINER sein als äußerer 20s-Timeout in adapter.py
"base_url": os.getenv( "base_url": os.getenv(
"ARCHIVAR_LLM_BASE_URL", "ARCHIVAR_LLM_BASE_URL",
os.getenv("OLLAMA_BASE_URL", "http://localhost:11434") os.getenv("OLLAMA_BASE_URL", "http://localhost:11434")
+18 -18
View File
@@ -1578,12 +1578,12 @@ class BidirectionalRouter:
# Try LLM — expects room ID directly or "STAY" # Try LLM — expects room ID directly or "STAY"
# Semaphore schützt vor parallelen Ollama-Aufrufen (qwen2.5:7b = single-request) # Semaphore schützt vor parallelen Ollama-Aufrufen (qwen2.5:7b = single-request)
# P0-CRITICAL: Semaphore-Acquire mit 3s Timeout — muss KLEINER sein als # P0-CRITICAL: Semaphore-Acquire mit 18s Timeout — muss KLEINER sein als
# der äußere 4s-Timeout in adapter.py. # der äußere 20s-Timeout in adapter.py.
try: try:
await asyncio.wait_for(self._llm_semaphore.acquire(), timeout=3.0) await asyncio.wait_for(self._llm_semaphore.acquire(), timeout=18.0)
except asyncio.TimeoutError: except asyncio.TimeoutError:
logger.warning("LLM semaphore acquire timed out (3s) — skipping LLM room lookup") logger.warning("LLM semaphore acquire timed out (18s) — skipping LLM room lookup")
return None, False, True # LLM-Aufruf fehlgeschlagen return None, False, True # LLM-Aufruf fehlgeschlagen
try: try:
llm_result = await self._classifier._llm_raw(prompt) llm_result = await self._classifier._llm_raw(prompt)
@@ -1848,22 +1848,22 @@ class BidirectionalRouter:
old_topic=old_topic or "(kein altes Thema)", old_topic=old_topic or "(kein altes Thema)",
) )
# LLM-Aufruf mit 3s Timeout (Semaphore + LLM-Call) # LLM-Aufruf mit 18s Semaphore + 15s LLM-Call Timeout
# P0-CRITICAL: Semaphore-Acquire mit 3s Timeout — muss KLEINER sein als # P0-CRITICAL: Semaphore-Acquire mit 18s Timeout — muss KLEINER sein als
# der äußere 4s-Timeout in adapter.py. # der äußere 20s-Timeout in adapter.py.
try: try:
await asyncio.wait_for(self._llm_semaphore.acquire(), timeout=3.0) await asyncio.wait_for(self._llm_semaphore.acquire(), timeout=18.0)
except asyncio.TimeoutError: except asyncio.TimeoutError:
logger.warning("V7-4: LLM-Semaphore Timeout (3s) → Fallback: NEU") logger.warning("V7-4: LLM-Semaphore Timeout (18s) → Fallback: NEU")
return None return None
try: try:
try: try:
llm_result = await asyncio.wait_for( llm_result = await asyncio.wait_for(
self._classifier._llm_raw(prompt), self._classifier._llm_raw(prompt),
timeout=3.0, timeout=15.0,
) )
except asyncio.TimeoutError: except asyncio.TimeoutError:
logger.warning("V7-4: LLM-Check Timeout (3s) → Fallback: NEU") logger.warning("V7-4: LLM-Check Timeout (15s) → Fallback: NEU")
return None return None
except Exception as e: except Exception as e:
logger.warning("V7-4: LLM-Check fehlgeschlagen: %s → Fallback: NEU", e) logger.warning("V7-4: LLM-Check fehlgeschlagen: %s → Fallback: NEU", e)
@@ -2207,25 +2207,25 @@ async def validate_sentence_marker(
v7_3_state=v7_3_state, v7_3_state=v7_3_state,
) )
# ── LLM-Aufruf mit 3s Timeout (Semaphore + LLM-Call) ────────────── # ── LLM-Aufruf mit 18s Semaphore + 15s LLM-Call Timeout ──────────
# P0-CRITICAL: Semaphore-Acquire mit 3s Timeout — muss KLEINER sein als # P0-CRITICAL: Semaphore-Acquire mit 18s Timeout — muss KLEINER sein als
# der äußere 4s-Timeout in adapter.py. # der äußere 20s-Timeout in adapter.py.
try: try:
await asyncio.wait_for(llm_semaphore.acquire(), timeout=3.0) await asyncio.wait_for(llm_semaphore.acquire(), timeout=18.0)
except asyncio.TimeoutError: except asyncio.TimeoutError:
logger.warning( logger.warning(
"A1b Stufe 4/5: LLM-Semaphore Timeout (3s) → Fallback: unentscheidbar" "A1b Stufe 4/5: LLM-Semaphore Timeout (18s) → Fallback: unentscheidbar"
) )
return None return None
try: try:
try: try:
llm_result = await asyncio.wait_for( llm_result = await asyncio.wait_for(
classifier._llm_raw(prompt), classifier._llm_raw(prompt),
timeout=3.0, timeout=15.0,
) )
except asyncio.TimeoutError: except asyncio.TimeoutError:
logger.warning( logger.warning(
"A1b Stufe 4/5: LLM-Validierung Timeout (3s) → Fallback: unentscheidbar" "A1b Stufe 4/5: LLM-Call Timeout (15s) → Fallback: unentscheidbar"
) )
return None return None
except Exception as e: except Exception as e: