agent.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from opendevin.controller.agent import Agent
  2. from opendevin.controller.state.state import State
  3. from opendevin.events.action import Action, AgentDelegateAction, AgentFinishAction
  4. from opendevin.events.observation import AgentDelegateObservation
  5. from opendevin.llm.llm import LLM
  6. class DelegatorAgent(Agent):
  7. """
  8. The planner agent utilizes a special prompting strategy to create long term plans for solving problems.
  9. The agent is given its previous action-observation pairs, current task, and hint based on last action taken at every step.
  10. """
  11. current_delegate: str = ''
  12. def __init__(self, llm: LLM):
  13. """
  14. Initialize the Delegator Agent with an LLM
  15. Parameters:
  16. - llm (LLM): The llm to be used by this agent
  17. """
  18. super().__init__(llm)
  19. def step(self, state: State) -> Action:
  20. """
  21. Checks to see if current step is completed, returns AgentFinishAction if True.
  22. Otherwise, creates a plan prompt and sends to model for inference, returning the result as the next action.
  23. Parameters:
  24. - state (State): The current state given the previous actions and observations
  25. Returns:
  26. - AgentFinishAction: If the last state was 'completed', 'verified', or 'abandoned'
  27. - Action: The next action to take based on llm response
  28. """
  29. if self.current_delegate == '':
  30. self.current_delegate = 'study'
  31. task = state.get_current_user_intent()
  32. return AgentDelegateAction(
  33. agent='StudyRepoForTaskAgent', inputs={'task': task}
  34. )
  35. last_observation = state.history[-1][1]
  36. if not isinstance(last_observation, AgentDelegateObservation):
  37. raise Exception('Last observation is not an AgentDelegateObservation')
  38. goal = state.get_current_user_intent()
  39. if self.current_delegate == 'study':
  40. self.current_delegate = 'coder'
  41. return AgentDelegateAction(
  42. agent='CoderAgent',
  43. inputs={
  44. 'task': goal,
  45. 'summary': last_observation.outputs['summary'],
  46. },
  47. )
  48. elif self.current_delegate == 'coder':
  49. self.current_delegate = 'verifier'
  50. return AgentDelegateAction(
  51. agent='VerifierAgent',
  52. inputs={
  53. 'task': goal,
  54. },
  55. )
  56. elif self.current_delegate == 'verifier':
  57. if (
  58. 'completed' in last_observation.outputs
  59. and last_observation.outputs['completed']
  60. ):
  61. return AgentFinishAction()
  62. else:
  63. self.current_delegate = 'coder'
  64. return AgentDelegateAction(
  65. agent='CoderAgent',
  66. inputs={
  67. 'task': goal,
  68. 'summary': last_observation.outputs['summary'],
  69. },
  70. )
  71. else:
  72. raise Exception('Invalid delegate state')
  73. def search_memory(self, query: str) -> list[str]:
  74. return []