neue Test
This commit is contained in:
1 parent
d2270fafdd
commit
bd36d78025
3 files changed
+41
-41
No files matched your search
+6
-4
@@ -3,12 +3,12 @@ app/models/dto.py — Pydantic-Modelle (DTOs) für WP-04/WP-05/WP-06
|
||||
|
||||
Zweck:
|
||||
Laufzeit-Modelle für FastAPI (Requests/Responses).
|
||||
WP-06 Update: Intent in ChatResponse.
|
||||
WP-06 Update: Intent & Intent-Source in ChatResponse.
|
||||
|
||||
Version:
|
||||
0.6.0 (WP-06: Decision Engine)
|
||||
0.6.1 (WP-06: Decision Engine Transparency)
|
||||
Stand:
|
||||
2025-12-08
|
||||
2025-12-09
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -123,6 +123,7 @@ class QueryHit(BaseModel):
|
||||
total_score: float
|
||||
paths: Optional[List[List[Dict]]] = None
|
||||
source: Optional[Dict] = None
|
||||
payload: Optional[Dict] = None # Added for flexibility
|
||||
explanation: Optional[Explanation] = None
|
||||
|
||||
|
||||
@@ -150,4 +151,5 @@ class ChatResponse(BaseModel):
|
||||
answer: str = Field(..., description="Generierte Antwort vom LLM")
|
||||
sources: List[QueryHit] = Field(..., description="Die für die Antwort genutzten Quellen")
|
||||
latency_ms: int
|
||||
intent: Optional[str] = Field("FACT", description="WP-06: Erkannter Intent (FACT/DECISION)")
|
||||
intent: Optional[str] = Field("FACT", description="WP-06: Erkannter Intent (FACT/DECISION)")
|
||||
intent_source: Optional[str] = Field("Unknown", description="WP-06: Quelle der Intent-Erkennung (Keyword vs. LLM)")
|
||||
+16
-29
@@ -1,6 +1,6 @@
|
||||
"""
|
||||
app/routers/chat.py — RAG Endpunkt (WP-06 Hybrid Router v2)
|
||||
Update: Robusteres LLM-Parsing für Small Language Models (SLMs).
|
||||
app/routers/chat.py — RAG Endpunkt (WP-06 Hybrid Router v3)
|
||||
Update: Transparenz über Intent-Source (Keyword vs. LLM).
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
@@ -87,11 +87,10 @@ def _build_enriched_context(hits: List[QueryHit]) -> str:
|
||||
|
||||
return "\n\n".join(context_parts)
|
||||
|
||||
async def _classify_intent(query: str, llm: LLMService) -> str:
|
||||
async def _classify_intent(query: str, llm: LLMService) -> tuple[str, str]:
|
||||
"""
|
||||
Hybrid Router v2:
|
||||
1. Keyword Check (Best/Longest Match) -> FAST
|
||||
2. LLM Fallback (Robust Parsing) -> SMART
|
||||
Hybrid Router v3:
|
||||
Gibt Tuple zurück: (Intent, Source)
|
||||
"""
|
||||
config = get_full_config()
|
||||
strategies = config.get("strategies", {})
|
||||
@@ -112,8 +111,7 @@ async def _classify_intent(query: str, llm: LLMService) -> str:
|
||||
best_intent = intent_name
|
||||
|
||||
if best_intent:
|
||||
logger.info(f"Intent detected via KEYWORD: {best_intent}")
|
||||
return best_intent
|
||||
return best_intent, "Keyword (Fast Path)"
|
||||
|
||||
# 2. SLOW PATH: LLM Router
|
||||
if settings.get("llm_fallback_enabled", False):
|
||||
@@ -122,35 +120,23 @@ async def _classify_intent(query: str, llm: LLMService) -> str:
|
||||
prompt = router_prompt_template.replace("{query}", query)
|
||||
logger.info("Keywords failed. Asking LLM for Intent...")
|
||||
|
||||
# Kurzer Raw Call
|
||||
raw_response = await llm.generate_raw_response(prompt)
|
||||
|
||||
# --- Robust Parsing für SLMs ---
|
||||
# Wir suchen nach den bekannten Strategie-Namen im Output
|
||||
# Parsing logic
|
||||
llm_output_upper = raw_response.upper()
|
||||
logger.info(f"LLM Router Raw Output: '{raw_response}'") # Debugging
|
||||
|
||||
found_intents = []
|
||||
for strat_key in strategies.keys():
|
||||
# Wir prüfen, ob der Strategie-Name (z.B. "EMPATHY") im Text vorkommt
|
||||
if strat_key in llm_output_upper:
|
||||
found_intents.append(strat_key)
|
||||
|
||||
# Entscheidung
|
||||
final_intent = "FACT"
|
||||
if len(found_intents) == 1:
|
||||
# Eindeutiger Treffer
|
||||
final_intent = found_intents[0]
|
||||
logger.info(f"Intent detected via LLM (Parsed): {final_intent}")
|
||||
return final_intent
|
||||
return found_intents[0], "LLM Router (Slow Path)"
|
||||
elif len(found_intents) > 1:
|
||||
# Mehrere Treffer (z.B. "Es ist FACT oder DECISION") -> Nimm den ersten oder Fallback
|
||||
logger.warning(f"LLM returned multiple intents {found_intents}. Using first match: {found_intents[0]}")
|
||||
return found_intents[0]
|
||||
return found_intents[0], f"LLM Ambiguous {found_intents}"
|
||||
else:
|
||||
logger.warning(f"LLM did not return a valid strategy name. Falling back to FACT.")
|
||||
return "FACT", "LLM Fallback (No Match)"
|
||||
|
||||
return "FACT"
|
||||
return "FACT", "Default (No Match)"
|
||||
|
||||
@router.post("/", response_model=ChatResponse)
|
||||
async def chat_endpoint(
|
||||
@@ -163,9 +149,9 @@ async def chat_endpoint(
|
||||
logger.info(f"Chat request [{query_id}]: {request.message[:50]}...")
|
||||
|
||||
try:
|
||||
# 1. Intent Detection
|
||||
intent = await _classify_intent(request.message, llm)
|
||||
logger.info(f"[{query_id}] Final Intent: {intent}")
|
||||
# 1. Intent Detection (mit Source)
|
||||
intent, intent_source = await _classify_intent(request.message, llm)
|
||||
logger.info(f"[{query_id}] Final Intent: {intent} via {intent_source}")
|
||||
|
||||
# Strategy Load
|
||||
strategy = get_decision_strategy(intent)
|
||||
@@ -227,7 +213,8 @@ async def chat_endpoint(
|
||||
answer=answer_text,
|
||||
sources=hits,
|
||||
latency_ms=duration_ms,
|
||||
intent=intent
|
||||
intent=intent,
|
||||
intent_source=intent_source # NEU: Source durchreichen
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in new issue
Block a user