diff --git a/gateway/config.py b/gateway/config.py index 08c3d8b..cb79cf8 100644 --- a/gateway/config.py +++ b/gateway/config.py @@ -31,8 +31,8 @@ GUARDRAIL_WARN_THRESHOLD = float(os.environ.get("GUARDRAIL_WARN_THRESHOLD", "0.7 GUARDRAIL_VERSION = "similarity-v2" # Strato 3 del guardrail: cross-encoder (richiede catena rerank attiva) GUARDRAIL_RERANK = os.environ.get("GUARDRAIL_RERANK", "false").lower() == "true" -GUARDRAIL_RERANK_BLOCK = float(os.environ.get("GUARDRAIL_RERANK_BLOCK", "0.88")) -GUARDRAIL_RERANK_SUGGEST = float(os.environ.get("GUARDRAIL_RERANK_SUGGEST", "0.80")) +GUARDRAIL_RERANK_BLOCK = float(os.environ.get("GUARDRAIL_RERANK_BLOCK", "0.90")) +GUARDRAIL_RERANK_SUGGEST = float(os.environ.get("GUARDRAIL_RERANK_SUGGEST", "0.85")) # Verifica supersede: cross-score (nuovo, vecchio) sotto soglia → warning non bloccante GUARDRAIL_SUPERSEDE_CHECK = os.environ.get("GUARDRAIL_SUPERSEDE_CHECK", "false").lower() == "true" GUARDRAIL_SUPERSEDE_MIN = float(os.environ.get("GUARDRAIL_SUPERSEDE_MIN", "0.50")) diff --git a/gateway/guardrail.py b/gateway/guardrail.py index ac2b841..0c3a13b 100644 --- a/gateway/guardrail.py +++ b/gateway/guardrail.py @@ -36,7 +36,7 @@ def text_hash(text: str) -> str: return hashlib.sha256(normalize_text(text).encode("utf-8")).hexdigest() -def find_similar(qdrant: Any, collection: str, text: str, vector: list[float], top_k: int = 3) -> list[dict]: +def find_similar(qdrant: Any, collection: str, text: str, vector: list[float], top_k: int = 5) -> list[dict]: qfilter = qm.Filter(must=[qm.IsEmptyCondition(is_empty=qm.PayloadField(key="superseded_by"))]) hits = qdrant.query_points(collection_name=collection, query=vector, query_filter=qfilter, limit=top_k, with_payload=True).points return [ diff --git a/gateway/routes.py b/gateway/routes.py index 75a7fb3..f5e54a4 100644 --- a/gateway/routes.py +++ b/gateway/routes.py @@ -246,7 +246,10 @@ async def score(body: ScoreIn, key: str = Depends(require_auth)) -> dict: @router.get("/v1/memories/{memory_id}") async def get_memory(memory_id: str, key: str = Depends(require_auth)) -> dict: - point = state.qdrant.retrieve(collection_name=COLLECTION, ids=[memory_id], with_payload=True) + try: + point = state.qdrant.retrieve(collection_name=COLLECTION, ids=[memory_id], with_payload=True) + except Exception: # noqa: BLE001 — id non-UUID o payload malformato → non trovato, non 500 + point = [] if not point: raise HTTPException(status_code=404, detail="Memoria non trovata") audit(key, "get", memory_id=memory_id)