mindnet/app/routers/query.py

34 lines
1.2 KiB
Python
Raw Normal View History

2025-10-07 11:31:26 +02:00
"""
2025-12-15 15:40:39 +01:00
FILE: app/routers/query.py
DESCRIPTION: Klassische Such-Endpunkte (Semantic & Hybrid). Initiiert asynchrones Feedback-Logging und ruft den richtigen Retriever Modus auf
VERSION: 0.2.0
STATUS: Active
DEPENDENCIES: app.models.dto, app.core.retriever, app.services.feedback_service
LAST_ANALYSIS: 2025-12-15
2025-10-07 11:31:26 +02:00
"""
2025-12-15 15:40:39 +01:00
2025-10-07 11:31:26 +02:00
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
2025-12-28 11:04:40 +01:00
from app.core.retrieval.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}")