From b9663cebbd1884099de2b48540dd742918ed9788 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=A3=92=E6=A3=92?= Date: Wed, 31 Jan 2024 21:00:02 +0800 Subject: [PATCH] fix parse_code bug. --- metagpt/utils/common.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index 543c627a3..ec20223b8 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -265,19 +265,22 @@ class CodeParser: return block_dict @classmethod - def parse_code(cls, block: str, text: str, lang: str = "", start_ends: str = r'["\'`]{3}') -> str: + def parse_code(cls, block: str, text: str, lang: str = "") -> str: if block: text = cls.parse_block(block, text) - pattern = rf"{start_ends}{lang}.*?\s+(.*?){start_ends}" - match = re.search(pattern, text, re.DOTALL) - if match: - code = match.group(1) - else: - logger.error(f"{pattern} not match following text:") - logger.error(text) - # raise Exception - return text # just assume original text is code - return code + start_ends = ["```", '"""', "'''"] + patterns = [] + for start_end in start_ends: + pattern = rf"{start_end}{lang}.*?\s+(.*?){start_end}" + match = re.search(pattern, text, re.DOTALL) + if match: + code = match.group(1) + return code + patterns.append(pattern) + logger.error(f"{patterns} not match following text:") + logger.error(text) + # raise Exception + return text # just assume original text is code @classmethod def parse_str(cls, block: str, text: str, lang: str = ""):