Преглед на файлове

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

* AgentHub.planner_agent.prompt: Optimize the get_prompt function by using a dictionary to map ActionType to hints, thus avoiding repetitions and making the code more readable and maintainable.

* Update prompt.py

* Fix Lint issues in prompt.py

* Fix lint issues in prompt.py

* Fix Lint issues in prompt.py

* Fix Lint issues prompt.py

* Remove trailing whitespace

---------

Co-authored-by: Alex Bäuerle <alex@a13x.io>
Co-authored-by: Robert Brennan <accounts@rbren.io>
Co-authored-by: Jim Su <jimsu@protonmail.com>
#Einswilli преди 1 година
родител
ревизия
157278704e
променени са 1 файла, в които са добавени 20 реда и са изтрити 30 реда
  1. 20 30
      agenthub/planner_agent/prompt.py

+ 20 - 30
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,