PYAE1994 commited on
Commit
b9aedf5
·
verified ·
1 Parent(s): e1bd82f

Update app/agent/planner.py

Browse files
Files changed (1) hide show
  1. 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
- Break goal into steps
9
  """
10
 
11
  prompt = f"""
12
  You are a planning agent.
13
 
14
- Break this goal into steps:
15
 
16
  GOAL:
17
  {goal}
18
 
19
- Return ONLY JSON like:
 
 
 
 
 
 
20
  {{
21
  "steps": [
22
  "step 1",
@@ -26,24 +32,81 @@ Return ONLY JSON like:
26
  }}
27
  """
28
 
29
- res = requests.post(
30
- Config.LLM_API_URL,
31
- headers={
32
- "Authorization": f"Bearer {Config.LLM_API_KEY}",
33
- "Content-Type": "application/json"
34
- },
35
- json={
36
- "model": Config.MODEL,
37
- "messages": [
38
- {"role": "user", "content": prompt}
39
- ],
40
- "temperature": 0.2
41
- }
42
- )
 
 
 
 
 
43
 
44
- content = res.json()["choices"][0]["message"]["content"]
 
 
 
 
45
 
46
- try:
47
- return json.loads(content)
48
- except:
49
- return {"steps": [goal]}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ }