prompt.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import json
  2. from typing import List, Tuple, Dict, Type
  3. from opendevin.plan import Plan
  4. from opendevin.action import Action, action_from_dict
  5. from opendevin.observation import Observation
  6. from opendevin.schema import ActionType
  7. from opendevin.logger import opendevin_logger as logger
  8. from opendevin.action import (
  9. NullAction,
  10. CmdRunAction,
  11. CmdKillAction,
  12. BrowseURLAction,
  13. FileReadAction,
  14. FileWriteAction,
  15. AgentRecallAction,
  16. AgentThinkAction,
  17. AgentFinishAction,
  18. AgentSummarizeAction,
  19. AddTaskAction,
  20. ModifyTaskAction,
  21. )
  22. from opendevin.observation import (
  23. NullObservation,
  24. )
  25. ACTION_TYPE_TO_CLASS: Dict[str, Type[Action]] = {
  26. ActionType.RUN: CmdRunAction,
  27. ActionType.KILL: CmdKillAction,
  28. ActionType.BROWSE: BrowseURLAction,
  29. ActionType.READ: FileReadAction,
  30. ActionType.WRITE: FileWriteAction,
  31. ActionType.RECALL: AgentRecallAction,
  32. ActionType.THINK: AgentThinkAction,
  33. ActionType.SUMMARIZE: AgentSummarizeAction,
  34. ActionType.FINISH: AgentFinishAction,
  35. ActionType.ADD_TASK: AddTaskAction,
  36. ActionType.MODIFY_TASK: ModifyTaskAction,
  37. }
  38. HISTORY_SIZE = 10
  39. prompt = """
  40. # Task
  41. You're a diligent software engineer AI. You can't see, draw, or interact with a
  42. browser, but you can read and write files, and you can run commands, and you can think.
  43. You've been given the following task:
  44. %(task)s
  45. ## Plan
  46. As you complete this task, you're building a plan and keeping
  47. track of your progress. Here's a JSON representation of your plan:
  48. %(plan)s
  49. %(plan_status)s
  50. You're responsible for managing this plan and the status of tasks in
  51. it, by using the `add_task` and `modify_task` actions described below.
  52. If the History below contradicts the state of any of these tasks, you
  53. MUST modify the task using the `modify_task` action described below.
  54. Be sure NOT to duplicate any tasks. Do NOT use the `add_task` action for
  55. a task that's already represented. Every task must be represented only once.
  56. Tasks that are sequential MUST be siblings. They must be added in order
  57. to their parent task.
  58. If you mark a task as 'completed', 'verified', or 'abandoned',
  59. all non-abandoned subtasks will be marked the same way.
  60. So before closing a task this way, you MUST not only be sure that it has
  61. been completed successfully--you must ALSO be sure that all its subtasks
  62. are ready to be marked the same way.
  63. If, and only if, ALL tasks have already been marked verified,
  64. you MUST respond with the `finish` action.
  65. ## History
  66. Here is a recent history of actions you've taken in service of this plan,
  67. as well as observations you've made. This only includes the MOST RECENT
  68. ten actions--more happened before that.
  69. %(history)s
  70. Your most recent action is at the bottom of that history.
  71. ## Action
  72. What is your next thought or action? Your response must be in JSON format.
  73. It must be an object, and it must contain two fields:
  74. * `action`, which is one of the actions below
  75. * `args`, which is a map of key-value pairs, specifying the arguments for that action
  76. * `read` - reads the content of a file. Arguments:
  77. * `path` - the path of the file to read
  78. * `write` - writes the content to a file. Arguments:
  79. * `path` - the path of the file to write
  80. * `content` - the content to write to the file
  81. * `run` - runs a command on the command line in a Linux shell. Arguments:
  82. * `command` - the command to run
  83. * `background` - if true, run the command in the background, so that other commands can be run concurrently. Useful for e.g. starting a server. You won't be able to see the logs. You don't need to end the command with `&`, just set this to true.
  84. * `kill` - kills a background command
  85. * `id` - the ID of the background command to kill
  86. * `browse` - opens a web page. Arguments:
  87. * `url` - the URL to open
  88. * `think` - make a plan, set a goal, or record your thoughts. Arguments:
  89. * `thought` - the thought to record
  90. * `add_task` - add a task to your plan. Arguments:
  91. * `parent` - the ID of the parent task
  92. * `goal` - the goal of the task
  93. * `subtasks` - a list of subtasks, each of which is a map with a `goal` key.
  94. * `modify_task` - close a task. Arguments:
  95. * `id` - the ID of the task to close
  96. * `state` - set to 'in_progress' to start the task, 'completed' to finish it, 'verified' to assert that it was successful, 'abandoned' to give up on it permanently, or `open` to stop working on it for now.
  97. * `finish` - if ALL of your tasks and subtasks have been verified or abandoned, and you're absolutely certain that you've completed your task and have tested your work, use the finish action to stop working.
  98. You MUST take time to think in between read, write, run, browse, and recall actions.
  99. You should never act twice in a row without thinking. But if your last several
  100. actions are all `think` actions, you should consider taking a different action.
  101. What is your next thought or action? Again, you must reply with JSON, and only with JSON.
  102. %(hint)s
  103. """
  104. def get_hint(latest_action_id: str) -> str:
  105. """ Returns action type hint based on given action_id """
  106. hints = {
  107. '': "You haven't taken any actions yet. Start by using `ls` to check out what files you're working with.",
  108. ActionType.RUN: 'You should think about the command you just ran, what output it gave, and how that affects your plan.',
  109. ActionType.READ: 'You should think about the file you just read, what you learned from it, and how that affects your plan.',
  110. ActionType.WRITE: 'You just changed a file. You should think about how it affects your plan.',
  111. ActionType.BROWSE: 'You should think about the page you just visited, and what you learned from it.',
  112. ActionType.THINK: "Look at your last thought in the history above. What does it suggest? Don't think anymore--take action.",
  113. ActionType.RECALL: 'You should think about the information you just recalled, and how it should affect your plan.',
  114. ActionType.ADD_TASK: 'You should think about the next action to take.',
  115. ActionType.MODIFY_TASK: 'You should think about the next action to take.',
  116. ActionType.SUMMARIZE: '',
  117. ActionType.FINISH: '',
  118. }
  119. return hints.get(latest_action_id, '')
  120. def get_prompt(plan: Plan, history: List[Tuple[Action, Observation]]) -> str:
  121. """
  122. Gets the prompt for the planner agent.
  123. Formatted with the most recent action-observation pairs, current task, and hint based on last action
  124. Parameters:
  125. - plan (Plan): The original plan outlined by the user with LLM defined tasks
  126. - history (List[Tuple[Action, Observation]]): List of corresponding action-observation pairs
  127. Returns:
  128. - str: The formatted string prompt with historical values
  129. """
  130. plan_str = json.dumps(plan.task.to_dict(), indent=2)
  131. sub_history = history[-HISTORY_SIZE:]
  132. history_dicts = []
  133. latest_action: Action = NullAction()
  134. for action, observation in sub_history:
  135. if not isinstance(action, NullAction):
  136. history_dicts.append(action.to_memory())
  137. latest_action = action
  138. if not isinstance(observation, NullObservation):
  139. observation_dict = observation.to_memory()
  140. if (
  141. 'extras' in observation_dict
  142. and 'screenshot' in observation_dict['extras']
  143. ):
  144. del observation_dict['extras']['screenshot']
  145. history_dicts.append(observation_dict)
  146. history_str = json.dumps(history_dicts, indent=2)
  147. current_task = plan.get_current_task()
  148. if current_task is not None:
  149. plan_status = f"You're currently working on this task:\n{current_task.goal}."
  150. if len(current_task.subtasks) == 0:
  151. plan_status += "\nIf it's not achievable AND verifiable with a SINGLE action, you MUST break it down into subtasks NOW."
  152. else:
  153. plan_status = "You're not currently working on any tasks. Your next action MUST be to mark a task as in_progress."
  154. hint = get_hint(latest_action.to_dict()['action'])
  155. logger.info('HINT:\n' + hint, extra={'msg_type': 'INFO'})
  156. return prompt % {
  157. 'task': plan.main_goal,
  158. 'plan': plan_str,
  159. 'history': history_str,
  160. 'hint': hint,
  161. 'plan_status': plan_status,
  162. }
  163. def parse_response(response: str) -> Action:
  164. """
  165. Parses the model output to find a valid action to take
  166. Parameters:
  167. - response (str): A response from the model that potentially contains an Action.
  168. Returns:
  169. - Action: A valid next action to perform from model output
  170. """
  171. json_start = response.find('{')
  172. json_end = response.rfind('}') + 1
  173. response = response[json_start:json_end]
  174. action_dict = json.loads(response)
  175. if 'contents' in action_dict:
  176. # The LLM gets confused here. Might as well be robust
  177. action_dict['content'] = action_dict.pop('contents')
  178. action = action_from_dict(action_dict)
  179. return action