-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplanner.py
328 lines (276 loc) · 11 KB
/
planner.py
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import json
from custom_types import LLMPlan
from plan import PlanConverter, PlanDAG
from prompts import PLAN_REFINE_PROMPT, PLAN_SYSTEM_PROMPT, PLAN_FIX_PROMPT
from utils import openai_client
class Planner:
"""
Handles the management, generation, refinement and replanning of plans.
Attributes:
plan_history (list[PlanDAG]): Stores different versions of the plan.
system_prompt (str): System prompt template for LLM interaction.
refine_prompt (str): Template for refining plans.
fix_plan_prompt (str): Template for fixing incomplete or incorrect plans.
client (object): OpenAI client used for LLM interactions.
config (dict): Configuration parameters for the model execution.
agent_registry (AgentRegistry): Registry containing all available agents.
"""
def __init__(self, agent_registry):
"""Initializes planner with agent registry, and required prompts and configurations"""
self.plan_history: list[PlanDAG] = [] # Stores different versions of the plan, list[PlanDAG]
self.system_prompt = PLAN_SYSTEM_PROMPT.format(
agent_registry=agent_registry.get_agents_description()
)
self.refine_prompt = PLAN_REFINE_PROMPT
self.fix_plan_prompt = PLAN_FIX_PROMPT
self.client = openai_client
self.config = {"model": "gpt-4o", "temperature": 0, "response_format": LLMPlan}
self.agent_registry = agent_registry
self.agent_names = agent_registry.get_agents_names()
def modify_config(self, params):
"""Modifies the LLM configuration."""
self.config.update(params)
def append_plan(self, plan):
"""Appends a new plan to the history."""
self.plan_history.append(plan)
def generate_plan(self, query: str, is_replan: bool = False) -> PlanDAG:
"""
Generates a new plan based on the user query.
Args:
query (str): The user's query or task description.
is_replan (bool): Indicates if it is a replan (default: False).
Returns:
PlanDAG: The generated plan in DAG format.
"""
llm_plan = self._llm_planner(query)
plan = PlanDAG().initialize_from_LLMPlan(query, llm_plan, self.agent_names)
if not is_replan:
plan.initialize_plan_status()
plan.intitialize_exec_status()
else:
plan.set_plan_status("MODIFIED")
plan.initialize_params(agent_registry=self.agent_registry)
self.plan_history.append(plan)
return plan
def refine_plan(self, query):
"""Refine plan using query and dag manipulation"""
pass
def fix_plan(self, plan):
"""
Fixes a given plan using LLM correction.
Args:
plan (dict): The plan to fix.
Returns:
PlanDAG: The corrected plan.
"""
query = plan.query
plan = PlanConverter.dag_to_LLMPlan(plan.dag)
llm_plan = self._llm_fixer(query, plan)
plan = PlanDAG().initialize_from_LLMPlan(query, llm_plan, self.agent_names)
plan.initialize_plan_status()
plan.intitialize_exec_status()
plan.initialize_params(agent_registry=self.agent_registry)
self.plan_history.append(plan)
return plan
def add_node(self, prev_plan, node_id, node_data):
"""
Adds a new node to the plan.
Args:
prev_plan (PlanDAG): The previous plan.
node_id (int): id of the node to add.
node_data (dict): Data associated with the new node.
Returns:
PlanDAG: The updated plan.
"""
plan = PlanDAG().initialize_from_dag(prev_plan.copy())
plan.add_node(node_id, node_data)
plan.set_node_plan_status(node_id, "MODIFIED")
self.plan_history.append(plan)
return plan
def remove_node(self, prev_plan, node_id):
"""
Removes a node from the plan.
Args:
prev_plan (PlanDAG): The previous plan.
node_id (int): id of the node to remove.
Returns:
PlanDAG: The updated plan.
"""
plan = PlanDAG().initialize_from_dag(prev_plan.copy())
plan.remove_node(node_id)
self.plan_history.append(plan)
return plan
def update_node(self, prev_plan, node_id, node_data):
"""
Updates a node's attributes in the plan.
Args:
prev_plan (PlanDAG): The previous plan.
node_id (int): id of the node to update.
node_data (dict): Updated node attributes.
Returns:
PlanDAG: The updated plan.
"""
plan = PlanDAG().initialize_from_dag(prev_plan.copy())
plan.update_node(node_id, node_data)
plan.set_node_plan_status(node_id, "MODIFIED")
self.plan_history.append(plan)
return plan
def update_node_edge(self, prev_plan, node_id, node_data, edges):
"""
Updates a node and its corresponding edges.
Args:
prev_plan (PlanDAG): The previous plan.
node_id (int): id of the node to update.
node_data (dict): Updated node attributes.
edges (list[dict]): List of edges to update.
Returns:
PlanDAG: The updated plan.
"""
plan = PlanDAG().initialize_from_dag(prev_plan.copy())
plan.update_node_edge(node_id, node_data, edges)
plan.set_node_plan_status(node_id, "MODIFIED")
self.plan_history.append(plan)
return plan
def add_edge(self, prev_plan, src, dest, edge_data):
"""
Adds an edge between two nodes.
Args:
prev_plan (PlanDAG): The previous plan.
src (int): Source node id.
dest (int): Destination node id.
edge_data (dict): Edge attributes.
Returns:
PlanDAG: The updated plan.
"""
plan = PlanDAG().initialize_from_dag(prev_plan.copy())
plan.add_edge(src, dest, edge_data)
plan.set_edge_plan_status(src, dest, "MODIFIED", key=(edge_data["src_output"], edge_data["dest_input"]))
self.plan_history.append(plan)
return plan
def remove_edge(self, prev_plan, src, dest, edge_data):
"""
Removes an edge from the plan.
Args:
prev_plan (PlanDAG): The previous plan.
src (int): Source node id.
dest (int): Destination node id.
edge_data (dict): Edge attributes.
Returns:
PlanDAG: The updated plan.
"""
plan = PlanDAG().initialize_from_dag(prev_plan.copy())
plan.remove_edge(src, dest, edge_data)
self.plan_history.append(plan)
return plan
def update_exec(self, prev_plan, node_id, node_exec, node_attr, node_attr_value):
"""
Updates the execution results and status of a node in the plan.
Args:
prev_plan (PlanDAG): The previous plan.
node_id (int): id of the node to update.
node_exec (dict): Execution data to be updated.
Returns:
PlanDAG: The updated plan with modified execution results and status.
"""
plan = PlanDAG().initialize_from_dag(prev_plan.copy())
plan.update_exec(node_id, node_exec, node_attr, node_attr_value)
plan.set_node_exec_status(node_id, "MODIFIED")
self.plan_history.append(plan)
return plan
def refine_plan_nl(self, feedback: str) -> PlanDAG:
"""
Refines the existing plan using natural language feedback.
Args:
feedback (str): User-provided feedback in natural language.
Returns:
PlanDAG: The refined plan with modifications applied.
"""
prev_plan = self.get_latest_plan()
prev_llm_plan = PlanConverter.dag_to_LLMPlan(prev_plan.dag)
llm_plan = self._llm_refiner(prev_plan=prev_llm_plan, feedback=feedback)
plan = PlanDAG().initialize_from_LLMPlan(prev_plan.query, llm_plan, self.agent_names)
plan.set_plan_status("MODIFIED")
plan.initialize_params(agent_registry=self.agent_registry)
self.plan_history.append(plan)
return plan
def refine_plan_dm(self, refined_plan) -> PlanDAG:
"""
Refines the existing plan using direct manipulation.
Args:
refined_plan (PlanDAG): The refined plan after direct manipulation.
Returns:
PlanDAG: The updated plan with modifications.
"""
self.plan_history.append(refined_plan)
pass
def get_latest_plan(self) -> PlanDAG:
"""Retrieves the most recent plan if available."""
return self.plan_history[-1] if self.plan_history else None
def clear(self) -> None:
"""
Clears the plan history.
"""
self.plan_history = []
def _llm_planner(self, query: str) -> LLMPlan:
"""
Uses LLM to generate a plan based on the user query.
Args:
query (str): The user query to generate the plan.
Returns:
LLMPlan: The generated plan in LLM format.
"""
messages = [
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": query},
]
response = self.client.beta.chat.completions.parse(
messages=messages, **self.config
)
response_obj = json.loads(response.choices[0].message.content)
return response_obj
def _llm_refiner(self, prev_plan, feedback):
"""
Refines the existing plan using LLM based on user feedback.
Args:
prev_plan (LLMPlan): The previous plan to refine.
feedback (str): User-provided feedback in natural language.
Returns:
LLMPlan: The refined plan in LLM format.
"""
messages = [
{"role": "system", "content": self.system_prompt},
{
"role": "user",
"content": self.refine_prompt.format(
prev_plan=prev_plan, feedback=feedback
),
},
]
response = self.client.beta.chat.completions.parse(
messages=messages, **self.config
)
response_obj = json.loads(response.choices[0].message.content)
return response_obj
def _llm_fixer(self, query, plan):
"""
Fixes an initial or incomplete plan using LLM.
Args:
query (str): The initial user query.
plan (dict): The incomplete or incorrect plan.
Returns:
LLMPlan: The corrected plan in LLM format.
"""
messages = [
{"role": "system", "content": self.system_prompt},
{
"role": "user",
"content": self.fix_plan_prompt.format(
query=query, plan=plan
),
},
]
response = self.client.beta.chat.completions.parse(
messages=messages, **self.config
)
response_obj = json.loads(response.choices[0].message.content)
return response_obj