2025-10-07 11:31:26 +02:00
|
|
|
|
"""
|
|
|
|
|
|
app/routers/query.py — Query-Endpunkte (WP-04)
|
|
|
|
|
|
|
|
|
|
|
|
Zweck:
|
|
|
|
|
|
Stellt POST /query bereit und ruft den passenden Retriever-Modus auf.
|
|
|
|
|
|
Kompatibilität:
|
|
|
|
|
|
Python 3.12+, FastAPI 0.110+
|
|
|
|
|
|
Version:
|
|
|
|
|
|
0.1.0 (Erstanlage)
|
|
|
|
|
|
Stand:
|
|
|
|
|
|
2025-10-07
|
|
|
|
|
|
Bezug:
|
|
|
|
|
|
- app/core/retriever.py
|
|
|
|
|
|
- app/models/dto.py
|
|
|
|
|
|
Nutzung:
|
|
|
|
|
|
app.include_router(query.router, prefix="/query", tags=["query"])
|
|
|
|
|
|
Änderungsverlauf:
|
2025-12-07 19:29:48 +01:00
|
|
|
|
0.2.0 (2025-12-07) - Update für WP04c Feedback
|
2025-10-07 11:31:26 +02:00
|
|
|
|
0.1.0 (2025-10-07) – Erstanlage.
|
|
|
|
|
|
"""
|
|
|
|
|
|
from __future__ import annotations
|
2025-12-07 19:29:48 +01:00
|
|
|
|
from fastapi import APIRouter, HTTPException, BackgroundTasks
|
2025-10-07 11:31:26 +02:00
|
|
|
|
from app.models.dto import QueryRequest, QueryResponse
|
|
|
|
|
|
from app.core.retriever import hybrid_retrieve, semantic_retrieve
|
2025-12-07 19:29:48 +01:00
|
|
|
|
# NEU:
|
|
|
|
|
|
from app.services.feedback_service import log_search
|
2025-10-07 11:31:26 +02:00
|
|
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("", response_model=QueryResponse)
|
2025-12-07 19:29:48 +01:00
|
|
|
|
def post_query(req: QueryRequest, background_tasks: BackgroundTasks) -> QueryResponse:
|
2025-10-07 11:31:26 +02:00
|
|
|
|
try:
|
|
|
|
|
|
if req.mode == "semantic":
|
2025-12-07 19:29:48 +01:00
|
|
|
|
res = semantic_retrieve(req)
|
|
|
|
|
|
else:
|
|
|
|
|
|
res = hybrid_retrieve(req)
|
|
|
|
|
|
|
|
|
|
|
|
# WP-04c: Logging im Hintergrund (bremst Antwort nicht)
|
|
|
|
|
|
background_tasks.add_task(log_search, req, res)
|
|
|
|
|
|
|
|
|
|
|
|
return res
|
2025-10-07 11:31:26 +02:00
|
|
|
|
except ValueError as e:
|
|
|
|
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
except Exception as e:
|
2025-12-07 19:29:48 +01:00
|
|
|
|
raise HTTPException(status_code=500, detail=f"query failed: {e}")
|