Update Decision Engine and related components for WP-25a: Bump version to 1.2.0, enhance multi-stream retrieval with pre-synthesis compression, and integrate Mixture of Experts (MoE) profile support. Refactor chat interface to utilize new compression logic and llm_profiles for improved synthesis. Maintain compatibility with existing methods and ensure robust error handling across services.
This commit is contained in:
1 parent
3d2f3d12d9
commit
d0eae8e43c
5 files changed
+299
-200
No files matched your search
+94
-90
@@ -1,16 +1,14 @@
|
||||
"""
|
||||
FILE: app/services/llm_service.py
|
||||
DESCRIPTION: Hybrid-Client für Ollama, Google GenAI (Gemini) und OpenRouter.
|
||||
Verwaltet provider-spezifische Prompts und Background-Last.
|
||||
WP-20: Optimiertes Fallback-Management zum Schutz von Cloud-Quoten.
|
||||
WP-22/JSON: Optionales JSON-Schema + strict (für OpenRouter).
|
||||
WP-25: Integration der DecisionEngine für Agentic Multi-Stream RAG.
|
||||
VERSION: 3.4.2 (WP-25: Ingest-Stability Patch)
|
||||
WP-25a: Implementierung der Mixture of Experts (MoE) Profil-Steuerung.
|
||||
VERSION: 3.5.0 (WP-25a: MoE & Profile Orchestration)
|
||||
STATUS: Active
|
||||
FIX:
|
||||
- Ingest-Stability: Entfernung des <5-Zeichen Guards (ermöglicht YES/NO Validierungen).
|
||||
- OpenRouter-Fix: Sicherung gegen leere 'choices' zur Vermeidung von JSON-Errors.
|
||||
- Erhalt der vollständigen v3.3.9 Logik für Rate-Limits, Retries und Background-Tasks.
|
||||
- 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.
|
||||
"""
|
||||
import httpx
|
||||
import yaml
|
||||
@@ -19,28 +17,28 @@ import asyncio
|
||||
import json
|
||||
from google import genai
|
||||
from google.genai import types
|
||||
from openai import AsyncOpenAI # Für OpenRouter (OpenAI-kompatibel)
|
||||
from openai import AsyncOpenAI
|
||||
from pathlib import Path
|
||||
from typing import Optional, Dict, Any, Literal
|
||||
from app.config import get_settings
|
||||
|
||||
# ENTSCHEIDENDER FIX: Import der neutralen Bereinigungs-Logik (WP-14)
|
||||
# Import der neutralen Bereinigungs-Logik
|
||||
from app.core.registry import clean_llm_text
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class LLMService:
|
||||
# GLOBALER SEMAPHOR für Hintergrund-Last Steuerung (WP-06)
|
||||
_background_semaphore = None
|
||||
|
||||
def __init__(self):
|
||||
self.settings = get_settings()
|
||||
self.prompts = self._load_prompts()
|
||||
|
||||
# WP-25: Lazy Initialization der DecisionEngine zur Vermeidung von Circular Imports
|
||||
# WP-25a: Zentrale Experten-Profile laden
|
||||
self.profiles = self._load_llm_profiles()
|
||||
|
||||
self._decision_engine = None
|
||||
|
||||
# Initialisiere Semaphore einmalig auf Klassen-Ebene
|
||||
if LLMService._background_semaphore is None:
|
||||
limit = getattr(self.settings, "BACKGROUND_LIMIT", 2)
|
||||
logger.info(f"🚦 LLMService: Initializing Background Semaphore with limit: {limit}")
|
||||
@@ -52,10 +50,9 @@ class LLMService:
|
||||
timeout=httpx.Timeout(self.settings.LLM_TIMEOUT)
|
||||
)
|
||||
|
||||
# 2. Google GenAI Client (Modern SDK)
|
||||
# 2. Google GenAI Client
|
||||
self.google_client = None
|
||||
if self.settings.GOOGLE_API_KEY:
|
||||
# FIX: Wir erzwingen api_version 'v1' für höhere Stabilität bei 2.5er Modellen.
|
||||
self.google_client = genai.Client(
|
||||
api_key=self.settings.GOOGLE_API_KEY,
|
||||
http_options={'api_version': 'v1'}
|
||||
@@ -68,24 +65,20 @@ class LLMService:
|
||||
self.openrouter_client = AsyncOpenAI(
|
||||
base_url="https://openrouter.ai/api/v1",
|
||||
api_key=self.settings.OPENROUTER_API_KEY,
|
||||
# Strikter Timeout für OpenRouter Free-Tier zur Vermeidung von Hangs.
|
||||
timeout=45.0
|
||||
)
|
||||
logger.info("🛰️ LLMService: OpenRouter Integration active.")
|
||||
|
||||
@property
|
||||
def decision_engine(self):
|
||||
"""Lazy Initialization der Decision Engine (WP-25)."""
|
||||
if self._decision_engine is None:
|
||||
from app.core.retrieval.decision_engine import DecisionEngine
|
||||
self._decision_engine = DecisionEngine()
|
||||
return self._decision_engine
|
||||
|
||||
def _load_prompts(self) -> dict:
|
||||
"""Lädt die Prompt-Konfiguration aus der YAML-Datei."""
|
||||
path = Path(self.settings.PROMPTS_PATH)
|
||||
if not path.exists():
|
||||
logger.error(f"❌ Prompts file not found at {path}")
|
||||
return {}
|
||||
try:
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
@@ -94,21 +87,28 @@ class LLMService:
|
||||
logger.error(f"❌ Failed to load prompts: {e}")
|
||||
return {}
|
||||
|
||||
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():
|
||||
logger.warning(f"⚠️ LLM Profiles file not found at {path}. System will use .env defaults.")
|
||||
return {}
|
||||
try:
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
data = yaml.safe_load(f) or {}
|
||||
return data.get("profiles", {})
|
||||
except Exception as e:
|
||||
logger.error(f"❌ Failed to load llm_profiles.yaml: {e}")
|
||||
return {}
|
||||
|
||||
def get_prompt(self, key: str, provider: str = None) -> str:
|
||||
"""
|
||||
Hole provider-spezifisches Template mit intelligenter Text-Kaskade.
|
||||
Kaskade: Gewählter Provider -> Gemini -> Ollama.
|
||||
"""
|
||||
active_provider = provider or self.settings.MINDNET_LLM_PROVIDER
|
||||
data = self.prompts.get(key, "")
|
||||
|
||||
if isinstance(data, dict):
|
||||
val = data.get(active_provider, data.get("gemini", data.get("ollama", "")))
|
||||
if isinstance(val, dict):
|
||||
logger.warning(f"⚠️ [LLMService] Nested dictionary detected for key '{key}'. Using first entry.")
|
||||
val = next(iter(val.values()), "") if val else ""
|
||||
return str(val)
|
||||
|
||||
return str(data)
|
||||
|
||||
async def generate_raw_response(
|
||||
@@ -123,34 +123,48 @@ class LLMService:
|
||||
model_override: Optional[str] = None,
|
||||
json_schema: Optional[Dict[str, Any]] = None,
|
||||
json_schema_name: str = "mindnet_json",
|
||||
strict_json_schema: bool = True
|
||||
strict_json_schema: bool = True,
|
||||
profile_name: Optional[str] = None # WP-25a
|
||||
) -> str:
|
||||
"""
|
||||
Haupteinstiegspunkt für LLM-Anfragen.
|
||||
WP-25 FIX: Schwellenwert entfernt, um kurze Ingest-Validierungen (YES/NO) zu unterstützen.
|
||||
Haupteinstiegspunkt für LLM-Anfragen mit Profil-Unterstützung.
|
||||
"""
|
||||
target_provider = provider or self.settings.MINDNET_LLM_PROVIDER
|
||||
target_provider = provider
|
||||
target_model = model_override
|
||||
target_temp = None
|
||||
|
||||
# WP-25a: Profil-Auflösung (Provider, Modell, Temperatur)
|
||||
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")
|
||||
logger.debug(f"🎭 MoE Call: Profil '{profile_name}' -> {target_provider}")
|
||||
|
||||
# Fallback auf Standard-Provider falls nichts übergeben/definiert wurde
|
||||
if not target_provider:
|
||||
target_provider = self.settings.MINDNET_LLM_PROVIDER
|
||||
|
||||
if priority == "background":
|
||||
async with LLMService._background_semaphore:
|
||||
res = await self._dispatch(
|
||||
target_provider, prompt, system, force_json,
|
||||
max_retries, base_delay, model_override,
|
||||
json_schema, json_schema_name, strict_json_schema
|
||||
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, model_override,
|
||||
json_schema, json_schema_name, strict_json_schema
|
||||
max_retries, base_delay, target_model,
|
||||
json_schema, json_schema_name, strict_json_schema, target_temp
|
||||
)
|
||||
|
||||
# WP-25 FIX: Nur noch auf absolut leere Antwort prüfen (ermöglicht YES/NO Antworten).
|
||||
# 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}. Falling back to OLLAMA.")
|
||||
res = await self._execute_ollama(prompt, system, force_json, max_retries, base_delay)
|
||||
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)
|
||||
|
||||
# WP-14 Fix: Bereinige Text-Antworten vor Rückgabe
|
||||
return clean_llm_text(res) if not force_json else res
|
||||
|
||||
async def _dispatch(
|
||||
@@ -164,9 +178,10 @@ class LLMService:
|
||||
model_override: Optional[str],
|
||||
json_schema: Optional[Dict[str, Any]],
|
||||
json_schema_name: str,
|
||||
strict_json_schema: bool
|
||||
strict_json_schema: bool,
|
||||
temperature: Optional[float] = None # WP-25a
|
||||
) -> str:
|
||||
"""Routet die Anfrage mit intelligenter Rate-Limit Erkennung."""
|
||||
"""Routet die Anfrage mit Rate-Limit Erkennung."""
|
||||
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)
|
||||
@@ -175,43 +190,42 @@ class LLMService:
|
||||
try:
|
||||
if provider == "openrouter" and self.openrouter_client:
|
||||
return await self._execute_openrouter(
|
||||
prompt=prompt,
|
||||
system=system,
|
||||
force_json=force_json,
|
||||
model_override=model_override,
|
||||
json_schema=json_schema,
|
||||
json_schema_name=json_schema_name,
|
||||
strict_json_schema=strict_json_schema
|
||||
prompt=prompt, system=system, force_json=force_json,
|
||||
model_override=model_override, json_schema=json_schema,
|
||||
json_schema_name=json_schema_name, strict_json_schema=strict_json_schema,
|
||||
temperature=temperature
|
||||
)
|
||||
|
||||
if provider == "gemini" and self.google_client:
|
||||
return await self._execute_google(prompt, system, force_json, model_override)
|
||||
return await self._execute_google(prompt, system, force_json, model_override, temperature)
|
||||
|
||||
return await self._execute_ollama(prompt, system, force_json, max_retries, base_delay)
|
||||
return await self._execute_ollama(prompt, system, force_json, max_retries, base_delay, temperature)
|
||||
|
||||
except Exception as e:
|
||||
err_str = str(e)
|
||||
is_rate_limit = any(x in err_str for x in ["429", "RESOURCE_EXHAUSTED", "rate_limited", "Too Many Requests"])
|
||||
|
||||
if is_rate_limit and rate_limit_attempts < max_rate_retries:
|
||||
if any(x in err_str for x in ["429", "RESOURCE_EXHAUSTED", "rate_limited"]):
|
||||
rate_limit_attempts += 1
|
||||
logger.warning(f"⏳ Rate Limit from {provider}. Attempt {rate_limit_attempts}. Waiting {wait_time}s...")
|
||||
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":
|
||||
logger.warning(f"🔄 Provider {provider} failed ({err_str}). Falling back to OLLAMA.")
|
||||
return await self._execute_ollama(prompt, system, force_json, max_retries, base_delay)
|
||||
return await self._execute_ollama(prompt, system, force_json, max_retries, base_delay, temperature)
|
||||
raise e
|
||||
|
||||
async def _execute_google(self, prompt, system, force_json, model_override):
|
||||
async def _execute_google(self, prompt, system, force_json, model_override, temperature):
|
||||
model = model_override or self.settings.GEMINI_MODEL
|
||||
clean_model = model.replace("models/", "")
|
||||
|
||||
config = types.GenerateContentConfig(
|
||||
system_instruction=system,
|
||||
response_mime_type="application/json" if force_json else "text/plain"
|
||||
)
|
||||
config_kwargs = {
|
||||
"system_instruction": system,
|
||||
"response_mime_type": "application/json" if force_json else "text/plain"
|
||||
}
|
||||
if temperature is not None:
|
||||
config_kwargs["temperature"] = temperature
|
||||
|
||||
config = types.GenerateContentConfig(**config_kwargs)
|
||||
|
||||
response = await asyncio.wait_for(
|
||||
asyncio.to_thread(
|
||||
self.google_client.models.generate_content,
|
||||
@@ -222,53 +236,47 @@ class LLMService:
|
||||
return response.text.strip()
|
||||
|
||||
async def _execute_openrouter(
|
||||
self,
|
||||
prompt: str,
|
||||
system: Optional[str],
|
||||
force_json: bool,
|
||||
model_override: Optional[str],
|
||||
json_schema: Optional[Dict[str, Any]] = None,
|
||||
json_schema_name: str = "mindnet_json",
|
||||
strict_json_schema: bool = True
|
||||
self, prompt: str, system: Optional[str], force_json: bool,
|
||||
model_override: Optional[str], json_schema: Optional[Dict[str, Any]] = None,
|
||||
json_schema_name: str = "mindnet_json", strict_json_schema: bool = True,
|
||||
temperature: Optional[float] = None
|
||||
) -> str:
|
||||
"""OpenRouter API Integration. WP-25 FIX: Sicherung gegen leere 'choices'."""
|
||||
model = model_override or self.settings.OPENROUTER_MODEL
|
||||
messages = []
|
||||
if system:
|
||||
messages.append({"role": "system", "content": system})
|
||||
if system: messages.append({"role": "system", "content": system})
|
||||
messages.append({"role": "user", "content": prompt})
|
||||
|
||||
kwargs: Dict[str, Any] = {}
|
||||
if temperature is not None:
|
||||
kwargs["temperature"] = temperature
|
||||
|
||||
if force_json:
|
||||
if json_schema:
|
||||
kwargs["response_format"] = {
|
||||
"type": "json_schema",
|
||||
"json_schema": {
|
||||
"name": json_schema_name, "strict": strict_json_schema, "schema": json_schema
|
||||
}
|
||||
"json_schema": {"name": json_schema_name, "strict": strict_json_schema, "schema": json_schema}
|
||||
}
|
||||
else:
|
||||
kwargs["response_format"] = {"type": "json_object"}
|
||||
|
||||
response = await self.openrouter_client.chat.completions.create(
|
||||
model=model,
|
||||
messages=messages,
|
||||
**kwargs
|
||||
model=model, messages=messages, **kwargs
|
||||
)
|
||||
|
||||
# WP-25 FIX: Sicherung gegen leere Antwort-Arrays
|
||||
if not response.choices or len(response.choices) == 0:
|
||||
logger.warning(f"🛰️ OpenRouter returned no choices for model {model}")
|
||||
if not response.choices:
|
||||
return ""
|
||||
|
||||
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):
|
||||
async def _execute_ollama(self, prompt, system, force_json, max_retries, base_delay, temperature=None):
|
||||
# WP-25a: Nutzt Profil-Temperatur oder Standard
|
||||
effective_temp = temperature if temperature is not None else (0.1 if force_json else 0.7)
|
||||
|
||||
payload = {
|
||||
"model": self.settings.LLM_MODEL,
|
||||
"prompt": prompt,
|
||||
"stream": False,
|
||||
"options": {"temperature": 0.1 if force_json else 0.7, "num_ctx": 8192}
|
||||
"options": {"temperature": effective_temp, "num_ctx": 8192}
|
||||
}
|
||||
if force_json: payload["format"] = "json"
|
||||
if system: payload["system"] = system
|
||||
@@ -281,15 +289,11 @@ class LLMService:
|
||||
return res.json().get("response", "").strip()
|
||||
except Exception as e:
|
||||
attempt += 1
|
||||
if attempt > max_retries:
|
||||
logger.error(f"❌ Ollama request failed: {e}")
|
||||
raise e
|
||||
wait_time = base_delay * (2 ** (attempt - 1))
|
||||
await asyncio.sleep(wait_time)
|
||||
if attempt > max_retries: raise e
|
||||
await asyncio.sleep(base_delay * (2 ** (attempt - 1)))
|
||||
|
||||
async def generate_rag_response(self, query: str, context_str: Optional[str] = None) -> str:
|
||||
"""WP-25: Orchestrierung via DecisionEngine."""
|
||||
logger.info(f"🚀 [WP-25] Chat Query: {query[:50]}...")
|
||||
return await self.decision_engine.ask(query)
|
||||
|
||||
async def close(self):
|
||||
|
||||
Reference in new issue
Block a user