Skip to content

Commit

Permalink
Merge pull request #1026 from Pythagora-io/multiblock-parser-inline-b…
Browse files Browse the repository at this point in the history
…ackticks

code block parser: only consider triple-backticks if at start of line
  • Loading branch information
LeonOstrez authored Jun 20, 2024
2 parents ac5d077 + 3011acc commit 6fbfa8a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
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

0 comments on commit 6fbfa8a

Please sign in to comment.