🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
Merge branch 'main' into feature/json_write_prd
femto7b34c433cde6
diff --cc metagpt/utils/common.pyindex 9add0db8f,d0ab7e81d..65cc15e82--- a/metagpt/utils/common.py+++ b/metagpt/utils/common.py@@@ -150,8 -151,56 +150,55 @@@ class OutputParserparsed_data[block] = contentreturn parsed_data+ @classmethod+ def extract_struct(cls, text: str, data_type: Union[type(list), type(dict)]) -> Union[list, dict]:+ """Extracts and parses a specified type of structure (dictionary or list) from the given text.+ The text only contains a list or dictionary, which may have nested structures.++ Args:+ text: The text containing the structure (dictionary or list).+ data_type: The data type to extract, can be "list" or "dict".++ Returns:+ - If extraction and parsing are successful, it returns the corresponding data structure (list or dictionary).+ - If extraction fails or parsing encounters an error, it throw an exception.++ Examples:+ >>> text = 'xxx [1, 2, ["a", "b", [3, 4]], {"x": 5, "y": [6, 7]}] xxx'+ >>> result_list = OutputParser.extract_struct(text, "list")+ >>> print(result_list)+ >>> # Output: [1, 2, ["a", "b", [3, 4]], {"x": 5, "y": [6, 7]}]++ >>> text = 'xxx {"x": 1, "y": {"a": 2, "b": {"c": 3}}} xxx'+ >>> result_dict = OutputParser.extract_struct(text, "dict")+ >>> print(result_dict)+ >>> # Output: {"x": 1, "y": {"a": 2, "b": {"c": 3}}}+ """+ # Find the first "[" or "{" and the last "]" or "}"+ start_index = text.find("[" if data_type is list else "{")+ end_index = text.rfind("]" if data_type is list else "}")++ if start_index != -1 and end_index != -1:+ # Extract the structure part+ structure_text = text[start_index:end_index + 1]++ try:+ # Attempt to convert the text to a Python data type using ast.literal_eval+ result = ast.literal_eval(structure_text)++ # Ensure the result matches the specified data type+ if isinstance(result, list) or isinstance(result, dict):+ return result++ raise ValueError(f"The extracted structure is not a {data_type}.")++ except (ValueError, SyntaxError) as e:+ raise Exception(f"Error while extracting and parsing the {data_type}: {e}")+ else:+ raise Exception(f"No {data_type} found in the text.")+class CodeParser:-@classmethoddef parse_block(cls, block: str, text: str) -> str:blocks = cls.parse_blocks(text)diff --cc requirements.txtindex 24f539a98,2e483a050..3a75f924c--- a/requirements.txt+++ b/requirements.txt@@@ -38,4 -38,5 +38,7 @@@ typing-inspect==0.8.typing_extensions==4.5.0libcst==1.0.1qdrant-client==1.4.0- pytest-mock==3.11.1++pytest-mock==3.11.1+ open-interpreter==0.1.3+ ta==0.10.2++