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

Improve markdown list parser #15295

Merged
merged 2 commits into from
Dec 29, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions libs/core/langchain_core/output_parsers/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,18 @@ def _type(self) -> str:
class MarkdownListOutputParser(ListOutputParser):
"""Parse a markdown list."""

pattern = r"-\s([^\n]+)"
pattern = r"^\s*[-*]\s([^\n]+)$"

def get_format_instructions(self) -> str:
return "Your response should be a markdown list, " "eg: `- foo\n- bar\n- baz`"

def parse(self, text: str) -> List[str]:
"""Parse the output of an LLM call."""
return re.findall(self.pattern, text)
return re.findall(self.pattern, text, re.MULTILINE)

def parse_iter(self, text: str) -> Iterator[re.Match]:
"""Parse the output of an LLM call."""
return re.finditer(self.pattern, text)
return re.finditer(self.pattern, text, re.MULTILINE)

@property
def _type(self) -> str:
Expand Down
1 change: 1 addition & 0 deletions libs/core/tests/unit_tests/output_parsers/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
NO_TICKS_WHITE_SPACE,
TEXT_BEFORE,
TEXT_AFTER,
TEXT_BEFORE_AND_AFTER,
]


Expand Down
6 changes: 3 additions & 3 deletions libs/core/tests/unit_tests/output_parsers/test_list_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_numbered_list() -> None:
"For example: \n\n1. foo\n\n2. bar\n\n3. baz"
)

text2 = "Items:\n\n1. apple\n\n2. banana\n\n3. cherry"
text2 = "Items:\n\n1. apple\n\n 2. banana\n\n3. cherry"

text3 = "No items in the list."

Expand Down Expand Up @@ -82,11 +82,11 @@ def test_numbered_list() -> None:
def test_markdown_list() -> None:
parser = MarkdownListOutputParser()
text1 = (
"Your response should be a numbered list with each item on a new line."
"Your response should be a numbered - not a list item - list with each item on a new line." # noqa: E501
"For example: \n- foo\n- bar\n- baz"
)

text2 = "Items:\n- apple\n- banana\n- cherry"
text2 = "Items:\n- apple\n - banana\n- cherry"

text3 = "No items in the list."

Expand Down