agent.py 2.8 KB

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