Skip to content

Commit

Permalink
Fix code parsing (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultmaster authored Dec 14, 2023
1 parent 9a4d670 commit 335cca5
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions coml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,27 @@ def parse_fix(response: str) -> tuple[str, str, str]:


def parse_code(response: str) -> str:
match = re.search(r"```.*\n([\s\S]+?)\n```", response)
match2 = re.search(r"```.*\n([\s\S]+)", response)
if match is not None:
code = match.group(1)
elif match2 is not None:
code = match2.group(1)
else:
# Give up. Return whole response.
warnings.warn("Unable to parse the code from response.")
code = response
return code
patterns = [
r"```.*\n([\s\S]+?)\n```",
r"```.*\n([\s\S]+?)```",
r"```([\s\S]+?)```",
r"```.*\n([\s\S]+)",
r"```([\s\S]+)\n```",
r"```([\s\S]+)```",
r"(.*)",
]
for index, pattern in enumerate(patterns):
match = re.search(pattern, response)
if match is not None:
if index > 0:
warnings.warn(
f"Unable to parse the code perfectly from response. "
f"Using pattern {pattern}."
)
return match.group(1)
# Give up. Return whole response.
warnings.warn("Unable to parse the code from response.")
return response


class CoMLAgent:
Expand Down

0 comments on commit 335cca5

Please sign in to comment.