fix(gateway): calibrazione soglie cross-gate (BLOCK 0.90, SUGGEST 0.85 su distribuzione reale: dup 0.9989 / correlato 0.8766 / irrilevante 0.0), find_similar top-5, GET id invalido → 404

This commit is contained in:
enne2
2026-09-08 13:04:45 +02:00
parent 128059dd4b
commit 681834d2a3
3 changed files with 7 additions and 4 deletions
+2 -2
View File
@@ -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"))
+1 -1
View File
@@ -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 [
+3
View File
@@ -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:
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)