mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-08 15:05:17 +02:00
update CodeParser.parse_code.
This commit is contained in:
parent
31813f2512
commit
a06d8023d6
3 changed files with 4 additions and 3 deletions
|
|
@ -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)}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue