Spaces:
Sleeping
Sleeping
Update app/agent/planner.py
Browse files- app/agent/planner.py +85 -22
app/agent/planner.py
CHANGED
|
@@ -5,18 +5,24 @@ from config import Config
|
|
| 5 |
|
| 6 |
def create_plan(goal: str):
|
| 7 |
"""
|
| 8 |
-
|
| 9 |
"""
|
| 10 |
|
| 11 |
prompt = f"""
|
| 12 |
You are a planning agent.
|
| 13 |
|
| 14 |
-
Break
|
| 15 |
|
| 16 |
GOAL:
|
| 17 |
{goal}
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
{{
|
| 21 |
"steps": [
|
| 22 |
"step 1",
|
|
@@ -26,24 +32,81 @@ Return ONLY JSON like:
|
|
| 26 |
}}
|
| 27 |
"""
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def create_plan(goal: str):
|
| 7 |
"""
|
| 8 |
+
Robust goal planner with full safety checks
|
| 9 |
"""
|
| 10 |
|
| 11 |
prompt = f"""
|
| 12 |
You are a planning agent.
|
| 13 |
|
| 14 |
+
Break the goal into small executable steps.
|
| 15 |
|
| 16 |
GOAL:
|
| 17 |
{goal}
|
| 18 |
|
| 19 |
+
RULES:
|
| 20 |
+
- Output MUST be valid JSON only
|
| 21 |
+
- No explanation
|
| 22 |
+
- No markdown
|
| 23 |
+
- No extra text
|
| 24 |
+
|
| 25 |
+
FORMAT:
|
| 26 |
{{
|
| 27 |
"steps": [
|
| 28 |
"step 1",
|
|
|
|
| 32 |
}}
|
| 33 |
"""
|
| 34 |
|
| 35 |
+
try:
|
| 36 |
+
# =========================
|
| 37 |
+
# CALL LLM SAFELY
|
| 38 |
+
# =========================
|
| 39 |
+
res = requests.post(
|
| 40 |
+
Config.LLM_API_URL,
|
| 41 |
+
headers={
|
| 42 |
+
"Authorization": f"Bearer {Config.LLM_API_KEY}",
|
| 43 |
+
"Content-Type": "application/json"
|
| 44 |
+
},
|
| 45 |
+
json={
|
| 46 |
+
"model": Config.MODEL,
|
| 47 |
+
"messages": [
|
| 48 |
+
{"role": "user", "content": prompt}
|
| 49 |
+
],
|
| 50 |
+
"temperature": 0.2
|
| 51 |
+
},
|
| 52 |
+
timeout=30
|
| 53 |
+
)
|
| 54 |
|
| 55 |
+
# =========================
|
| 56 |
+
# HTTP CHECK
|
| 57 |
+
# =========================
|
| 58 |
+
if res.status_code != 200:
|
| 59 |
+
return {"steps": [goal]}
|
| 60 |
|
| 61 |
+
# =========================
|
| 62 |
+
# SAFE JSON PARSE
|
| 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 |
+
choices = data.get("choices", [])
|
| 76 |
+
if not choices:
|
| 77 |
+
return {"steps": [goal]}
|
| 78 |
+
|
| 79 |
+
content = (
|
| 80 |
+
choices[0]
|
| 81 |
+
.get("message", {})
|
| 82 |
+
.get("content", "")
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
if not content:
|
| 86 |
+
return {"steps": [goal]}
|
| 87 |
+
|
| 88 |
+
# =========================
|
| 89 |
+
# PARSE JSON CONTENT
|
| 90 |
+
# =========================
|
| 91 |
+
try:
|
| 92 |
+
parsed = json.loads(content)
|
| 93 |
+
|
| 94 |
+
if isinstance(parsed, dict) and "steps" in parsed:
|
| 95 |
+
if isinstance(parsed["steps"], list):
|
| 96 |
+
return parsed
|
| 97 |
+
|
| 98 |
+
except Exception:
|
| 99 |
+
pass
|
| 100 |
+
|
| 101 |
+
# =========================
|
| 102 |
+
# FALLBACK STEP SPLIT
|
| 103 |
+
# =========================
|
| 104 |
+
return {
|
| 105 |
+
"steps": [goal]
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
except Exception:
|
| 109 |
+
# HARD FALLBACK
|
| 110 |
+
return {
|
| 111 |
+
"steps": [goal]
|
| 112 |
+
}
|