forked from topherchris420/james_library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtruth_layer.py
More file actions
45 lines (37 loc) · 1.4 KB
/
truth_layer.py
File metadata and controls
45 lines (37 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""Grounding-first response envelope utilities."""
from __future__ import annotations
from dataclasses import asdict, dataclass
from typing import Any
@dataclass
class Evidence:
source: str
quote: str
span_start: int | None = None
span_end: int | None = None
def build_grounded_response(
answer: str,
confidence: float,
provenance: list[str],
evidence: list[Evidence],
repro_steps: list[str],
) -> dict[str, Any]:
"""Build a response object with explicit provenance and grounding checks."""
bounded_confidence = max(0.0, min(1.0, float(confidence)))
red_badge = len(evidence) == 0 or len(provenance) == 0
return {
"answer": answer,
"confidence": bounded_confidence,
"provenance": provenance,
"evidence": [asdict(e) for e in evidence],
"repro_steps": repro_steps,
"grounded": not red_badge,
"red_badge": red_badge,
}
def assert_grounded(response: dict[str, Any]) -> None:
"""Raise ValueError if response is not properly grounded."""
required = ["confidence", "provenance", "evidence", "repro_steps"]
missing = [k for k in required if k not in response]
if missing:
raise ValueError(f"Missing grounding fields: {', '.join(missing)}")
if not response.get("provenance") or not response.get("evidence"):
raise ValueError("Ungrounded response: provenance and evidence are required")