Update FastAPI application and related services for WP-25a: Enhance lifespan management with Mixture of Experts (MoE) integrity checks, improve logging and error handling in LLMService, and integrate profile-driven orchestration across components. Bump versions for main application, ingestion services, and LLM profiles to reflect new features and optimizations.
This commit is contained in:
1 parent
9a98093e70
commit
9b906bbabf
9 files changed
+408
-195
No files matched your search
+61
-31
@@ -1,14 +1,14 @@
|
||||
"""
|
||||
FILE: app/services/llm_service.py
|
||||
DESCRIPTION: Hybrid-Client für Ollama, Google GenAI (Gemini) und OpenRouter.
|
||||
WP-25a: Implementierung der Mixture of Experts (MoE) Profil-Steuerung.
|
||||
VERSION: 3.5.0 (WP-25a: MoE & Profile Orchestration)
|
||||
WP-25a: Implementierung der Mixture of Experts (MoE) Kaskaden-Steuerung.
|
||||
VERSION: 3.5.2 (WP-25a: MoE & Fallback Cascade Support)
|
||||
STATUS: Active
|
||||
FIX:
|
||||
- WP-25a: Profilbasiertes Routing via llm_profiles.yaml.
|
||||
- WP-25a: Unterstützung individueller Temperaturen pro Experten-Profil.
|
||||
- WP-25: Beibehaltung der Ingest-Stability (kein Schwellenwert für YES/NO).
|
||||
- WP-25: Erhalt der vollständigen v3.4.2 Resilienz-Logik.
|
||||
- WP-25a: Implementierung der rekursiven Fallback-Kaskade via fallback_profile.
|
||||
- WP-25a: Schutz gegen zirkuläre Profil-Referenzen (visited_profiles).
|
||||
- WP-25a: Erweitertes Logging für Tracing der Experten-Entscheidungen.
|
||||
- Erhalt der Ingest-Stability (WP-25) und des Rate-Limit-Managements.
|
||||
"""
|
||||
import httpx
|
||||
import yaml
|
||||
@@ -89,7 +89,6 @@ class LLMService:
|
||||
|
||||
def _load_llm_profiles(self) -> dict:
|
||||
"""WP-25a: Lädt die zentralen MoE-Profile aus der llm_profiles.yaml."""
|
||||
# Wir nutzen den in settings oder decision_engine definierten Pfad
|
||||
path_str = getattr(self.settings, "LLM_PROFILES_PATH", "config/llm_profiles.yaml")
|
||||
path = Path(path_str)
|
||||
if not path.exists():
|
||||
@@ -124,22 +123,27 @@ class LLMService:
|
||||
json_schema: Optional[Dict[str, Any]] = None,
|
||||
json_schema_name: str = "mindnet_json",
|
||||
strict_json_schema: bool = True,
|
||||
profile_name: Optional[str] = None # WP-25a
|
||||
profile_name: Optional[str] = None,
|
||||
visited_profiles: Optional[list] = None
|
||||
) -> str:
|
||||
"""
|
||||
Haupteinstiegspunkt für LLM-Anfragen mit Profil-Unterstützung.
|
||||
Haupteinstiegspunkt für LLM-Anfragen mit rekursiver Kaskaden-Logik.
|
||||
"""
|
||||
visited_profiles = visited_profiles or []
|
||||
target_provider = provider
|
||||
target_model = model_override
|
||||
target_temp = None
|
||||
fallback_profile = None
|
||||
|
||||
# WP-25a: Profil-Auflösung (Provider, Modell, Temperatur)
|
||||
# 1. Profil-Auflösung
|
||||
if profile_name and self.profiles:
|
||||
profile = self.profiles.get(profile_name)
|
||||
if profile:
|
||||
target_provider = profile.get("provider", target_provider)
|
||||
target_model = profile.get("model", target_model)
|
||||
target_temp = profile.get("temperature")
|
||||
fallback_profile = profile.get("fallback_profile")
|
||||
visited_profiles.append(profile_name)
|
||||
logger.info(f"🎭 MoE Dispatch: Profil='{profile_name}' -> Provider='{target_provider}' | Model='{target_model}'")
|
||||
else:
|
||||
logger.warning(f"⚠️ Profil '{profile_name}' nicht in llm_profiles.yaml gefunden!")
|
||||
@@ -149,26 +153,52 @@ class LLMService:
|
||||
target_provider = self.settings.MINDNET_LLM_PROVIDER
|
||||
logger.info(f"ℹ️ Kein Provider/Profil definiert. Nutze Default: {target_provider}")
|
||||
|
||||
if priority == "background":
|
||||
async with LLMService._background_semaphore:
|
||||
# 2. Ausführung mit Fehler-Handling für Kaskade
|
||||
try:
|
||||
if priority == "background":
|
||||
async with LLMService._background_semaphore:
|
||||
res = await self._dispatch(
|
||||
target_provider, prompt, system, force_json,
|
||||
max_retries, base_delay, target_model,
|
||||
json_schema, json_schema_name, strict_json_schema, target_temp
|
||||
)
|
||||
else:
|
||||
res = await self._dispatch(
|
||||
target_provider, prompt, system, force_json,
|
||||
max_retries, base_delay, target_model,
|
||||
json_schema, json_schema_name, strict_json_schema, target_temp
|
||||
)
|
||||
else:
|
||||
res = await self._dispatch(
|
||||
target_provider, prompt, system, force_json,
|
||||
max_retries, base_delay, target_model,
|
||||
json_schema, json_schema_name, strict_json_schema, target_temp
|
||||
)
|
||||
|
||||
# WP-25 Fix: Ingest-Stability (Ermöglicht YES/NO ohne Schwellenwert-Blockade)
|
||||
if not res and target_provider != "ollama":
|
||||
logger.warning(f"⚠️ [WP-25] Empty response from {target_provider}. Fallback to OLLAMA.")
|
||||
res = await self._execute_ollama(prompt, system, force_json, max_retries, base_delay, target_temp)
|
||||
# Check auf leere Cloud-Antworten (WP-25 Stability)
|
||||
if not res and target_provider != "ollama":
|
||||
logger.warning(f"⚠️ Empty response from {target_provider}. Triggering fallback chain.")
|
||||
raise ValueError(f"Empty response from {target_provider}")
|
||||
|
||||
return clean_llm_text(res) if not force_json else res
|
||||
return clean_llm_text(res) if not force_json else res
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"❌ Error during execution of profile '{profile_name}' ({target_provider}): {e}")
|
||||
|
||||
# 3. Kaskaden-Logik: Nächstes Profil in der Kette versuchen
|
||||
if fallback_profile and fallback_profile not in visited_profiles:
|
||||
logger.info(f"🔄 Switching to fallback profile: '{fallback_profile}'")
|
||||
return await self.generate_raw_response(
|
||||
prompt=prompt, system=system, force_json=force_json,
|
||||
max_retries=max_retries, base_delay=base_delay,
|
||||
priority=priority, provider=provider, model_override=model_override,
|
||||
json_schema=json_schema, json_schema_name=json_schema_name,
|
||||
strict_json_schema=strict_json_schema,
|
||||
profile_name=fallback_profile,
|
||||
visited_profiles=visited_profiles
|
||||
)
|
||||
|
||||
# 4. Ultimativer Notanker: Falls alles fehlschlägt, direkt zu Ollama
|
||||
if target_provider != "ollama" and self.settings.LLM_FALLBACK_ENABLED:
|
||||
logger.warning(f"🚨 Kaskade erschöpft. Nutze finalen Ollama-Notanker.")
|
||||
res = await self._execute_ollama(prompt, system, force_json, max_retries, base_delay)
|
||||
return clean_llm_text(res) if not force_json else res
|
||||
|
||||
raise e
|
||||
|
||||
async def _dispatch(
|
||||
self,
|
||||
@@ -182,9 +212,9 @@ class LLMService:
|
||||
json_schema: Optional[Dict[str, Any]],
|
||||
json_schema_name: str,
|
||||
strict_json_schema: bool,
|
||||
temperature: Optional[float] = None # WP-25a
|
||||
temperature: Optional[float] = None
|
||||
) -> str:
|
||||
"""Routet die Anfrage mit Rate-Limit Erkennung."""
|
||||
"""Routet die Anfrage an den spezifischen Provider-Executor."""
|
||||
rate_limit_attempts = 0
|
||||
max_rate_retries = min(max_retries, getattr(self.settings, "LLM_RATE_LIMIT_RETRIES", 3))
|
||||
wait_time = getattr(self.settings, "LLM_RATE_LIMIT_WAIT", 60.0)
|
||||
@@ -206,14 +236,13 @@ class LLMService:
|
||||
|
||||
except Exception as e:
|
||||
err_str = str(e)
|
||||
# Rate-Limit Handling (429)
|
||||
if any(x in err_str for x in ["429", "RESOURCE_EXHAUSTED", "rate_limited"]):
|
||||
rate_limit_attempts += 1
|
||||
logger.warning(f"⏳ Rate Limit {provider}. Attempt {rate_limit_attempts}. Wait {wait_time}s.")
|
||||
await asyncio.sleep(wait_time)
|
||||
continue
|
||||
|
||||
if self.settings.LLM_FALLBACK_ENABLED and provider != "ollama":
|
||||
return await self._execute_ollama(prompt, system, force_json, max_retries, base_delay, temperature)
|
||||
# Andere Fehler werden an generate_raw_response für die Kaskade gereicht
|
||||
raise e
|
||||
|
||||
async def _execute_google(self, prompt, system, force_json, model_override, temperature):
|
||||
@@ -245,7 +274,6 @@ class LLMService:
|
||||
temperature: Optional[float] = None
|
||||
) -> str:
|
||||
model = model_override or self.settings.OPENROUTER_MODEL
|
||||
# ERWEITERTES LOGGING VOR DEM CALL
|
||||
logger.info(f"🛰️ OpenRouter Call: Model='{model}' | Temp={temperature}")
|
||||
messages = []
|
||||
if system: messages.append({"role": "system", "content": system})
|
||||
@@ -274,7 +302,7 @@ class LLMService:
|
||||
return response.choices[0].message.content.strip() if response.choices[0].message.content else ""
|
||||
|
||||
async def _execute_ollama(self, prompt, system, force_json, max_retries, base_delay, temperature=None):
|
||||
# WP-25a: Nutzt Profil-Temperatur oder Standard
|
||||
# Nutzt Profil-Temperatur oder strikte Defaults für lokale Hardware-Schonung
|
||||
effective_temp = temperature if temperature is not None else (0.1 if force_json else 0.7)
|
||||
|
||||
payload = {
|
||||
@@ -294,7 +322,9 @@ class LLMService:
|
||||
return res.json().get("response", "").strip()
|
||||
except Exception as e:
|
||||
attempt += 1
|
||||
if attempt > max_retries: raise e
|
||||
if attempt > max_retries:
|
||||
logger.error(f"❌ Ollama final failure after {attempt} attempts: {e}")
|
||||
raise e
|
||||
await asyncio.sleep(base_delay * (2 ** (attempt - 1)))
|
||||
|
||||
async def generate_rag_response(self, query: str, context_str: Optional[str] = None) -> str:
|
||||
|
||||
Reference in new issue
Block a user