From cff4eff78d3d7b015f0cd49cd22e5dfe3276186d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=A3=92=E6=A3=92?= Date: Tue, 23 Jan 2024 17:46:41 +0800 Subject: [PATCH] update CodeParser.parse_code. --- metagpt/provider/openai_api.py | 2 +- metagpt/utils/common.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/metagpt/provider/openai_api.py b/metagpt/provider/openai_api.py index fc741f038..dad44087c 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, start_ends=r'["\'`]{3}') + code = CodeParser.parse_code(None, message.content) 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 36392debc..ed73cb061 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -8,6 +8,7 @@ Add generic class-to-string and object-to-string conversion functionality. @Modified By: mashenquan, 2023/11/27. Bug fix: `parse_recipient` failed to parse the recipient in certain GPT-3.5 responses. +@Modified By: liubangbang, 2024/01/23. Update: support [```, ''', \"\"\" ] codes in CodeParser.parse_code. """ from __future__ import annotations @@ -264,7 +265,7 @@ class CodeParser: return block_dict @classmethod - def parse_code(cls, block: str, text: str, lang: str = "", start_ends: str = "```") -> str: + def parse_code(cls, block: str, text: str, lang: str = "", start_ends: str = r'["\'`]{3}') -> str: if block: text = cls.parse_block(block, text) pattern = rf"{start_ends}{lang}.*?\s+(.*?){start_ends}"