response_parser.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import ast
  2. from openhands.controller.action_parser import ActionParser, ResponseParser
  3. from openhands.core.logger import openhands_logger as logger
  4. from openhands.events.action import (
  5. Action,
  6. BrowseInteractiveAction,
  7. )
  8. class BrowsingResponseParser(ResponseParser):
  9. def __init__(self):
  10. # Need to pay attention to the item order in self.action_parsers
  11. super().__init__()
  12. self.action_parsers = [BrowsingActionParserMessage()]
  13. self.default_parser = BrowsingActionParserBrowseInteractive()
  14. def parse(self, response: str) -> Action:
  15. action_str = self.parse_response(response)
  16. return self.parse_action(action_str)
  17. def parse_response(self, response) -> str:
  18. action_str = response['choices'][0]['message']['content']
  19. if action_str is None:
  20. return ''
  21. action_str = action_str.strip()
  22. if action_str and not action_str.endswith('```'):
  23. action_str = action_str + ')```'
  24. logger.debug(action_str)
  25. return action_str
  26. def parse_action(self, action_str: str) -> Action:
  27. for action_parser in self.action_parsers:
  28. if action_parser.check_condition(action_str):
  29. return action_parser.parse(action_str)
  30. return self.default_parser.parse(action_str)
  31. class BrowsingActionParserMessage(ActionParser):
  32. """Parser action:
  33. - BrowseInteractiveAction(browser_actions) - unexpected response format, message back to user
  34. """
  35. def __init__(
  36. self,
  37. ):
  38. pass
  39. def check_condition(self, action_str: str) -> bool:
  40. return '```' not in action_str
  41. def parse(self, action_str: str) -> Action:
  42. msg = f'send_msg_to_user("""{action_str}""")'
  43. return BrowseInteractiveAction(
  44. browser_actions=msg,
  45. thought=action_str,
  46. browsergym_send_msg_to_user=action_str,
  47. )
  48. class BrowsingActionParserBrowseInteractive(ActionParser):
  49. """Parser action:
  50. - BrowseInteractiveAction(browser_actions) - handle send message to user function call in BrowserGym
  51. """
  52. def __init__(
  53. self,
  54. ):
  55. pass
  56. def check_condition(self, action_str: str) -> bool:
  57. return True
  58. def parse(self, action_str: str) -> Action:
  59. thought = action_str.split('```')[0].strip()
  60. action_str = action_str.split('```')[1].strip()
  61. msg_content = ''
  62. for sub_action in action_str.split('\n'):
  63. if 'send_msg_to_user(' in sub_action:
  64. tree = ast.parse(sub_action)
  65. args = tree.body[0].value.args # type: ignore
  66. msg_content = args[0].value
  67. return BrowseInteractiveAction(
  68. browser_actions=action_str,
  69. thought=thought,
  70. browsergym_send_msg_to_user=msg_content,
  71. )