fix: universal parsing + OpenRouter + state bug — purpose_agent/purpose_function.py
Browse files
purpose_agent/purpose_function.py
CHANGED
|
@@ -225,21 +225,26 @@ class PurposeFunction:
|
|
| 225 |
]
|
| 226 |
|
| 227 |
# Get structured evaluation from LLM
|
|
|
|
|
|
|
| 228 |
try:
|
| 229 |
raw_score = self.llm.generate_structured(
|
| 230 |
messages, schema=PURPOSE_SCORE_SCHEMA, temperature=0.2
|
| 231 |
)
|
| 232 |
-
except Exception
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
raw_score =
|
| 236 |
-
|
| 237 |
-
# Extract and validate scores
|
| 238 |
-
|
| 239 |
-
|
|
|
|
|
|
|
|
|
|
| 240 |
reasoning = str(raw_score.get("reasoning", ""))
|
| 241 |
evidence = str(raw_score.get("evidence", ""))
|
| 242 |
-
confidence =
|
| 243 |
|
| 244 |
# Clamp to valid range
|
| 245 |
phi_before = max(0.0, min(10.0, phi_before))
|
|
|
|
| 225 |
]
|
| 226 |
|
| 227 |
# Get structured evaluation from LLM
|
| 228 |
+
from purpose_agent.robust_parser import parse_critic_response
|
| 229 |
+
|
| 230 |
try:
|
| 231 |
raw_score = self.llm.generate_structured(
|
| 232 |
messages, schema=PURPOSE_SCORE_SCHEMA, temperature=0.2
|
| 233 |
)
|
| 234 |
+
except Exception:
|
| 235 |
+
# Structured output not available — use universal text parser
|
| 236 |
+
raw = self.llm.generate(messages, temperature=0.2, max_tokens=2000)
|
| 237 |
+
raw_score = parse_critic_response(raw)
|
| 238 |
+
|
| 239 |
+
# Extract and validate scores (safe — parse_critic_response always returns valid keys)
|
| 240 |
+
def _safe_float(v, d=0.0):
|
| 241 |
+
try: return float(str(v).rstrip('.'))
|
| 242 |
+
except (ValueError, TypeError): return d
|
| 243 |
+
phi_before = _safe_float(raw_score.get("phi_before", 0.0))
|
| 244 |
+
phi_after = _safe_float(raw_score.get("phi_after", 0.0))
|
| 245 |
reasoning = str(raw_score.get("reasoning", ""))
|
| 246 |
evidence = str(raw_score.get("evidence", ""))
|
| 247 |
+
confidence = _safe_float(raw_score.get("confidence", 0.5))
|
| 248 |
|
| 249 |
# Clamp to valid range
|
| 250 |
phi_before = max(0.0, min(10.0, phi_before))
|