response_parser.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from opendevin.controller.action_parser import ResponseParser
  2. from opendevin.core.utils import json
  3. from opendevin.events.action import (
  4. Action,
  5. )
  6. from opendevin.events.serialization.action import action_from_dict
  7. class MonologueResponseParser(ResponseParser):
  8. def __init__(
  9. self,
  10. ):
  11. pass
  12. def parse(self, response: str) -> Action:
  13. action_str = self.parse_response(response)
  14. return self.parse_action(action_str)
  15. def parse_response(self, response) -> str:
  16. # get the next action from the response
  17. return response['choices'][0]['message']['content']
  18. def parse_action(self, action_str: str) -> Action:
  19. """
  20. Parses a string to find an action within it
  21. Parameters:
  22. - response (str): The string to be parsed
  23. Returns:
  24. - Action: The action that was found in the response string
  25. """
  26. # attempt to load the JSON dict from the response
  27. action_dict = json.loads(action_str)
  28. if 'content' in action_dict:
  29. # The LLM gets confused here. Might as well be robust
  30. action_dict['contents'] = action_dict.pop('content')
  31. return action_from_dict(action_dict)