Upload backend/chatbot/legal_guardrails.py with huggingface_hub
Browse files
backend/chatbot/legal_guardrails.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Guardrails RAIL schema and helpers for structured legal answers.
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from __future__ import annotations
|
| 6 |
+
|
| 7 |
+
from functools import lru_cache
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
from typing import Dict, Optional
|
| 10 |
+
|
| 11 |
+
from guardrails import Guard
|
| 12 |
+
|
| 13 |
+
SCHEMA_DIR = Path(__file__).resolve().parent / "schemas"
|
| 14 |
+
RAIL_PATH = SCHEMA_DIR / "legal_answer.rail"
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
@lru_cache(maxsize=1)
|
| 18 |
+
def get_legal_guard() -> Guard:
|
| 19 |
+
"""Return cached Guard instance for legal answers."""
|
| 20 |
+
|
| 21 |
+
return Guard.from_rail(rail_file=str(RAIL_PATH))
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def ensure_schema_files() -> Optional[Dict[str, str]]:
|
| 25 |
+
"""
|
| 26 |
+
Return metadata for the legal RAIL schema to help packaging.
|
| 27 |
+
|
| 28 |
+
Called during setup to make sure the schema file is discovered by tools
|
| 29 |
+
such as setup scripts or bundlers.
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
if RAIL_PATH.exists():
|
| 33 |
+
return {"legal_rail": str(RAIL_PATH)}
|
| 34 |
+
return None
|
| 35 |
+
|