Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d1a065fec8 | ||
|
|
f686ecf947 | ||
|
|
6ac1f318d0 | ||
|
|
cbaf664123 | ||
|
|
470e653da6 | ||
|
|
43d3d8f7f3 | ||
|
|
83c0c9944d |
No files matched your search
@@ -34,6 +34,8 @@ class Settings:
|
|||||||
# --- WP-20 Hybrid LLM Provider ---
|
# --- WP-20 Hybrid LLM Provider ---
|
||||||
# Erlaubt: "ollama" | "gemini" | "openrouter"
|
# Erlaubt: "ollama" | "gemini" | "openrouter"
|
||||||
MINDNET_LLM_PROVIDER: str = os.getenv("MINDNET_LLM_PROVIDER", "openrouter").lower()
|
MINDNET_LLM_PROVIDER: str = os.getenv("MINDNET_LLM_PROVIDER", "openrouter").lower()
|
||||||
|
# Standardwert 10000, falls nichts in der .env steht
|
||||||
|
MAX_OLLAMA_CHARS: int = int(os.getenv("MAX_OLLAMA_CHARS", 10000))
|
||||||
|
|
||||||
# Google AI Studio (2025er Lite-Modell für höhere Kapazität)
|
# Google AI Studio (2025er Lite-Modell für höhere Kapazität)
|
||||||
GOOGLE_API_KEY: str | None = os.getenv("GOOGLE_API_KEY")
|
GOOGLE_API_KEY: str | None = os.getenv("GOOGLE_API_KEY")
|
||||||
|
|||||||
+69
-36
@@ -1,11 +1,12 @@
|
|||||||
"""
|
"""
|
||||||
FILE: app/routers/chat.py
|
FILE: app/routers/chat.py
|
||||||
DESCRIPTION: Haupt-Chat-Interface (RAG & Interview). Enthält Intent-Router (Keywords/LLM) und Prompt-Construction.
|
DESCRIPTION: Haupt-Chat-Interface (RAG & Interview). Enthält Intent-Router (Keywords/LLM) und Prompt-Construction.
|
||||||
VERSION: 2.7.1 (WP-22 Semantic Graph Routing)
|
VERSION: 2.7.8 (Full Unabridged Stability Edition)
|
||||||
STATUS: Active
|
STATUS: Active
|
||||||
FIX: Umstellung auf llm.get_prompt() zur Behebung des 500 Server Errors (Dictionary replace crash).
|
FIX:
|
||||||
DEPENDENCIES: app.config, app.models.dto, app.services.llm_service, app.core.retriever, app.services.feedback_service
|
1. Implementiert Context-Throttling für Ollama (MAX_OLLAMA_CHARS).
|
||||||
EXTERNAL_CONFIG: config/decision_engine.yaml, config/types.yaml
|
2. Deaktiviert LLM-Retries für den Chat (max_retries=0).
|
||||||
|
3. Behebt Double-Fallback-Schleifen und Silent Refusals.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from fastapi import APIRouter, HTTPException, Depends
|
from fastapi import APIRouter, HTTPException, Depends
|
||||||
@@ -36,7 +37,7 @@ def _load_decision_config() -> Dict[str, Any]:
|
|||||||
path = Path(settings.DECISION_CONFIG_PATH)
|
path = Path(settings.DECISION_CONFIG_PATH)
|
||||||
default_config = {
|
default_config = {
|
||||||
"strategies": {
|
"strategies": {
|
||||||
"FACT": {"trigger_keywords": []}
|
"FACT": {"trigger_keywords": [], "preferred_provider": "openrouter"}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,7 +160,7 @@ def _is_question(query: str) -> bool:
|
|||||||
q = query.strip().lower()
|
q = query.strip().lower()
|
||||||
if "?" in q: return True
|
if "?" in q: return True
|
||||||
|
|
||||||
# W-Fragen Indikatoren (falls User das ? vergisst)
|
# W-Fragen Indikatoren
|
||||||
starters = ["wer", "wie", "was", "wo", "wann", "warum", "weshalb", "wozu", "welche", "bist du", "entspricht"]
|
starters = ["wer", "wie", "was", "wo", "wann", "warum", "weshalb", "wozu", "welche", "bist du", "entspricht"]
|
||||||
if any(q.startswith(s + " ") for s in starters):
|
if any(q.startswith(s + " ") for s in starters):
|
||||||
return True
|
return True
|
||||||
@@ -170,7 +171,7 @@ async def _classify_intent(query: str, llm: LLMService) -> tuple[str, str]:
|
|||||||
"""
|
"""
|
||||||
Hybrid Router v5:
|
Hybrid Router v5:
|
||||||
1. Decision Keywords (Strategie) -> Prio 1
|
1. Decision Keywords (Strategie) -> Prio 1
|
||||||
2. Type Keywords (Interview Trigger) -> Prio 2, ABER NUR WENN KEINE FRAGE!
|
2. Type Keywords (Interview Trigger) -> Prio 2
|
||||||
3. LLM (Fallback) -> Prio 3
|
3. LLM (Fallback) -> Prio 3
|
||||||
"""
|
"""
|
||||||
config = get_full_config()
|
config = get_full_config()
|
||||||
@@ -179,7 +180,7 @@ async def _classify_intent(query: str, llm: LLMService) -> tuple[str, str]:
|
|||||||
|
|
||||||
query_lower = query.lower()
|
query_lower = query.lower()
|
||||||
|
|
||||||
# 1. FAST PATH A: Strategie Keywords (z.B. "Soll ich...")
|
# 1. FAST PATH A: Strategie Keywords
|
||||||
for intent_name, strategy in strategies.items():
|
for intent_name, strategy in strategies.items():
|
||||||
if intent_name == "FACT": continue
|
if intent_name == "FACT": continue
|
||||||
keywords = strategy.get("trigger_keywords", [])
|
keywords = strategy.get("trigger_keywords", [])
|
||||||
@@ -187,7 +188,7 @@ async def _classify_intent(query: str, llm: LLMService) -> tuple[str, str]:
|
|||||||
if k.lower() in query_lower:
|
if k.lower() in query_lower:
|
||||||
return intent_name, "Keyword (Strategy)"
|
return intent_name, "Keyword (Strategy)"
|
||||||
|
|
||||||
# 2. FAST PATH B: Type Keywords (z.B. "Projekt", "Werte") -> INTERVIEW
|
# 2. FAST PATH B: Type Keywords -> INTERVIEW
|
||||||
if not _is_question(query_lower):
|
if not _is_question(query_lower):
|
||||||
types_cfg = get_types_config()
|
types_cfg = get_types_config()
|
||||||
types_def = types_cfg.get("types", {})
|
types_def = types_cfg.get("types", {})
|
||||||
@@ -200,15 +201,15 @@ async def _classify_intent(query: str, llm: LLMService) -> tuple[str, str]:
|
|||||||
|
|
||||||
# 3. SLOW PATH: LLM Router
|
# 3. SLOW PATH: LLM Router
|
||||||
if settings.get("llm_fallback_enabled", False):
|
if settings.get("llm_fallback_enabled", False):
|
||||||
# FIX: Nutze get_prompt statt direktem Zugriff auf dict
|
router_prompt_template = llm.get_prompt("llm_router_prompt")
|
||||||
router_prompt_template = llm.get_prompt("router_prompt")
|
|
||||||
|
|
||||||
if router_prompt_template:
|
if router_prompt_template:
|
||||||
prompt = router_prompt_template.replace("{query}", query)
|
prompt = router_prompt_template.replace("{query}", query)
|
||||||
logger.info("Keywords failed (or Question detected). Asking LLM for Intent...")
|
logger.info("Keywords failed (or Question detected). Asking LLM for Intent...")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
raw_response = await llm.generate_raw_response(prompt, priority="realtime")
|
# FIX: Auch beim Routing keine Retries im Chat-Fluss
|
||||||
|
raw_response = await llm.generate_raw_response(prompt, priority="realtime", max_retries=0)
|
||||||
llm_output_upper = raw_response.upper()
|
llm_output_upper = raw_response.upper()
|
||||||
|
|
||||||
if "INTERVIEW" in llm_output_upper or "CREATE" in llm_output_upper:
|
if "INTERVIEW" in llm_output_upper or "CREATE" in llm_output_upper:
|
||||||
@@ -241,9 +242,11 @@ async def chat_endpoint(
|
|||||||
# Strategy Load
|
# Strategy Load
|
||||||
strategy = get_decision_strategy(intent)
|
strategy = get_decision_strategy(intent)
|
||||||
prompt_key = strategy.get("prompt_template", "rag_template")
|
prompt_key = strategy.get("prompt_template", "rag_template")
|
||||||
|
preferred_provider = strategy.get("preferred_provider")
|
||||||
|
|
||||||
sources_hits = []
|
sources_hits = []
|
||||||
final_prompt = ""
|
final_prompt = ""
|
||||||
|
context_str = ""
|
||||||
|
|
||||||
if intent == "INTERVIEW":
|
if intent == "INTERVIEW":
|
||||||
# --- INTERVIEW MODE ---
|
# --- INTERVIEW MODE ---
|
||||||
@@ -264,7 +267,6 @@ async def chat_endpoint(
|
|||||||
logger.info(f"[{query_id}] Interview Type: {target_type}. Fields: {len(fields_list)}")
|
logger.info(f"[{query_id}] Interview Type: {target_type}. Fields: {len(fields_list)}")
|
||||||
fields_str = "\n- " + "\n- ".join(fields_list)
|
fields_str = "\n- " + "\n- ".join(fields_list)
|
||||||
|
|
||||||
# FIX: Nutze get_prompt() zur Auflösung der provider-spezifischen Templates
|
|
||||||
template = llm.get_prompt(prompt_key)
|
template = llm.get_prompt(prompt_key)
|
||||||
final_prompt = template.replace("{context_str}", "Dialogverlauf...") \
|
final_prompt = template.replace("{context_str}", "Dialogverlauf...") \
|
||||||
.replace("{query}", request.message) \
|
.replace("{query}", request.message) \
|
||||||
@@ -274,14 +276,10 @@ async def chat_endpoint(
|
|||||||
sources_hits = []
|
sources_hits = []
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# --- RAG MODE ---
|
# --- RAG MODE (FACT, DECISION, EMPATHY, CODING) ---
|
||||||
inject_types = strategy.get("inject_types", [])
|
inject_types = strategy.get("inject_types", [])
|
||||||
prepend_instr = strategy.get("prepend_instruction", "")
|
prepend_instr = strategy.get("prepend_instruction", "")
|
||||||
|
|
||||||
# --- WP-22: Semantic Graph Routing (Teil C) ---
|
|
||||||
edge_boosts = strategy.get("edge_boosts", {})
|
edge_boosts = strategy.get("edge_boosts", {})
|
||||||
if edge_boosts:
|
|
||||||
logger.info(f"[{query_id}] Applying Edge Boosts: {edge_boosts}")
|
|
||||||
|
|
||||||
query_req = QueryRequest(
|
query_req = QueryRequest(
|
||||||
query=request.message,
|
query=request.message,
|
||||||
@@ -308,32 +306,66 @@ async def chat_endpoint(
|
|||||||
if strat_hit.node_id not in existing_ids:
|
if strat_hit.node_id not in existing_ids:
|
||||||
hits.append(strat_hit)
|
hits.append(strat_hit)
|
||||||
|
|
||||||
if not hits:
|
context_str = _build_enriched_context(hits) if hits else "Keine relevanten Notizen gefunden."
|
||||||
context_str = "Keine relevanten Notizen gefunden."
|
|
||||||
else:
|
|
||||||
context_str = _build_enriched_context(hits)
|
|
||||||
|
|
||||||
# FIX: Nutze get_prompt() zur Auflösung der provider-spezifischen Templates
|
|
||||||
template = llm.get_prompt(prompt_key)
|
|
||||||
|
|
||||||
if not template:
|
# --- STABILITY FIX: OLLAMA CONTEXT THROTTLE ---
|
||||||
template = "{context_str}\n\n{query}"
|
# Begrenzt den Text, um den "decode: cannot decode batches" Fehler zu vermeiden.
|
||||||
|
# MAX_OLLAMA_CHARS = 10000
|
||||||
|
|
||||||
|
settings = get_settings() # Falls noch nicht im Scope vorhanden
|
||||||
|
max_chars = getattr(settings, "MAX_OLLAMA_CHARS", 10000)
|
||||||
|
if preferred_provider == "ollama" and len(context_str) > max_chars:
|
||||||
|
logger.warning(f"⚠️ [{query_id}] Context zu groß für Ollama ({len(context_str)} chars). Kürze auf {max_chars}.")
|
||||||
|
context_str = context_str[:max_chars] + "\n[...gekürzt zur Stabilität...]"
|
||||||
|
|
||||||
|
template = llm.get_prompt(prompt_key) or "{context_str}\n\n{query}"
|
||||||
|
|
||||||
if prepend_instr:
|
if prepend_instr:
|
||||||
context_str = f"{prepend_instr}\n\n{context_str}"
|
context_str = f"{prepend_instr}\n\n{context_str}"
|
||||||
|
|
||||||
final_prompt = template.replace("{context_str}", context_str).replace("{query}", request.message)
|
final_prompt = template.replace("{context_str}", context_str).replace("{query}", request.message)
|
||||||
sources_hits = hits
|
sources_hits = hits
|
||||||
|
|
||||||
# --- GENERATION ---
|
# --- DEBUG SPOT 1: PROMPT CONSTRUCTION ---
|
||||||
|
logger.info(f"[{query_id}] PROMPT CONSTRUCTION COMPLETE. Length: {len(final_prompt)} chars.")
|
||||||
|
if not final_prompt.strip():
|
||||||
|
logger.error(f"[{query_id}] CRITICAL: Final prompt is empty before sending to LLM!")
|
||||||
|
|
||||||
|
# --- GENERATION WITH NO-RETRY & DEEP FALLBACK ---
|
||||||
system_prompt = llm.get_prompt("system_prompt")
|
system_prompt = llm.get_prompt("system_prompt")
|
||||||
|
|
||||||
# Chat nutzt IMMER realtime priority
|
# --- DEBUG SPOT 2: PRIMARY CALL ---
|
||||||
answer_text = await llm.generate_raw_response(
|
logger.info(f"[{query_id}] PRIMARY CALL: Sending request to provider '{preferred_provider}' (No Retries)...")
|
||||||
prompt=final_prompt,
|
|
||||||
system=system_prompt,
|
answer_text = ""
|
||||||
priority="realtime"
|
try:
|
||||||
)
|
# FIX: max_retries=0 verhindert Hänger durch Retry-Kaskaden im Chat
|
||||||
|
answer_text = await llm.generate_raw_response(
|
||||||
|
prompt=final_prompt,
|
||||||
|
system=system_prompt,
|
||||||
|
priority="realtime",
|
||||||
|
provider=preferred_provider,
|
||||||
|
max_retries=0
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"🛑 [{query_id}] Primary Provider '{preferred_provider}' failed: {e}")
|
||||||
|
|
||||||
|
# DEEP FALLBACK: Wenn die Antwort leer ist (Silent Refusal) oder der Primary abgestürzt ist
|
||||||
|
if not answer_text.strip() and preferred_provider != "ollama":
|
||||||
|
# --- DEBUG SPOT 3: FALLBACK TRIGGER ---
|
||||||
|
logger.warning(f"🛑 [{query_id}] PRIMARY '{preferred_provider}' returned EMPTY or FAILED. Triggering Deep Fallback to Ollama...")
|
||||||
|
|
||||||
|
try:
|
||||||
|
answer_text = await llm.generate_raw_response(
|
||||||
|
prompt=final_prompt,
|
||||||
|
system=system_prompt,
|
||||||
|
priority="realtime",
|
||||||
|
provider="ollama",
|
||||||
|
max_retries=0
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"🛑 [{query_id}] Deep Fallback to Ollama also failed: {e}")
|
||||||
|
answer_text = "Entschuldigung, das System ist aktuell überlastet. Bitte versuche es in einem Moment erneut."
|
||||||
|
|
||||||
duration_ms = int((time.time() - start_time) * 1000)
|
duration_ms = int((time.time() - start_time) * 1000)
|
||||||
|
|
||||||
@@ -344,7 +376,7 @@ async def chat_endpoint(
|
|||||||
query_text=request.message,
|
query_text=request.message,
|
||||||
results=sources_hits,
|
results=sources_hits,
|
||||||
mode="interview" if intent == "INTERVIEW" else "chat_rag",
|
mode="interview" if intent == "INTERVIEW" else "chat_rag",
|
||||||
metadata={"intent": intent, "source": intent_source}
|
metadata={"intent": intent, "source": intent_source, "provider": preferred_provider}
|
||||||
)
|
)
|
||||||
except: pass
|
except: pass
|
||||||
|
|
||||||
@@ -359,4 +391,5 @@ async def chat_endpoint(
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error in chat endpoint: {e}", exc_info=True)
|
logger.error(f"Error in chat endpoint: {e}", exc_info=True)
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
# Wir geben eine benutzerfreundliche Meldung zurück, statt nur den Error-Stack
|
||||||
|
raise HTTPException(status_code=500, detail="Das System konnte die Anfrage nicht verarbeiten.")
|
||||||
+20
-23
@@ -6,9 +6,12 @@ DESCRIPTION: Hybrid-Client für Ollama, Google GenAI (Gemini) und OpenRouter.
|
|||||||
WP-20 Fix: Bulletproof Prompt-Auflösung für format() Aufrufe.
|
WP-20 Fix: Bulletproof Prompt-Auflösung für format() Aufrufe.
|
||||||
WP-22/JSON: Optionales JSON-Schema + strict (für OpenRouter structured outputs).
|
WP-22/JSON: Optionales JSON-Schema + strict (für OpenRouter structured outputs).
|
||||||
FIX: Intelligente Rate-Limit Erkennung (429 Handling), v1-API Sync & Timeouts.
|
FIX: Intelligente Rate-Limit Erkennung (429 Handling), v1-API Sync & Timeouts.
|
||||||
VERSION: 3.3.6
|
VERSION: 3.3.7
|
||||||
STATUS: Active
|
STATUS: Active
|
||||||
DEPENDENCIES: httpx, yaml, logging, asyncio, json, google-genai, openai, app.config
|
FIX:
|
||||||
|
- Implementiert striktes max_retries Handling für alle Provider (v.a. für Chat-Stabilität).
|
||||||
|
- Synchronisiert Rate-Limit Retries mit dem max_retries Parameter.
|
||||||
|
- Optimiert Logging für sofortige Fehlererkennung.
|
||||||
"""
|
"""
|
||||||
import httpx
|
import httpx
|
||||||
import yaml
|
import yaml
|
||||||
@@ -84,8 +87,6 @@ class LLMService:
|
|||||||
Hole provider-spezifisches Template mit intelligenter Text-Kaskade.
|
Hole provider-spezifisches Template mit intelligenter Text-Kaskade.
|
||||||
HINWEIS: Dies ist nur ein Text-Lookup und verbraucht kein API-Kontingent.
|
HINWEIS: Dies ist nur ein Text-Lookup und verbraucht kein API-Kontingent.
|
||||||
Kaskade: Gewählter Provider -> Gemini (Cloud-Stil) -> Ollama (Basis-Stil).
|
Kaskade: Gewählter Provider -> Gemini (Cloud-Stil) -> Ollama (Basis-Stil).
|
||||||
|
|
||||||
WP-20 Fix: Garantiert die Rückgabe eines Strings, um AttributeError zu vermeiden.
|
|
||||||
"""
|
"""
|
||||||
active_provider = provider or self.settings.MINDNET_LLM_PROVIDER
|
active_provider = provider or self.settings.MINDNET_LLM_PROVIDER
|
||||||
data = self.prompts.get(key, "")
|
data = self.prompts.get(key, "")
|
||||||
@@ -118,11 +119,6 @@ class LLMService:
|
|||||||
) -> str:
|
) -> str:
|
||||||
"""
|
"""
|
||||||
Haupteinstiegspunkt für LLM-Anfragen mit Priorisierung.
|
Haupteinstiegspunkt für LLM-Anfragen mit Priorisierung.
|
||||||
|
|
||||||
force_json:
|
|
||||||
- Ollama: nutzt payload["format"]="json"
|
|
||||||
- Gemini: nutzt response_mime_type="application/json"
|
|
||||||
- OpenRouter: nutzt response_format=json_object (Fallback) oder json_schema
|
|
||||||
"""
|
"""
|
||||||
target_provider = provider or self.settings.MINDNET_LLM_PROVIDER
|
target_provider = provider or self.settings.MINDNET_LLM_PROVIDER
|
||||||
|
|
||||||
@@ -154,11 +150,12 @@ class LLMService:
|
|||||||
strict_json_schema: bool
|
strict_json_schema: bool
|
||||||
) -> str:
|
) -> str:
|
||||||
"""
|
"""
|
||||||
Routet die Anfrage mit intelligenter Rate-Limit Erkennung (WP-20 + WP-76).
|
Routet die Anfrage mit intelligenter Rate-Limit Erkennung.
|
||||||
Schleife läuft über MINDNET_LLM_RATE_LIMIT_RETRIES.
|
Nutzt max_retries um die Rate-Limit Schleife zu begrenzen.
|
||||||
"""
|
"""
|
||||||
rate_limit_attempts = 0
|
rate_limit_attempts = 0
|
||||||
max_rate_retries = getattr(self.settings, "LLM_RATE_LIMIT_RETRIES", 3)
|
# FIX: Wir nutzen max_retries als Limit für Rate-Limit Versuche, wenn explizit klein gewählt (z.B. Chat)
|
||||||
|
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)
|
wait_time = getattr(self.settings, "LLM_RATE_LIMIT_WAIT", 60.0)
|
||||||
|
|
||||||
while rate_limit_attempts <= max_rate_retries:
|
while rate_limit_attempts <= max_rate_retries:
|
||||||
@@ -182,18 +179,17 @@ class LLMService:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
err_str = str(e)
|
err_str = str(e)
|
||||||
# Intelligente 429 Erkennung für alle Cloud-Provider
|
# Intelligente 429 Erkennung
|
||||||
is_rate_limit = any(x in err_str for x in ["429", "RESOURCE_EXHAUSTED", "rate_limited", "Too Many Requests"])
|
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 is_rate_limit and rate_limit_attempts < max_rate_retries:
|
||||||
rate_limit_attempts += 1
|
rate_limit_attempts += 1
|
||||||
logger.warning(
|
logger.warning(
|
||||||
f"⏳ [LLMService] Rate Limit (429) detected from {provider}. "
|
f"⏳ [LLMService] Rate Limit detected from {provider}. "
|
||||||
f"Attempt {rate_limit_attempts}/{max_rate_retries}. "
|
f"Attempt {rate_limit_attempts}/{max_rate_retries}. Waiting {wait_time}s..."
|
||||||
f"Waiting {wait_time}s before cloud retry..."
|
|
||||||
)
|
)
|
||||||
await asyncio.sleep(wait_time)
|
await asyncio.sleep(wait_time)
|
||||||
continue # Nächster Versuch in der Cloud-Schleife
|
continue
|
||||||
|
|
||||||
# Wenn kein Rate-Limit oder Retries erschöpft -> Fallback zu Ollama (falls aktiviert)
|
# Wenn kein Rate-Limit oder Retries erschöpft -> Fallback zu Ollama (falls aktiviert)
|
||||||
if self.settings.LLM_FALLBACK_ENABLED and provider != "ollama":
|
if self.settings.LLM_FALLBACK_ENABLED and provider != "ollama":
|
||||||
@@ -206,14 +202,12 @@ class LLMService:
|
|||||||
async def _execute_google(self, prompt, system, force_json, model_override):
|
async def _execute_google(self, prompt, system, force_json, model_override):
|
||||||
"""Native Google SDK Integration (Gemini) mit v1 Fix."""
|
"""Native Google SDK Integration (Gemini) mit v1 Fix."""
|
||||||
model = model_override or self.settings.GEMINI_MODEL
|
model = model_override or self.settings.GEMINI_MODEL
|
||||||
# Fix: Bereinige Modellnamen (Entfernung von 'models/' Präfix)
|
|
||||||
clean_model = model.replace("models/", "")
|
clean_model = model.replace("models/", "")
|
||||||
|
|
||||||
config = types.GenerateContentConfig(
|
config = types.GenerateContentConfig(
|
||||||
system_instruction=system,
|
system_instruction=system,
|
||||||
response_mime_type="application/json" if force_json else "text/plain"
|
response_mime_type="application/json" if force_json else "text/plain"
|
||||||
)
|
)
|
||||||
# Thread-Offloading mit striktem Timeout gegen "Hangs"
|
|
||||||
response = await asyncio.wait_for(
|
response = await asyncio.wait_for(
|
||||||
asyncio.to_thread(
|
asyncio.to_thread(
|
||||||
self.google_client.models.generate_content,
|
self.google_client.models.generate_content,
|
||||||
@@ -233,7 +227,7 @@ class LLMService:
|
|||||||
json_schema_name: str = "mindnet_json",
|
json_schema_name: str = "mindnet_json",
|
||||||
strict_json_schema: bool = True
|
strict_json_schema: bool = True
|
||||||
) -> str:
|
) -> str:
|
||||||
"""OpenRouter API Integration (OpenAI-kompatibel) mit Schema-Support."""
|
"""OpenRouter API Integration (OpenAI-kompatibel)."""
|
||||||
model = model_override or self.settings.OPENROUTER_MODEL
|
model = model_override or self.settings.OPENROUTER_MODEL
|
||||||
messages = []
|
messages = []
|
||||||
if system:
|
if system:
|
||||||
@@ -262,14 +256,14 @@ class LLMService:
|
|||||||
return response.choices[0].message.content.strip()
|
return response.choices[0].message.content.strip()
|
||||||
|
|
||||||
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):
|
||||||
"""Lokaler Ollama Call mit exponentiellem Backoff."""
|
"""Lokaler Ollama Call mit striktem Retry-Limit."""
|
||||||
payload = {
|
payload = {
|
||||||
"model": self.settings.LLM_MODEL,
|
"model": self.settings.LLM_MODEL,
|
||||||
"prompt": prompt,
|
"prompt": prompt,
|
||||||
"stream": False,
|
"stream": False,
|
||||||
"options": {
|
"options": {
|
||||||
"temperature": 0.1 if force_json else 0.7,
|
"temperature": 0.1 if force_json else 0.7,
|
||||||
"num_ctx": 8192
|
"num_ctx": 8192 # Begrenzung für Stabilität (WP-20)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if force_json:
|
if force_json:
|
||||||
@@ -285,9 +279,11 @@ class LLMService:
|
|||||||
return res.json().get("response", "").strip()
|
return res.json().get("response", "").strip()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
attempt += 1
|
attempt += 1
|
||||||
|
# WICHTIG: Wenn max_retries=0 (Chat), bricht dies nach dem 1. Versuch (attempt=1) sofort ab.
|
||||||
if attempt > max_retries:
|
if attempt > max_retries:
|
||||||
logger.error(f"❌ Ollama Error after {attempt} retries: {e}")
|
logger.error(f"❌ Ollama request failed after {attempt} attempt(s): {e}")
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
wait_time = base_delay * (2 ** (attempt - 1))
|
wait_time = base_delay * (2 ** (attempt - 1))
|
||||||
logger.warning(f"⚠️ Ollama attempt {attempt} failed. Retrying in {wait_time}s...")
|
logger.warning(f"⚠️ Ollama attempt {attempt} failed. Retrying in {wait_time}s...")
|
||||||
await asyncio.sleep(wait_time)
|
await asyncio.sleep(wait_time)
|
||||||
@@ -300,6 +296,7 @@ class LLMService:
|
|||||||
|
|
||||||
final_prompt = rag_template.format(context_str=context_str, query=query)
|
final_prompt = rag_template.format(context_str=context_str, query=query)
|
||||||
|
|
||||||
|
# RAG Aufrufe im Chat nutzen nun standardmäßig max_retries=2 (überschreibbar)
|
||||||
return await self.generate_raw_response(
|
return await self.generate_raw_response(
|
||||||
final_prompt,
|
final_prompt,
|
||||||
system=system_prompt,
|
system=system_prompt,
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ strategies:
|
|||||||
# 3. Empathie / "Ich"-Modus (Lokal & Privat via Ollama)
|
# 3. Empathie / "Ich"-Modus (Lokal & Privat via Ollama)
|
||||||
EMPATHY:
|
EMPATHY:
|
||||||
description: "Reaktion auf emotionale Zustände."
|
description: "Reaktion auf emotionale Zustände."
|
||||||
preferred_provider: "ollama"
|
preferred_provider: "openrouter"
|
||||||
trigger_keywords:
|
trigger_keywords:
|
||||||
- "ich fühle"
|
- "ich fühle"
|
||||||
- "traurig"
|
- "traurig"
|
||||||
@@ -127,7 +127,7 @@ strategies:
|
|||||||
# 5. Interview / Datenerfassung (Lokal)
|
# 5. Interview / Datenerfassung (Lokal)
|
||||||
INTERVIEW:
|
INTERVIEW:
|
||||||
description: "Der User möchte Wissen erfassen."
|
description: "Der User möchte Wissen erfassen."
|
||||||
preferred_provider: "ollama"
|
preferred_provider: "openrouter"
|
||||||
trigger_keywords:
|
trigger_keywords:
|
||||||
- "neue notiz"
|
- "neue notiz"
|
||||||
- "etwas notieren"
|
- "etwas notieren"
|
||||||
|
|||||||
@@ -53,6 +53,20 @@ Wenn der Intent `FACT` oder `DECISION` ist, wird folgender Flow ausgeführt:
|
|||||||
|
|
||||||
Der `LLMService` (`app/services/llm_service.py`) fungiert als zentraler Hybrid-Client für OpenRouter, Google Gemini und Ollama. Er schützt das System vor Überlastung und verwaltet Quoten.
|
Der `LLMService` (`app/services/llm_service.py`) fungiert als zentraler Hybrid-Client für OpenRouter, Google Gemini und Ollama. Er schützt das System vor Überlastung und verwaltet Quoten.
|
||||||
|
|
||||||
|
Mit Version 2.8.1 wurde die Architektur der Antwort-Generierung grundlegend gehärtet:
|
||||||
|
|
||||||
|
A. Fail-Fast Prinzip (No-Retry Chat)
|
||||||
|
Im Gegensatz zur Ingestion-Pipeline nutzt das Chat-Backend für Echtzeit-Anfragen keine internen Retries (max_retries=0).
|
||||||
|
|
||||||
|
Scheitert ein Provider (Timeout/Fehler), wird sofort die Fallback-Kaskade eingeleitet oder der Deep Fallback zu Ollama getriggert.
|
||||||
|
|
||||||
|
Dies verhindert die kumulative Wartezeit von mehreren Minuten bei Provider-Störungen.
|
||||||
|
|
||||||
|
B. Context-Throttling & Memory Guard
|
||||||
|
Drosselung: Vor der Übergabe an Ollama prüft die chat.py, ob der Kontext (RAG-Hits) die Grenze von MAX_OLLAMA_CHARS überschreitet und kürzt diesen ggf..
|
||||||
|
|
||||||
|
Modell-Lock: Der LLMService erzwingt im Ollama-Payload den Parameter num_ctx: 8192. Dies stabilisiert den VRAM-Verbrauch und verhindert, dass das Modell versucht, speicherintensive 128k-Kontexte zu reservieren.
|
||||||
|
|
||||||
### 2.1 Prioritäts-Semaphor
|
### 2.1 Prioritäts-Semaphor
|
||||||
|
|
||||||
Jeder LLM-Request steuert über ein `priority`-Flag den Zugriff auf Hardware- und API-Ressourcen:
|
Jeder LLM-Request steuert über ein `priority`-Flag den Zugriff auf Hardware- und API-Ressourcen:
|
||||||
|
|||||||
@@ -29,8 +29,8 @@ Diese Variablen steuern die Infrastruktur, Pfade und globale Timeouts.
|
|||||||
| `MINDNET_PROMPTS_PATH` | `config/prompts.yaml` | Pfad zu LLM Prompts. |
|
| `MINDNET_PROMPTS_PATH` | `config/prompts.yaml` | Pfad zu LLM Prompts. |
|
||||||
| `MINDNET_LLM_PROVIDER` | `openrouter` | **Neu (WP-20):** Aktiver Provider (`openrouter`, `gemini`, `ollama`). |
|
| `MINDNET_LLM_PROVIDER` | `openrouter` | **Neu (WP-20):** Aktiver Provider (`openrouter`, `gemini`, `ollama`). |
|
||||||
| `MINDNET_LLM_FALLBACK` | `true` | **Neu (WP-20):** Aktiviert automatischen Ollama-Fallback bei Cloud-Fehlern. |
|
| `MINDNET_LLM_FALLBACK` | `true` | **Neu (WP-20):** Aktiviert automatischen Ollama-Fallback bei Cloud-Fehlern. |
|
||||||
| `MINDNET_LLM_RATE_LIMIT_WAIT`| `60.0` | **Neu (WP-76):** Wartezeit in Sekunden bei HTTP 429 (Rate Limit). |
|
| `MINDNET_LLM_RATE_LIMIT_WAIT`| `60.0` | **Neu (WP-20):** Wartezeit in Sekunden bei HTTP 429 (Rate Limit). |
|
||||||
| `MINDNET_LLM_RATE_LIMIT_RETRIES`| `3` | **Neu (WP-76):** Anzahl Cloud-Retries vor lokalem Fallback. |
|
| `MINDNET_LLM_RATE_LIMIT_RETRIES`| `3` | **Neu (WP-20):** Anzahl Cloud-Retries vor lokalem Fallback. |
|
||||||
| `GOOGLE_API_KEY` | *(Key)* | API Key für Google AI Studio. |
|
| `GOOGLE_API_KEY` | *(Key)* | API Key für Google AI Studio. |
|
||||||
| `MINDNET_GEMINI_MODEL` | `gemini-2.5-flash-lite` | **Update 2025:** Optimiertes Lite-Modell für hohe Quoten. |
|
| `MINDNET_GEMINI_MODEL` | `gemini-2.5-flash-lite` | **Update 2025:** Optimiertes Lite-Modell für hohe Quoten. |
|
||||||
| `OPENROUTER_API_KEY` | *(Key)* | API Key für OpenRouter Integration. |
|
| `OPENROUTER_API_KEY` | *(Key)* | API Key für OpenRouter Integration. |
|
||||||
@@ -38,6 +38,7 @@ Diese Variablen steuern die Infrastruktur, Pfade und globale Timeouts.
|
|||||||
| `MINDNET_LLM_MODEL` | `phi3:mini` | Name des lokalen Chat-Modells (Ollama). |
|
| `MINDNET_LLM_MODEL` | `phi3:mini` | Name des lokalen Chat-Modells (Ollama). |
|
||||||
| `MINDNET_EMBEDDING_MODEL` | `nomic-embed-text` | Name des Embedding-Modells (Ollama). |
|
| `MINDNET_EMBEDDING_MODEL` | `nomic-embed-text` | Name des Embedding-Modells (Ollama). |
|
||||||
| `MINDNET_OLLAMA_URL` | `http://127.0.0.1:11434`| URL zum lokalen LLM-Server. |
|
| `MINDNET_OLLAMA_URL` | `http://127.0.0.1:11434`| URL zum lokalen LLM-Server. |
|
||||||
|
| `MAX_OLLAMA_CHARS` | `10000`| Maximale Länge des Kontext-Strings, der an das lokale Modell gesendet wird. Verhindert Batch-Decoding-Fehler bei sehr großen Notiz-Historien. |
|
||||||
| `MINDNET_LLM_TIMEOUT` | `300.0` | Timeout in Sekunden für LLM-Anfragen. |
|
| `MINDNET_LLM_TIMEOUT` | `300.0` | Timeout in Sekunden für LLM-Anfragen. |
|
||||||
| `MINDNET_API_TIMEOUT` | `300.0` | Globales API-Timeout für das Frontend. |
|
| `MINDNET_API_TIMEOUT` | `300.0` | Globales API-Timeout für das Frontend. |
|
||||||
| `MINDNET_LL_BACKGROUND_LIMIT`| `2` | **Traffic Control:** Max. parallele Hintergrund-Tasks (Semaphore). |
|
| `MINDNET_LL_BACKGROUND_LIMIT`| `2` | **Traffic Control:** Max. parallele Hintergrund-Tasks (Semaphore). |
|
||||||
|
|||||||
@@ -19,14 +19,31 @@ context: "Installationsanleitung, Systemd-Units und Wartungsprozesse für Mindne
|
|||||||
### 1.2 Qdrant (Docker)
|
### 1.2 Qdrant (Docker)
|
||||||
Startet die Vektor-DB mit persistentem Storage auf Port 6333.
|
Startet die Vektor-DB mit persistentem Storage auf Port 6333.
|
||||||
|
|
||||||
|
dockercompose.yaml
|
||||||
```bash
|
```bash
|
||||||
docker run -d \
|
services:
|
||||||
--name mindnet_qdrant \
|
qdrant:
|
||||||
--restart always \
|
image: qdrant/qdrant
|
||||||
-p 6333:6333 \
|
container_name: qdrant
|
||||||
-v $(pwd)/qdrant_storage:/qdrant/storage \
|
ports:
|
||||||
qdrant/qdrant
|
- "6333:6333"
|
||||||
|
volumes:
|
||||||
|
- ./qdrant_data:/qdrant/storage
|
||||||
|
|
||||||
|
ulimits:
|
||||||
|
nofile:
|
||||||
|
soft: 65535
|
||||||
|
hard: 65535
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
```
|
```
|
||||||
|
```bash
|
||||||
|
docker compose up -d
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
Um Abstürze der Vektordatenbank bei einer hohen Anzahl an Collections (z. B. durch viele Notiz-Typen oder Dev-Umgebungen) zu vermeiden, müssen die System-Limits für den Container angepasst werden.
|
||||||
|
Hintergrund: Qdrant öffnet für jedes Segment einer Collection mehrere Dateien. Ohne diese Erhöhung führt das Standard-Linux-Limit (1024) zum Absturz mit dem Fehler os error 24 (Too many open files).
|
||||||
|
|
||||||
### 1.3 Ollama (Modelle)
|
### 1.3 Ollama (Modelle)
|
||||||
**Wichtig:** Seit v2.4 ist `nomic-embed-text` Pflicht für Embeddings.
|
**Wichtig:** Seit v2.4 ist `nomic-embed-text` Pflicht für Embeddings.
|
||||||
|
|||||||
Reference in new issue
Block a user