agent.py 3.1 KB

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