update CodeParser.parse_code.

This commit is contained in:
刘棒棒 2024-01-23 17:36:19 +08:00
parent 31813f2512
commit a06d8023d6
3 changed files with 4 additions and 3 deletions

View file

@ -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)}

View file

@ -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)

View file

@ -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"