action_parser.py 1.9 KB

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