Spaces:
Sleeping
Sleeping
fix(guardrail): merge domain_keywords into signature and skip guardrail when sigma is empty
Browse files
backend/modules/reasoning_engine.py
CHANGED
|
@@ -270,13 +270,32 @@ class ReasoningEngine:
|
|
| 270 |
"chunk_count": 0
|
| 271 |
}
|
| 272 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
agent_data = {
|
| 274 |
"id": agent.id,
|
| 275 |
"name": agent.name,
|
| 276 |
"system_prompt": agent.system_prompt,
|
| 277 |
"domain": agent.domain or "general",
|
| 278 |
-
"domain_signature":
|
| 279 |
-
"prompt_analysis":
|
| 280 |
"knowledge_graph": agent.knowledge_graph_json or {},
|
| 281 |
"chunk_count": agent.chunk_count or 0
|
| 282 |
}
|
|
@@ -317,6 +336,10 @@ class ReasoningEngine:
|
|
| 317 |
|
| 318 |
sigma = set(s.lower() for s in (domain_signature or []))
|
| 319 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
if not phi_q:
|
| 321 |
logger.info("Guardrail: phi_q is empty — query has no content tokens, rejecting.")
|
| 322 |
return False, 0.0
|
|
|
|
| 270 |
"chunk_count": 0
|
| 271 |
}
|
| 272 |
else:
|
| 273 |
+
# Build combined signature: domain_signature (TF-IDF terms from docs)
|
| 274 |
+
# + domain_keywords (generic domain terms from prompt analysis)
|
| 275 |
+
# This ensures generic queries like "medical question" match the guardrail
|
| 276 |
+
raw_signature = list(agent.domain_signature or [])
|
| 277 |
+
prompt_analysis = agent.prompt_analysis or {"domain": agent.domain or "general"}
|
| 278 |
+
domain_keywords = agent.domain_keywords or prompt_analysis.get("domain_keywords", [])
|
| 279 |
+
|
| 280 |
+
# Merge domain_keywords into signature (deduped)
|
| 281 |
+
existing = set(s.lower() for s in raw_signature)
|
| 282 |
+
for kw in domain_keywords:
|
| 283 |
+
if kw.lower() not in existing:
|
| 284 |
+
raw_signature.append(kw.lower())
|
| 285 |
+
existing.add(kw.lower())
|
| 286 |
+
|
| 287 |
+
# Also add the domain name itself (e.g., "medical")
|
| 288 |
+
domain_name = (agent.domain or "general").lower()
|
| 289 |
+
if domain_name not in existing:
|
| 290 |
+
raw_signature.append(domain_name)
|
| 291 |
+
|
| 292 |
agent_data = {
|
| 293 |
"id": agent.id,
|
| 294 |
"name": agent.name,
|
| 295 |
"system_prompt": agent.system_prompt,
|
| 296 |
"domain": agent.domain or "general",
|
| 297 |
+
"domain_signature": raw_signature,
|
| 298 |
+
"prompt_analysis": prompt_analysis,
|
| 299 |
"knowledge_graph": agent.knowledge_graph_json or {},
|
| 300 |
"chunk_count": agent.chunk_count or 0
|
| 301 |
}
|
|
|
|
| 336 |
|
| 337 |
sigma = set(s.lower() for s in (domain_signature or []))
|
| 338 |
|
| 339 |
+
if not sigma:
|
| 340 |
+
logger.info("Guardrail: sigma is empty — no domain signature, allowing query through.")
|
| 341 |
+
return True, 1.0
|
| 342 |
+
|
| 343 |
if not phi_q:
|
| 344 |
logger.info("Guardrail: phi_q is empty — query has no content tokens, rejecting.")
|
| 345 |
return False, 0.0
|