action_parser.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from abc import ABC, abstractmethod
  2. from openhands.events.action import Action
  3. class ResponseParser(ABC):
  4. """This abstract base class is a general interface for an response parser dedicated to
  5. parsing the action from the response from the LLM.
  6. """
  7. def __init__(
  8. self,
  9. ):
  10. # Need pay attention to the item order in self.action_parsers
  11. self.action_parsers = []
  12. @abstractmethod
  13. def parse(self, response: str) -> Action:
  14. """Parses the action from the response from the LLM.
  15. Parameters:
  16. - response (str): The response from the LLM.
  17. Returns:
  18. - action (Action): The action parsed from the response.
  19. """
  20. pass
  21. @abstractmethod
  22. def parse_response(self, response) -> str:
  23. """Parses the action from the response from the LLM.
  24. Parameters:
  25. - response (str): The response from the LLM.
  26. Returns:
  27. - action_str (str): The action str parsed from the response.
  28. """
  29. pass
  30. @abstractmethod
  31. def parse_action(self, action_str: str) -> Action:
  32. """Parses the action from the response from the LLM.
  33. Parameters:
  34. - action_str (str): The response from the LLM.
  35. Returns:
  36. - action (Action): The action parsed from the response.
  37. """
  38. pass
  39. class ActionParser(ABC):
  40. """This abstract base class is a general interface for an action parser dedicated to
  41. parsing the action from the action str from the LLM.
  42. """
  43. @abstractmethod
  44. def check_condition(self, action_str: str) -> bool:
  45. """Check if the action string can be parsed by this parser."""
  46. pass
  47. @abstractmethod
  48. def parse(self, action_str: str) -> Action:
  49. """Parses the action from the action string from the LLM response."""
  50. pass