CoolFace
Apppublic

coding-alt/AutoGPT

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
json_tests.py115 linesDownload Raw Back to unit
1import unittest2 3from autogpt.json_utils.json_fix_llm import fix_and_parse_json4 5 6class TestParseJson(unittest.TestCase):7    def test_valid_json(self):8        # Test that a valid JSON string is parsed correctly9        json_str = '{"name": "John", "age": 30, "city": "New York"}'10        obj = fix_and_parse_json(json_str)11        self.assertEqual(obj, {"name": "John", "age": 30, "city": "New York"})12 13    def test_invalid_json_minor(self):14        # Test that an invalid JSON string can be fixed with gpt15        json_str = '{"name": "John", "age": 30, "city": "New York",}'16        self.assertEqual(17            fix_and_parse_json(json_str, try_to_fix_with_gpt=False),18            {"name": "John", "age": 30, "city": "New York"},19        )20 21    def test_invalid_json_major_with_gpt(self):22        # Test that an invalid JSON string raises an error when try_to_fix_with_gpt is False23        json_str = 'BEGIN: "name": "John" - "age": 30 - "city": "New York" :END'24        self.assertEqual(25            fix_and_parse_json(json_str, try_to_fix_with_gpt=True),26            {"name": "John", "age": 30, "city": "New York"},27        )28 29    def test_invalid_json_major_without_gpt(self):30        # Test that a REALLY invalid JSON string raises an error when try_to_fix_with_gpt is False31        json_str = 'BEGIN: "name": "John" - "age": 30 - "city": "New York" :END'32        # Assert that this raises an exception:33        with self.assertRaises(Exception):34            fix_and_parse_json(json_str, try_to_fix_with_gpt=False)35 36    def test_invalid_json_leading_sentence_with_gpt(self):37        # Test that a REALLY invalid JSON string raises an error when try_to_fix_with_gpt is False38        json_str = """I suggest we start by browsing the repository to find any issues that we can fix.39 40{41    "command": {42        "name": "browse_website",43        "args":{44            "url": "https://github.com/Torantulino/Auto-GPT"45        }46    },47    "thoughts":48    {49        "text": "I suggest we start browsing the repository to find any issues that we can fix.",50        "reasoning": "Browsing the repository will give us an idea of the current state of the codebase and identify any issues that we can address to improve the repo.",51        "plan": "- Look through the repository to find any issues.\n- Investigate any issues to determine what needs to be fixed\n- Identify possible solutions to fix the issues\n- Open Pull Requests with fixes",52        "criticism": "I should be careful while browsing so as not to accidentally introduce any new bugs or issues.",53        "speak": "I will start browsing the repository to find any issues we can fix."54    }55}"""56        good_obj = {57            "command": {58                "name": "browse_website",59                "args": {"url": "https://github.com/Torantulino/Auto-GPT"},60            },61            "thoughts": {62                "text": "I suggest we start browsing the repository to find any issues that we can fix.",63                "reasoning": "Browsing the repository will give us an idea of the current state of the codebase and identify any issues that we can address to improve the repo.",64                "plan": "- Look through the repository to find any issues.\n- Investigate any issues to determine what needs to be fixed\n- Identify possible solutions to fix the issues\n- Open Pull Requests with fixes",65                "criticism": "I should be careful while browsing so as not to accidentally introduce any new bugs or issues.",66                "speak": "I will start browsing the repository to find any issues we can fix.",67            },68        }69        # Assert that this raises an exception:70        self.assertEqual(71            fix_and_parse_json(json_str, try_to_fix_with_gpt=False), good_obj72        )73 74    def test_invalid_json_leading_sentence_with_gpt(self):75        # Test that a REALLY invalid JSON string raises an error when try_to_fix_with_gpt is False76        json_str = """I will first need to browse the repository (https://github.com/Torantulino/Auto-GPT) and identify any potential bugs that need fixing. I will use the "browse_website" command for this.77 78{79    "command": {80        "name": "browse_website",81        "args":{82            "url": "https://github.com/Torantulino/Auto-GPT"83        }84    },85    "thoughts":86    {87        "text": "Browsing the repository to identify potential bugs",88        "reasoning": "Before fixing bugs, I need to identify what needs fixing. I will use the 'browse_website' command to analyze the repository.",89        "plan": "- Analyze the repository for potential bugs and areas of improvement",90        "criticism": "I need to ensure I am thorough and pay attention to detail while browsing the repository.",91        "speak": "I am browsing the repository to identify potential bugs."92    }93}"""94        good_obj = {95            "command": {96                "name": "browse_website",97                "args": {"url": "https://github.com/Torantulino/Auto-GPT"},98            },99            "thoughts": {100                "text": "Browsing the repository to identify potential bugs",101                "reasoning": "Before fixing bugs, I need to identify what needs fixing. I will use the 'browse_website' command to analyze the repository.",102                "plan": "- Analyze the repository for potential bugs and areas of improvement",103                "criticism": "I need to ensure I am thorough and pay attention to detail while browsing the repository.",104                "speak": "I am browsing the repository to identify potential bugs.",105            },106        }107        # Assert that this raises an exception:108        self.assertEqual(109            fix_and_parse_json(json_str, try_to_fix_with_gpt=False), good_obj110        )111 112 113if __name__ == "__main__":114    unittest.main()115