korrektur zu besseren Analyse
This commit is contained in:
1 parent
972cd0dfac
commit
bd44af2b68
2 files changed
+23
-36
No files matched your search
+8
-25
@@ -1,5 +1,6 @@
|
||||
"""
|
||||
app/routers/chat.py — RAG Endpunkt (WP-06 Hybrid Router)
|
||||
Version: 0.2.1 (Fix: System Prompt Separation)
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
@@ -23,7 +24,6 @@ logger = logging.getLogger(__name__)
|
||||
_DECISION_CONFIG_CACHE = None
|
||||
|
||||
def _load_decision_config() -> Dict[str, Any]:
|
||||
"""Lädt die Decision-Engine Konfiguration (Late Binding)."""
|
||||
settings = get_settings()
|
||||
path = Path(settings.DECISION_CONFIG_PATH)
|
||||
default_config = {
|
||||
@@ -88,11 +88,6 @@ def _build_enriched_context(hits: List[QueryHit]) -> str:
|
||||
return "\n\n".join(context_parts)
|
||||
|
||||
async def _classify_intent(query: str, llm: LLMService) -> str:
|
||||
"""
|
||||
Hybrid Router:
|
||||
1. Keyword Check (Best/Longest Match) -> FAST
|
||||
2. LLM Fallback (wenn in config aktiv) -> SMART
|
||||
"""
|
||||
config = get_full_config()
|
||||
strategies = config.get("strategies", {})
|
||||
settings = config.get("settings", {})
|
||||
@@ -101,7 +96,7 @@ async def _classify_intent(query: str, llm: LLMService) -> str:
|
||||
best_intent = None
|
||||
max_match_length = 0
|
||||
|
||||
# 1. FAST PATH: Keywords
|
||||
# 1. FAST PATH
|
||||
for intent_name, strategy in strategies.items():
|
||||
if intent_name == "FACT": continue
|
||||
keywords = strategy.get("trigger_keywords", [])
|
||||
@@ -115,23 +110,21 @@ async def _classify_intent(query: str, llm: LLMService) -> str:
|
||||
logger.info(f"Intent detected via KEYWORD: {best_intent}")
|
||||
return best_intent
|
||||
|
||||
# 2. SLOW PATH: LLM Router
|
||||
# 2. SLOW PATH
|
||||
if settings.get("llm_fallback_enabled", False):
|
||||
router_prompt_template = settings.get("llm_router_prompt", "")
|
||||
if router_prompt_template:
|
||||
prompt = router_prompt_template.replace("{query}", query)
|
||||
logger.info("Keywords failed. Asking LLM for Intent...")
|
||||
|
||||
# Kurzer Raw Call
|
||||
# Router braucht keinen System-Prompt, nur den Classifier-Prompt
|
||||
llm_decision = await llm.generate_raw_response(prompt)
|
||||
|
||||
# Cleaning
|
||||
llm_decision = llm_decision.strip().upper()
|
||||
if ":" in llm_decision:
|
||||
llm_decision = llm_decision.split(":")[-1].strip()
|
||||
|
||||
# Validierung: Nur bekannte Intents zulassen
|
||||
# Entferne Satzzeichen
|
||||
# Satzzeichen entfernen für sauberen Match
|
||||
llm_decision = ''.join(filter(str.isalnum, llm_decision))
|
||||
|
||||
if llm_decision in strategies:
|
||||
@@ -185,7 +178,6 @@ async def chat_endpoint(
|
||||
)
|
||||
strategy_result = await retriever.search(strategy_req)
|
||||
|
||||
# Merge
|
||||
existing_ids = {h.node_id for h in hits}
|
||||
for strat_hit in strategy_result.results:
|
||||
if strat_hit.node_id not in existing_ids:
|
||||
@@ -198,27 +190,18 @@ async def chat_endpoint(
|
||||
context_str = _build_enriched_context(hits)
|
||||
|
||||
# 5. Generation
|
||||
# Wir laden das Template aus dem Service (da dort die prompts.yaml geladen ist)
|
||||
template = llm.prompts.get(prompt_key, "{context_str}\n\n{query}")
|
||||
system_prompt = llm.prompts.get("system_prompt", "")
|
||||
|
||||
if prepend_instr:
|
||||
context_str = f"{prepend_instr}\n\n{context_str}"
|
||||
|
||||
# Manuelles Bauen des finalen Prompts für volle Kontrolle
|
||||
final_prompt = template.replace("{context_str}", context_str).replace("{query}", request.message)
|
||||
|
||||
# Aufruf via Raw Response (da wir den Prompt schon fertig haben)
|
||||
# Wir müssen den System-Prompt manuell mitgeben?
|
||||
# generate_raw_response in llm_service unterstützt aktuell kein 'system'.
|
||||
# -> Wir erweitern generate_raw_response oder nutzen einen Hack: System + Prompt.
|
||||
|
||||
# SAUBERER WEG: Wir bauen den Payload für Ollama hier manuell zusammen und rufen eine generische Methode.
|
||||
# Da LLMService.generate_raw_response keine System-Msg nimmt, packen wir sie davor.
|
||||
full_text_prompt = f"{system_prompt}\n\n{final_prompt}"
|
||||
|
||||
logger.info(f"[{query_id}] Sending to LLM (Intent: {intent}, Template: {prompt_key})...")
|
||||
answer_text = await llm.generate_raw_response(full_text_prompt)
|
||||
|
||||
# FIX: System-Prompt separat übergeben!
|
||||
answer_text = await llm.generate_raw_response(prompt=final_prompt, system=system_prompt)
|
||||
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
|
||||
|
||||
Reference in new issue
Block a user