Spaces:
Sleeping
Sleeping
priyansh-saxena1 commited on
Commit ·
c9ecd03
1
Parent(s): 11a7703
fix: ROS hallucination guard + debug logging
Browse files- app/graph.py +24 -0
app/graph.py
CHANGED
|
@@ -140,6 +140,30 @@ def agent_node(state: IntakeState) -> dict:
|
|
| 140 |
object.__setattr__(result, "reply", "Thank you — I have everything I need.")
|
| 141 |
break
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
print(f"[{time.time():.3f}] [Graph Node] LLM returned. Preparing node dictionaries...")
|
| 144 |
|
| 145 |
stage = compute_stage(result)
|
|
|
|
| 140 |
object.__setattr__(result, "reply", "Thank you — I have everything I need.")
|
| 141 |
break
|
| 142 |
|
| 143 |
+
# ── ROS Hallucination Guard: LLM can only ADD one new ROS system per turn ──
|
| 144 |
+
try:
|
| 145 |
+
prev_state = json.loads(current_json)
|
| 146 |
+
prev_ros = prev_state.get("ros", {})
|
| 147 |
+
except Exception:
|
| 148 |
+
prev_ros = {}
|
| 149 |
+
new_ros_keys = [k for k in result.ros if k not in prev_ros]
|
| 150 |
+
if len(new_ros_keys) > 1:
|
| 151 |
+
print(f"[ROSGuard] LLM hallucinated {len(new_ros_keys)} new ROS systems in one turn: {new_ros_keys}. Keeping only first.")
|
| 152 |
+
# Keep only the first new system, strip the rest
|
| 153 |
+
allowed_ros = dict(prev_ros) # keep old
|
| 154 |
+
allowed_ros[new_ros_keys[0]] = result.ros[new_ros_keys[0]]
|
| 155 |
+
object.__setattr__(result, "ros", allowed_ros)
|
| 156 |
+
# Generate a reply asking about the next missing system
|
| 157 |
+
ROS_QUESTIONS = {
|
| 158 |
+
"cardiac": "Have you experienced any palpitations, leg swelling, or dizziness?",
|
| 159 |
+
"respiratory": "Have you had any shortness of breath, coughing, or wheezing?",
|
| 160 |
+
"gi": "Have you had any nausea, vomiting, or heartburn?",
|
| 161 |
+
}
|
| 162 |
+
for sys_name, question in ROS_QUESTIONS.items():
|
| 163 |
+
if sys_name not in allowed_ros:
|
| 164 |
+
object.__setattr__(result, "reply", question)
|
| 165 |
+
break
|
| 166 |
+
|
| 167 |
print(f"[{time.time():.3f}] [Graph Node] LLM returned. Preparing node dictionaries...")
|
| 168 |
|
| 169 |
stage = compute_stage(result)
|