Skip to content
Open
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
14 changes: 8 additions & 6 deletions evoagentx/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,15 @@ def _parse_title_content(cls, content: str, title_format: str = "## {title}", **
if len(attr_types) == 0:
return {}

output_titles = [title_format.format(title=attr) for attr in attr_types.keys()]
output_titles = [
(title_format.format(title=attr), attr)
for attr in attr_types
]

def is_output_title(text: str):
for title in output_titles:
for title, attr in output_titles:
if text.strip().lower().startswith(title.lower()):
return True, title
return True, attr
return False, None

def process_lines(lines: List[str], datatype: str):
Expand All @@ -517,7 +520,7 @@ def process_lines(lines: List[str], datatype: str):
current_attr_name: str = None
current_attr_lines: list = None
for line in content.split("\n"):
is_title, title = is_output_title(line)
is_title, attr_name = is_output_title(line)
if is_title:
if current_attr_name is not None and current_attr_lines is not None:
# if we already have some content for a title, and now we reach a new title
Expand All @@ -527,8 +530,7 @@ def process_lines(lines: List[str], datatype: str):

# reset content for new title
current_attr_lines = []
current_attr_name = title.replace("#", "").strip()
output_titles.remove(title)
current_attr_name = attr_name
else:
if current_attr_lines is not None:
current_attr_lines.append(line)
Expand Down
28 changes: 28 additions & 0 deletions tests/src/models/test_base_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from evoagentx.models.base_model import LLMOutputParser


class RepeatedHeadingOutput(LLMOutputParser):
count: int
enabled: bool
label: str


def test_title_parser_uses_latest_value_for_repeated_headings():
content = """## count
1
## count
2
## enabled
false
## enabled
true
## label
alpha
## label
beta"""

result = RepeatedHeadingOutput.parse(content, parse_mode="title")

assert result.count == 2
assert result.enabled is True
assert result.label == "beta"