helper.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import os
  2. import re
  3. from opendevin.events.action import CmdRunAction, MessageAction
  4. def analysis_size(size_str):
  5. size_str = size_str.strip()
  6. avails = {
  7. 'B': 1,
  8. 'Byte': 1,
  9. 'K': 1024,
  10. 'KB': 1024,
  11. 'M': 1024 * 1024,
  12. 'MB': 1024 * 1024,
  13. 'G': 1024 * 1024 * 1024,
  14. 'GB': 1024 * 1024 * 1024,
  15. 'T': 1024 * 1024 * 1024 * 1024,
  16. 'TB': 1024 * 1024 * 1024 * 1024,
  17. 'P': 1024 * 1024 * 1024 * 1024 * 1024,
  18. 'PB': 1024 * 1024 * 1024 * 1024 * 1024,
  19. }
  20. for size_unit in avails:
  21. if size_str.endswith(size_unit):
  22. return int(size_str[: -len(size_unit)]) * avails[size_unit]
  23. return int(size_str)
  24. def compare_results(check_method: str, model_answer: str, final_ans: str) -> bool:
  25. try:
  26. match check_method:
  27. case 'check/integer-match.py':
  28. return int(model_answer) == int(final_ans)
  29. case 'check/size-match.py':
  30. return analysis_size(model_answer) == analysis_size(final_ans)
  31. return (
  32. model_answer.replace('\r\n', '\n').replace('\r', '\n').strip()
  33. == final_ans.replace('\r\n', '\n').replace('\r', '\n').strip()
  34. )
  35. except Exception:
  36. return False
  37. def create_sh_file(filename: str, cmds: str) -> None:
  38. with open(filename, 'w', encoding='utf-8') as file:
  39. file.write(cmds.replace('\r\n', '\n'))
  40. os.chmod(filename, 0o755)
  41. def try_parse_answer(act) -> str | None:
  42. raw_ans = ''
  43. if isinstance(act, MessageAction) and act.source == 'agent':
  44. raw_ans = act.content
  45. elif isinstance(act, CmdRunAction) and act.source == 'agent':
  46. raw_ans = act.thought
  47. else:
  48. return None
  49. agent_answer = re.findall(r'<solution>(.*?)</solution>', raw_ans)
  50. if not agent_answer:
  51. return None
  52. return agent_answer[0].strip()