judge_agent.py (10760B)
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | import os import json import base64 import re import tempfile from typing import TypedDict, Dict, Any, Optional from langchain_openai import AzureChatOpenAI from langchain_core.messages import SystemMessage, HumanMessage from langgraph.graph import StateGraph, END from qdrant_client import models from dotenv import load_dotenv load_dotenv() from Sentinel.fmu import FMU from agent.sub_agents.base_agent import BaseReasoningAgent from Qdrant.Client import client from Qdrant.Store import COLLECTION_NAME # Import farm_memory to allow writing verdicts from agent.sub_agents.water_and_atmospheric_dependencies.retrieval import diagnose_plant, ask_memory, farm_memory # --- STATE DEFINITION --- class JudgeState(TypedDict): # Inputs current_fmu: Any image_b64: str # Added to State # Internal Context prev_point: Any crop_id: str # Forensic Evidence visual_report: Dict biography: Any # Verdict reward: float outcome: str explanation: str # Output training_data: Optional[Dict] class JudgeAgent(BaseReasoningAgent): def __init__(self): super().__init__(name="Judge Agent") self.qdrant = client # LLM for the "Deliberation" phase self.llm = AzureChatOpenAI( azure_endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"), api_key=os.environ.get("AZURE_OPENAI_API_KEY"), api_version=os.environ.get("AZURE_OPENAI_API_VERSION", "2024-12-01-preview"), deployment_name=os.environ.get("AZURE_OPENAI_DEPLOYMENT_NAME", "gpt-4.1"), temperature=0.1 ) self.app = self._build_graph() def _build_graph(self): workflow = StateGraph(JudgeState) workflow.add_node("retrieve_evidence", self.node_retrieve_evidence) workflow.add_node("run_forensics", self.node_run_forensics) workflow.add_node("deliberate", self.node_deliberate) workflow.add_node("file_verdict", self.node_file_verdict) workflow.set_entry_point("retrieve_evidence") workflow.add_conditional_edges( "retrieve_evidence", lambda x: "run_forensics" if x.get("prev_point") else "end_no_history", { "run_forensics": "run_forensics", "end_no_history": END } ) workflow.add_edge("run_forensics", "deliberate") workflow.add_edge("deliberate", "file_verdict") workflow.add_edge("file_verdict", END) return workflow.compile() # --- NODES --- def node_retrieve_evidence(self, state: JudgeState): print(f"[{self.name}] 🕵️ Retrieve Evidence...") fmu = state["current_fmu"] crop_id = fmu.metadata.get("crop_id") current_seq = fmu.metadata.get("sequence_number", 1) if current_seq <= 1: print(" -> First cycle. No history to judge.") return {"prev_point": None} prev_seq = current_seq - 1 try: s_filter = models.Filter( must=[ models.FieldCondition(key="crop_id", match=models.MatchValue(value=crop_id)), models.FieldCondition(key="sequence_number", match=models.MatchValue(value=prev_seq)) ] ) res, _ = self.qdrant.scroll( collection_name=COLLECTION_NAME, scroll_filter=s_filter, limit=1, with_vectors=True ) return {"prev_point": res[0] if res else None, "crop_id": crop_id} except Exception as e: print(f" -> DB Error: {e}") return {"prev_point": None} def node_run_forensics(self, state: JudgeState): """ Executes the TWO mandated tools: diagnose_plant and ask_memory. """ print(f"[{self.name}] 🔎 Running Forensics...") crop_id = state["crop_id"] image_b64 = state.get("image_b64") # --- TOOL 1: diagnose_plant --- visual_data = {"status": "No Image"} if image_b64: try: with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp: temp.write(base64.b64decode(image_b64)) temp_path = temp.name print(f" -> Invoking Tool inside: diagnose_plant") visual_data = diagnose_plant.invoke({"image_b64": image_b64}) os.remove(temp_path) except Exception as e: visual_data = {"error": str(e)} else: print(" -> No image found for diagnosis.") # --- TOOL 2: ask_memory --- # print(f" -> Invoking Tool: ask_memory for '{crop_id}'") try: # We updated the tool to expect 'plant_id', so we must pass that key raw_memory = ask_memory.invoke({"plant_id": crop_id}) # FIX: Force conversion to string to ensure it renders in prompt # print(f" -> Memory Retrieved: '{raw_memory}'") memory_data = str(raw_memory) # print(f" -> Memory Retrieved {memory_data}") self except Exception as e: print(f" -> Memory Tool Error: {e}") memory_data = "Memory unavailable." # Explicitly return the dict to update state keys return { "visual_report": visual_data, "biography": memory_data } def node_deliberate(self, state: JudgeState): """ LLM synthesizes Visual + History + Sensor Delta to form a verdict. """ print(f"[{self.name}] ⚖️ Deliberating...") # --- DEBUG: Verify State Content --- # print(f"DEBUG CHECK -> Biography Content: '{state.get('biography')}'") prev_sensors = state["prev_point"].payload.get("sensors", {}) curr_sensors = state["current_fmu"].metadata.get("sensors", {}) visual = state["visual_report"] # Ensure history is never None history = state.get("biography", "No history available.") prompt = f""" You are the Chief Judge of an Automated Farm. Evaluate the result of the LAST ACTION based on the transition from State N-1 to State N. --- EVIDENCE --- PREVIOUS SENSORS (N-1): {prev_sensors} CURRENT SENSORS (N): {curr_sensors} VISUAL AUTOPSY (Doctor's Report): {json.dumps(visual, indent=2)} PLANT BIOGRAPHY (Past Issues): {history} --- RUBRIC --- 1. IF Doctor found "DISEASE_DETECTED": Reward = -1.0 (Critical Failure). 2. IF Doctor found "HEALTHY" AND Sensors moved closer to targets: Reward = 0.5 to 1.0. 3. IF Sensors drifted away from targets: Reward = -0.5. TASK: Output JSON: {{ "outcome": "IMPROVED"|"DETERIORATED"|"STABLE", "reward": float(-1.0 to 1.0), "reason": "Short explanation" }} """ # print("Judge Prompt:\n", prompt) try: response = self.llm.invoke([HumanMessage(content=prompt)]) # print(f" -> LLM Response for Judge Deliberation: {response.content}") content = response.content.strip() # print(f" -> Raw LLM Output: '{content}'") code_block_match = re.search(r"```json\s*(\{.*?\})\s*```", content, re.DOTALL) if code_block_match: json_str = code_block_match.group(1) else: # 2. Fallback: Find the first '{' and the last '}' # This handles cases where the LLM forgets the ```json tags json_match = re.search(r"\{.*\}", content, re.DOTALL) if json_match: json_str = json_match.group(0) else: raise ValueError(f"No JSON found in response: {content[:50]}...") # 3. Parse verdict = json.loads(json_str) print(f" -> Verdict: {verdict.get('outcome')} ({verdict.get('reward')})") return { "outcome": verdict.get("outcome", "STABLE"), "reward": verdict.get("reward", 0.0), "explanation": verdict.get("reason", "Analysis complete.") } except Exception as e: print(f" -> Deliberation Failed: {e}") return {"outcome": "ERROR", "reward": 0.0, "explanation": "Judge LLM failed."} def node_file_verdict(self, state: JudgeState): """ Writes the final judgment to Qdrant AND FarmMemory. """ print(f"[{self.name}] 📝 Filing Verdict...") prev_id = state["prev_point"].id crop_id = state["crop_id"] # 1. Update Qdrant Snapshot self.qdrant.set_payload( collection_name=COLLECTION_NAME, points=[prev_id], payload={ "outcome": f"{state['outcome']} | Reward: {state['reward']}", "reward_score": state['reward'], "explanation_log": state["explanation"], "visual_diagnosis": str(state["visual_report"].get("health_assessment", "N/A")) } ) # 2. Write to FarmMemory (Text/Biography Store) try: verdict_summary = ( f"Cycle Review for {crop_id}: Result was {state['outcome']} " f"(Reward: {state['reward']}). Judge's Note: {state['explanation']}" ) farm_memory.log_event(crop_id, verdict_summary) except Exception as e: print(f" -> ⚠️ Failed to log to FarmMemory: {e}") # Prepare Training Data Bundle training_data = { "reward": state['reward'], "prev_action_idx": state["prev_point"].payload.get("bandit_action_id"), "prev_vector": state["prev_point"].vector, "prev_sensors": state["prev_point"].payload.get("sensor_data", {}) } return {"training_data": training_data} # --- ENTRY POINT --- def review_previous_cycle(self, current_fmu: FMU, image_b64: str): initial_state = { "current_fmu": current_fmu, "image_b64": image_b64, "prev_point": None, "crop_id": "", "visual_report": {}, "biography": "", # Starts empty "reward": 0.0, "outcome": "", "explanation": "", "training_data": None } result = self.app.invoke(initial_state) return result.get("training_data") |