Skip to content

Commit 335cca5

Browse files
authored
Fix code parsing (microsoft#16)
1 parent 9a4d670 commit 335cca5

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

coml/core.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,27 @@ def parse_fix(response: str) -> tuple[str, str, str]:
6767

6868

6969
def parse_code(response: str) -> str:
70-
match = re.search(r"```.*\n([\s\S]+?)\n```", response)
71-
match2 = re.search(r"```.*\n([\s\S]+)", response)
72-
if match is not None:
73-
code = match.group(1)
74-
elif match2 is not None:
75-
code = match2.group(1)
76-
else:
77-
# Give up. Return whole response.
78-
warnings.warn("Unable to parse the code from response.")
79-
code = response
80-
return code
70+
patterns = [
71+
r"```.*\n([\s\S]+?)\n```",
72+
r"```.*\n([\s\S]+?)```",
73+
r"```([\s\S]+?)```",
74+
r"```.*\n([\s\S]+)",
75+
r"```([\s\S]+)\n```",
76+
r"```([\s\S]+)```",
77+
r"(.*)",
78+
]
79+
for index, pattern in enumerate(patterns):
80+
match = re.search(pattern, response)
81+
if match is not None:
82+
if index > 0:
83+
warnings.warn(
84+
f"Unable to parse the code perfectly from response. "
85+
f"Using pattern {pattern}."
86+
)
87+
return match.group(1)
88+
# Give up. Return whole response.
89+
warnings.warn("Unable to parse the code from response.")
90+
return response
8191

8292

8393
class CoMLAgent:

0 commit comments

Comments
 (0)