agent.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from openhands.agenthub.planner_agent.prompt import get_prompt_and_images
  2. from openhands.agenthub.planner_agent.response_parser import PlannerResponseParser
  3. from openhands.controller.agent import Agent
  4. from openhands.controller.state.state import State
  5. from openhands.core.config import AgentConfig
  6. from openhands.core.message import ImageContent, Message, TextContent
  7. from openhands.events.action import Action, AgentFinishAction
  8. from openhands.llm.llm import LLM
  9. class PlannerAgent(Agent):
  10. VERSION = '1.0'
  11. """
  12. The planner agent utilizes a special prompting strategy to create long term plans for solving problems.
  13. The agent is given its previous action-observation pairs, current task, and hint based on last action taken at every step.
  14. """
  15. response_parser = PlannerResponseParser()
  16. def __init__(self, llm: LLM, config: AgentConfig):
  17. """Initialize the Planner Agent with an LLM
  18. Parameters:
  19. - llm (LLM): The llm to be used by this agent
  20. """
  21. super().__init__(llm, config)
  22. def step(self, state: State) -> Action:
  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 state.root_task.state in [
  32. 'completed',
  33. 'verified',
  34. 'abandoned',
  35. ]:
  36. return AgentFinishAction()
  37. prompt, image_urls = get_prompt_and_images(
  38. state, self.llm.config.max_message_chars
  39. )
  40. content = [TextContent(text=prompt)]
  41. if self.llm.vision_is_active() and image_urls:
  42. content.append(ImageContent(image_urls=image_urls))
  43. message = Message(role='user', content=content)
  44. resp = self.llm.completion(messages=self.llm.format_messages_for_llm(message))
  45. return self.response_parser.parse(resp)