agent.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from opendevin.controller.agent import Agent
  2. from opendevin.controller.state.state import State
  3. from opendevin.events.action import Action, AgentFinishAction
  4. from opendevin.llm.llm import LLM
  5. from .prompt import get_prompt, parse_response
  6. class PlannerAgent(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. def __init__(self, llm: LLM):
  12. """
  13. Initialize the Planner Agent with an LLM
  14. Parameters:
  15. - llm (LLM): The llm to be used by this agent
  16. """
  17. super().__init__(llm)
  18. def step(self, state: State) -> Action:
  19. """
  20. Checks to see if current step is completed, returns AgentFinishAction if True.
  21. Otherwise, creates a plan prompt and sends to model for inference, returning the result as the next action.
  22. Parameters:
  23. - state (State): The current state given the previous actions and observations
  24. Returns:
  25. - AgentFinishAction: If the last state was 'completed', 'verified', or 'abandoned'
  26. - Action: The next action to take based on llm response
  27. """
  28. if state.root_task.state in [
  29. 'completed',
  30. 'verified',
  31. 'abandoned',
  32. ]:
  33. return AgentFinishAction()
  34. prompt = get_prompt(state)
  35. messages = [{'content': prompt, 'role': 'user'}]
  36. resp = self.llm.completion(messages=messages)
  37. action_resp = resp['choices'][0]['message']['content']
  38. state.num_of_chars += len(prompt) + len(action_resp)
  39. action = parse_response(action_resp)
  40. return action
  41. def search_memory(self, query: str) -> list[str]:
  42. return []