Update für Feedback Loop
This commit is contained in:
1 parent
bbc1e589f5
commit
76ea8e3350
2 files changed
+77
-32
No files matched your search
+33
-6
@@ -1,6 +1,12 @@
|
||||
"""
|
||||
app/routers/chat.py — RAG Endpunkt (WP-06 Hybrid Router v3)
|
||||
Update: Transparenz über Intent-Source (Keyword vs. LLM).
|
||||
app/routers/chat.py — RAG Endpunkt (WP-06 Hybrid Router + WP-04c Feedback)
|
||||
Version: 2.3.2 (Merged Stability Patch)
|
||||
|
||||
Features:
|
||||
- Hybrid Intent Router (Keyword + LLM)
|
||||
- Strategic Retrieval (Late Binding via Config)
|
||||
- Context Enrichment (Payload/Source Fallback)
|
||||
- Data Flywheel (Feedback Logging Integration)
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
@@ -15,6 +21,8 @@ from app.config import get_settings
|
||||
from app.models.dto import ChatRequest, ChatResponse, QueryRequest, QueryHit
|
||||
from app.services.llm_service import LLMService
|
||||
from app.core.retriever import Retriever
|
||||
# [MERGE] Integration Feedback Service (WP-04c)
|
||||
from app.services.feedback_service import log_search
|
||||
|
||||
router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -77,7 +85,7 @@ def _build_enriched_context(hits: List[QueryHit]) -> str:
|
||||
)
|
||||
title = hit.note_id or "Unbekannt"
|
||||
|
||||
# FIX: Wir holen den Typ aus Payload oder Source (Fallback)
|
||||
# [FIX] Robustes Auslesen des Typs (Payload > Source > Unknown)
|
||||
payload = hit.payload or {}
|
||||
note_type = payload.get("type") or source.get("type", "unknown")
|
||||
note_type = str(note_type).upper()
|
||||
@@ -173,7 +181,7 @@ async def chat_endpoint(
|
||||
retrieve_result = await retriever.search(query_req)
|
||||
hits = retrieve_result.results
|
||||
|
||||
# 3. Strategic Retrieval
|
||||
# 3. Strategic Retrieval (WP-06 Kernfeature)
|
||||
if inject_types:
|
||||
logger.info(f"[{query_id}] Executing Strategic Retrieval for types: {inject_types}...")
|
||||
strategy_req = QueryRequest(
|
||||
@@ -207,18 +215,37 @@ async def chat_endpoint(
|
||||
|
||||
logger.info(f"[{query_id}] Sending to LLM (Intent: {intent}, Template: {prompt_key})...")
|
||||
|
||||
# System-Prompt separat übergeben
|
||||
# System-Prompt separat übergeben (WP-06a Fix)
|
||||
answer_text = await llm.generate_raw_response(prompt=final_prompt, system=system_prompt)
|
||||
|
||||
duration_ms = int((time.time() - start_time) * 1000)
|
||||
|
||||
# 6. Logging (Fire & Forget) - [MERGE POINT]
|
||||
# Wir loggen alles für das Data Flywheel (WP-08 Self-Tuning)
|
||||
try:
|
||||
log_search(
|
||||
query_id=query_id,
|
||||
query_text=request.message,
|
||||
results=hits,
|
||||
mode="chat_rag",
|
||||
metadata={
|
||||
"intent": intent,
|
||||
"intent_source": intent_source,
|
||||
"generated_answer": answer_text,
|
||||
"model": llm.settings.LLM_MODEL
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Logging failed: {e}")
|
||||
|
||||
# 7. Response
|
||||
return ChatResponse(
|
||||
query_id=query_id,
|
||||
answer=answer_text,
|
||||
sources=hits,
|
||||
latency_ms=duration_ms,
|
||||
intent=intent,
|
||||
intent_source=intent_source # Source durchreichen
|
||||
intent_source=intent_source
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in new issue
Block a user