fix_with_patch.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from evaluator import *
  2. DESCRIPTION = "Test if the model can generate a .patch file to fix a bug in a given Python code."
  3. TAGS = ['code', 'fix', 'python']
  4. code = """import re
  5. class Tokenizer:
  6. def __init__(self, input_str):
  7. '''
  8. input_str: a string of digits and operators
  9. '''
  10. self.position = 0
  11. self.tokens = re.findall(r'\d+|(\+|\-|\*|/|sin|cos|\(|\))', input_str)
  12. def get_next_token(self):
  13. if self.position >= len(self.tokens):
  14. return None
  15. token = self.tokens[self.position]
  16. self.position += 1
  17. return token
  18. def peek_next_token(self):
  19. if self.position >= len(self.tokens):
  20. return None
  21. return self.tokens[self.position]
  22. """
  23. question = f"""
  24. Write a .patch file to stdout that will fix the bug in this tokenizer. Still keep the new file name the same after patching.
  25. I'll evaluate you based on my ability to do
  26. - echo [your patch] > fix.patch
  27. - patch < fix.patch
  28. - python3 run_tokenizer.py
  29. Here is the entirety of run_tokenizer.py:
  30. ```python
  31. {code}
  32. ```
  33. """
  34. def setup_fn():
  35. code = """import re
  36. class Tokenizer:
  37. def __init__(self, input_str):
  38. '''
  39. input_str: a string of digits and operators
  40. '''
  41. self.position = 0
  42. self.tokens = re.findall(r'\d+|(\+|\-|\*|/|sin|cos|\(|\))', input_str)
  43. def get_next_token(self):
  44. if self.position >= len(self.tokens):
  45. return None
  46. token = self.tokens[self.position]
  47. self.position += 1
  48. return token
  49. def peek_next_token(self):
  50. if self.position >= len(self.tokens):
  51. return None
  52. return self.tokens[self.position]"""
  53. import time
  54. open('run_tokenizer.py', 'w').write(code)
  55. time.sleep(.5)
  56. def check():
  57. import os
  58. import time
  59. os.system("patch < fix.patch")
  60. time.sleep(.5)
  61. import run_tokenizer
  62. return run_tokenizer.Tokenizer("cos(1)+sin(23)*5").tokens == ['cos', '(', '1', ')', '+', 'sin', '(', '23', ')', '*', '5']
  63. TestFixPatch = Setup(setup_fn) >> question >> LLMRun() >> ExtractCode(manual="Take the below answer to my question and return just the complete .patch in a single file so I can copy and paste it into an editor and apply it with the `patch` tool as-is. Include nothing else other than the patch. Here is the code: <A>") >> MakeFile("fix.patch") >> PyEvaluator(check)
  64. if __name__ == "__main__":
  65. print(run_test(TestFixPatch))