Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code block parser: only consider triple-backticks if at start of line #1026

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions core/llm/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class MultiCodeBlockParser:
"""

def __init__(self):
# FIXME: ``` should be the only content on the line`
self.pattern = re.compile(r"```([a-z0-9]+\n)?(.*?)```\s*", re.DOTALL)
self.pattern = re.compile(r"^```([a-z0-9]+\n)?(.*?)^```\s*", re.DOTALL | re.MULTILINE)

def __call__(self, text: str) -> list[str]:
blocks = []
Expand Down
5 changes: 3 additions & 2 deletions tests/llm/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
("```\n```", [""]),
("```py\n```", [""]),
("```py\nsome code\n```", ["some code"]),
("```py\nsome ``` code\n```", ["some ``` code"]),
(
"some text preamble\n" "```\nsome code\n```\n" "```py\nmore\ncode\n```\n" "some text conclusion",
["some code", "more\ncode"],
Expand Down Expand Up @@ -49,7 +50,7 @@ def test_code_block_parser(input, expected):
[
("{}", True, {}),
('{"a": 1}', True, {"a": 1}),
('```json\n{"a": 1, "b": "c"}```', True, {"a": 1, "b": "c"}),
('```json\n{"a": 1, "b": "c"}\n```', True, {"a": 1, "b": "c"}),
("", True, ValueError),
("", False, None),
("{bad json}", True, ValueError),
Expand Down Expand Up @@ -85,7 +86,7 @@ def test_parse_json_no_spec(input, strict, expected):
},
),
(
'```{"name": "John", "children": [{"age": 1, "name": "Jane", "geo": [1.0, 2.0]}]}```',
'```\n{"name": "John", "children": [{"age": 1, "name": "Jane", "geo": [1.0, 2.0]}]}\n```',
{
"name": "John",
"children": [
Expand Down
Loading