Skip to content

πŸ™Œ OpenHands: AI-Driven Development

style: Refactor get_prompt function for readability and maintainability (#1051)

#Einswilli157278704e12
diff --git a/agenthub/planner_agent/prompt.py b/agenthub/planner_agent/prompt.py
index 9ead741d6a..b925171f86 100644
--- a/agenthub/planner_agent/prompt.py
+++ b/agenthub/planner_agent/prompt.py
@@ -130,6 +130,25 @@ What is your next thought or action? Again, you must reply with JSON, and only w
"""
+def get_hint(latest_action_id: str) -> str:
+ """ Returns action type hint based on given action_id """
+
+ hints = {
+ '': 'You haven\'t taken any actions yet. Start by using `ls` to check out what files you\'re working with.',
+ ActionType.RUN: 'You should think about the command you just ran, what output it gave, and how that affects your plan.',
+ ActionType.READ: 'You should think about the file you just read, what you learned from it, and how that affects your plan.',
+ ActionType.WRITE: 'You just changed a file. You should think about how it affects your plan.',
+ ActionType.BROWSE: 'You should think about the page you just visited, and what you learned from it.',
+ ActionType.THINK: 'Look at your last thought in the history above. What does it suggest? Don\'t think anymore--take action.',
+ ActionType.RECALL: 'You should think about the information you just recalled, and how it should affect your plan.',
+ ActionType.ADD_TASK: 'You should think about the next action to take.',
+ ActionType.MODIFY_TASK: 'You should think about the next action to take.',
+ ActionType.SUMMARIZE: '',
+ ActionType.FINISH: '',
+ }
+ return hints.get(latest_action_id, '')
+
+
def get_prompt(plan: Plan, history: List[Tuple[Action, Observation]]) -> str:
"""
Gets the prompt for the planner agent.
@@ -160,8 +179,6 @@ def get_prompt(plan: Plan, history: List[Tuple[Action, Observation]]) -> str:
del observation_dict['extras']['screenshot']
history_dicts.append(observation_dict)
history_str = json.dumps(history_dicts, indent=2)
-
- hint = ''
current_task = plan.get_current_task()
if current_task is not None:
plan_status = f"You're currently working on this task:\n{current_task.goal}."
@@ -169,34 +186,7 @@ def get_prompt(plan: Plan, history: List[Tuple[Action, Observation]]) -> str:
plan_status += "\nIf it's not achievable AND verifiable with a SINGLE action, you MUST break it down into subtasks NOW."
else:
plan_status = "You're not currently working on any tasks. Your next action MUST be to mark a task as in_progress."
- hint = plan_status
-
- latest_action_id = latest_action.to_dict()['action']
-
- if current_task is not None:
- if latest_action_id == '':
- hint = "You haven't taken any actions yet. Start by using `ls` to check out what files you're working with."
- elif latest_action_id == ActionType.RUN:
- hint = 'You should think about the command you just ran, what output it gave, and how that affects your plan.'
- elif latest_action_id == ActionType.READ:
- hint = 'You should think about the file you just read, what you learned from it, and how that affects your plan.'
- elif latest_action_id == ActionType.WRITE:
- hint = 'You just changed a file. You should think about how it affects your plan.'
- elif latest_action_id == ActionType.BROWSE:
- hint = 'You should think about the page you just visited, and what you learned from it.'
- elif latest_action_id == ActionType.THINK:
- hint = "Look at your last thought in the history above. What does it suggest? Don't think anymore--take action."
- elif latest_action_id == ActionType.RECALL:
- hint = 'You should think about the information you just recalled, and how it should affect your plan.'
- elif latest_action_id == ActionType.ADD_TASK:
- hint = 'You should think about the next action to take.'
- elif latest_action_id == ActionType.MODIFY_TASK:
- hint = 'You should think about the next action to take.'
- elif latest_action_id == ActionType.SUMMARIZE:
- hint = ''
- elif latest_action_id == ActionType.FINISH:
- hint = ''
-
+ hint = get_hint(latest_action.to_dict()['action'])
logger.info('HINT:\n' + hint, extra={'msg_type': 'INFO'})
return prompt % {
'task': plan.main_goal,