fix_json.py 1.3 KB

1234567891011121314151617181920212223242526
  1. from evaluator import *
  2. DESCRIPTION = "Test if the model can fix broken JSON objects."
  3. TAGS = ['code', 'python']
  4. question = """Write me a python function called fix_json that fixes broken JSON objects, and returns the parsed object. I'll pass the JSON object as a string and it should fix:
  5. - Incorect parenthesis, e.g., {"a": (5, 4)} -> {"a": [5, 4]}
  6. - Incorrect single quotes, e.g., {'a': 5} -> {"a": 5}
  7. - False/True -> false/true
  8. """
  9. help_err = """Make sure not to break data inside strings! So if there's a True or a ( inside a string keep them the way they are. Also don't break quoted strings"""
  10. test_case, answer = make_python_test([("""fix_json('''{"a": 5, "b": [3, 4]}''')""", """{"a": 5, "b": [3, 4]}"""),
  11. ("""fix_json('''{'a': 5.0, 'b': (True, 4)}''')""", """{"a": 5.0, "b": [True, 4]}"""),
  12. ("""fix_json('''{'True': False, 'b()': (3, 4), "c'": []}''')""", """{"True": False, "b()": [3, 4], "c'": []}"""),
  13. ])
  14. TestFixJSON = question >> LLMRun() >> ExtractCode(keep_main=False) >> PythonRun(test_case) >> SubstringEvaluator(answer)
  15. TestFixJSONHelp = (question+help_err) >> LLMRun() >> ExtractCode(keep_main=False) >> PythonRun(test_case) >> SubstringEvaluator(answer)
  16. if __name__ == "__main__":
  17. print(run_test(TestFixJSONHelp))