45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
"""
|
||
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:
|
||
0.2.0 (2025-12-07) - Update für WP04c Feedback
|
||
0.1.0 (2025-10-07) – Erstanlage.
|
||
"""
|
||
from __future__ import annotations
|
||
from fastapi import APIRouter, HTTPException, BackgroundTasks
|
||
from app.models.dto import QueryRequest, QueryResponse
|
||
from app.core.retriever import hybrid_retrieve, semantic_retrieve
|
||
# NEU:
|
||
from app.services.feedback_service import log_search
|
||
|
||
router = APIRouter()
|
||
|
||
@router.post("", response_model=QueryResponse)
|
||
def post_query(req: QueryRequest, background_tasks: BackgroundTasks) -> QueryResponse:
|
||
try:
|
||
if req.mode == "semantic":
|
||
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
|
||
except ValueError as e:
|
||
raise HTTPException(status_code=400, detail=str(e))
|
||
except Exception as e:
|
||
raise HTTPException(status_code=500, detail=f"query failed: {e}") |