Skip to content

🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming

update CodeParser.parse_code.

刘棒棒a06d8023d640
diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py
index dad44087c..fc741f038 100644
--- a/metagpt/provider/openai_api.py
+++ b/metagpt/provider/openai_api.py
@@ -251,7 +251,7 @@ class OpenAILLM(BaseLLM):
# reponse is code, fix openai tools_call respond bug.
code_formats = ("```", '"""', "'''")
if message.content.startswith(code_formats) and message.content.endswith(code_formats):
- code = CodeParser.parse_code(None, message.content)
+ code = CodeParser.parse_code(None, message.content, start_ends=r'["\'`]{3}')
return {"language": "python", "code": code}
# reponse is message
return {"language": "markdown", "code": self.get_choice_text(rsp)}
diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py
index b20b4acd2..36392debc 100644
--- a/metagpt/utils/common.py
+++ b/metagpt/utils/common.py
@@ -264,10 +264,10 @@ class CodeParser:
return block_dict
@classmethod
- def parse_code(cls, block: str, text: str, lang: str = "") -> str:
+ def parse_code(cls, block: str, text: str, lang: str = "", start_ends: str = "```") -> str:
if block:
text = cls.parse_block(block, text)
- pattern = rf"```{lang}.*?\s+(.*?)```"
+ pattern = rf"{start_ends}{lang}.*?\s+(.*?){start_ends}"
match = re.search(pattern, text, re.DOTALL)
if match:
code = match.group(1)
diff --git a/tests/metagpt/provider/test_openai.py b/tests/metagpt/provider/test_openai.py
index 7a771bcac..1743fed92 100644
--- a/tests/metagpt/provider/test_openai.py
+++ b/tests/metagpt/provider/test_openai.py
@@ -139,6 +139,7 @@ class TestOpenAI:
assert "code" in code
assert "language" in code
assert "hello world" in code["code"]
+ logger.info(f'code is : {code["code"]}')
if "Completed a python code for hello world!" == code["code"]:
code["language"] == "markdown"