Spaces:
Sleeping
Sleeping
Update app/agent/planner.py
Browse files- app/agent/planner.py +31 -30
app/agent/planner.py
CHANGED
|
@@ -5,22 +5,26 @@ from config import Config
|
|
| 5 |
|
| 6 |
def create_plan(goal: str):
|
| 7 |
"""
|
| 8 |
-
Robust
|
|
|
|
|
|
|
|
|
|
| 9 |
"""
|
| 10 |
|
| 11 |
prompt = f"""
|
| 12 |
You are a planning agent.
|
| 13 |
|
| 14 |
-
Break the goal into
|
| 15 |
|
| 16 |
GOAL:
|
| 17 |
{goal}
|
| 18 |
|
| 19 |
RULES:
|
| 20 |
-
- Output
|
| 21 |
-
-
|
| 22 |
-
-
|
| 23 |
-
-
|
|
|
|
| 24 |
|
| 25 |
FORMAT:
|
| 26 |
{{
|
|
@@ -34,7 +38,7 @@ FORMAT:
|
|
| 34 |
|
| 35 |
try:
|
| 36 |
# =========================
|
| 37 |
-
# CALL LLM
|
| 38 |
# =========================
|
| 39 |
res = requests.post(
|
| 40 |
Config.LLM_API_URL,
|
|
@@ -59,54 +63,51 @@ FORMAT:
|
|
| 59 |
return {"steps": [goal]}
|
| 60 |
|
| 61 |
# =========================
|
| 62 |
-
#
|
| 63 |
# =========================
|
| 64 |
try:
|
| 65 |
data = res.json()
|
| 66 |
except Exception:
|
| 67 |
return {"steps": [goal]}
|
| 68 |
|
| 69 |
-
# =========================
|
| 70 |
-
# SAFE STRUCTURE CHECK
|
| 71 |
-
# =========================
|
| 72 |
if not isinstance(data, dict):
|
| 73 |
return {"steps": [goal]}
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
| 77 |
return {"steps": [goal]}
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
.get("message", {})
|
| 82 |
-
.get("content", "")
|
| 83 |
-
)
|
| 84 |
|
| 85 |
if not content:
|
| 86 |
return {"steps": [goal]}
|
| 87 |
|
| 88 |
# =========================
|
| 89 |
-
# PARSE
|
| 90 |
# =========================
|
| 91 |
try:
|
| 92 |
parsed = json.loads(content)
|
| 93 |
|
| 94 |
-
if
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
except Exception:
|
| 99 |
pass
|
| 100 |
|
| 101 |
# =========================
|
| 102 |
-
# FALLBACK
|
| 103 |
# =========================
|
| 104 |
-
return {
|
| 105 |
-
"steps": [goal]
|
| 106 |
-
}
|
| 107 |
|
| 108 |
except Exception:
|
| 109 |
-
# HARD
|
| 110 |
-
return {
|
| 111 |
-
"steps": [goal]
|
| 112 |
-
}
|
|
|
|
| 5 |
|
| 6 |
def create_plan(goal: str):
|
| 7 |
"""
|
| 8 |
+
Robust AI planner:
|
| 9 |
+
- safe LLM call
|
| 10 |
+
- strict JSON parsing
|
| 11 |
+
- fallback protection
|
| 12 |
"""
|
| 13 |
|
| 14 |
prompt = f"""
|
| 15 |
You are a planning agent.
|
| 16 |
|
| 17 |
+
Break the goal into executable steps.
|
| 18 |
|
| 19 |
GOAL:
|
| 20 |
{goal}
|
| 21 |
|
| 22 |
RULES:
|
| 23 |
+
- Output ONLY valid JSON
|
| 24 |
+
- NO explanation
|
| 25 |
+
- NO markdown
|
| 26 |
+
- NO extra text
|
| 27 |
+
- steps must be simple actionable instructions
|
| 28 |
|
| 29 |
FORMAT:
|
| 30 |
{{
|
|
|
|
| 38 |
|
| 39 |
try:
|
| 40 |
# =========================
|
| 41 |
+
# CALL LLM
|
| 42 |
# =========================
|
| 43 |
res = requests.post(
|
| 44 |
Config.LLM_API_URL,
|
|
|
|
| 63 |
return {"steps": [goal]}
|
| 64 |
|
| 65 |
# =========================
|
| 66 |
+
# JSON PARSE SAFE
|
| 67 |
# =========================
|
| 68 |
try:
|
| 69 |
data = res.json()
|
| 70 |
except Exception:
|
| 71 |
return {"steps": [goal]}
|
| 72 |
|
|
|
|
|
|
|
|
|
|
| 73 |
if not isinstance(data, dict):
|
| 74 |
return {"steps": [goal]}
|
| 75 |
|
| 76 |
+
# =========================
|
| 77 |
+
# EXTRACT CONTENT
|
| 78 |
+
# =========================
|
| 79 |
+
choices = data.get("choices")
|
| 80 |
+
if not choices or not isinstance(choices, list):
|
| 81 |
return {"steps": [goal]}
|
| 82 |
|
| 83 |
+
message = choices[0].get("message", {})
|
| 84 |
+
content = message.get("content", "")
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
if not content:
|
| 87 |
return {"steps": [goal]}
|
| 88 |
|
| 89 |
# =========================
|
| 90 |
+
# PARSE PLAN JSON
|
| 91 |
# =========================
|
| 92 |
try:
|
| 93 |
parsed = json.loads(content)
|
| 94 |
|
| 95 |
+
if (
|
| 96 |
+
isinstance(parsed, dict)
|
| 97 |
+
and "steps" in parsed
|
| 98 |
+
and isinstance(parsed["steps"], list)
|
| 99 |
+
and len(parsed["steps"]) > 0
|
| 100 |
+
):
|
| 101 |
+
return parsed
|
| 102 |
|
| 103 |
except Exception:
|
| 104 |
pass
|
| 105 |
|
| 106 |
# =========================
|
| 107 |
+
# FALLBACK (SAFE)
|
| 108 |
# =========================
|
| 109 |
+
return {"steps": [goal]}
|
|
|
|
|
|
|
| 110 |
|
| 111 |
except Exception:
|
| 112 |
+
# HARD FAIL SAFE
|
| 113 |
+
return {"steps": [goal]}
|
|
|
|
|
|