mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-11 15:15:18 +02:00
remove replace_content_between_markers function of RoleZeroContextBuilder and
update tests
This commit is contained in:
parent
1de01c1f24
commit
0a1c965933
2 changed files with 10 additions and 36 deletions
|
|
@ -1,7 +1,6 @@
|
|||
"""RoleZero context builder."""
|
||||
|
||||
import copy
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from metagpt.exp_pool.context_builders.base import BaseContextBuilder
|
||||
|
|
@ -32,32 +31,9 @@ class RoleZeroContextBuilder(BaseContextBuilder):
|
|||
|
||||
def replace_example_content(self, text: str, new_example_content: str) -> str:
|
||||
return self.replace_content_of_example_tag(text, new_example_content)
|
||||
# return self.replace_content_between_markers(text, "# Example", "# Instruction", new_example_content)
|
||||
|
||||
@staticmethod
|
||||
def replace_content_of_example_tag(self, text: str, new_example_content: str) -> str:
|
||||
def replace_content_of_example_tag(text: str, new_example_content: str) -> str:
|
||||
pattern = "# Past Experience\n<experience>"
|
||||
replaced_text = text.replace(pattern, "# Past Example\n" + new_example_content)
|
||||
return replaced_text
|
||||
|
||||
@staticmethod
|
||||
def replace_content_between_markers(text: str, start_marker: str, end_marker: str, new_content: str) -> str:
|
||||
"""Replace the content between `start_marker` and `end_marker` in the text with `new_content`.
|
||||
|
||||
Args:
|
||||
text (str): The original text.
|
||||
new_content (str): The new content to replace the old content.
|
||||
start_marker (str): The marker indicating the start of the content to be replaced, such as '# Example'.
|
||||
end_marker (str): The marker indicating the end of the content to be replaced, such as '# Instruction'.
|
||||
|
||||
Returns:
|
||||
str: The text with the content replaced.
|
||||
"""
|
||||
|
||||
pattern = re.compile(f"({start_marker}\n)(.*?)(\n{end_marker})", re.DOTALL)
|
||||
|
||||
def replacement(match):
|
||||
return f"{match.group(1)}{new_content}\n{match.group(3)}"
|
||||
|
||||
replaced_text = pattern.sub(replacement, text)
|
||||
replaced_text = text.replace(pattern, "# Past Experience\n" + new_example_content)
|
||||
return replaced_text
|
||||
|
|
|
|||
|
|
@ -30,22 +30,20 @@ class TestRoleZeroContextBuilder:
|
|||
assert result == [{"content": "Updated content"}]
|
||||
|
||||
def test_replace_example_content(self, context_builder, mocker):
|
||||
mocker.patch.object(RoleZeroContextBuilder, "replace_content_between_markers", return_value="Replaced content")
|
||||
mocker.patch.object(RoleZeroContextBuilder, "replace_content_of_example_tag", return_value="Replaced content")
|
||||
result = context_builder.replace_example_content("Original text", "New example content")
|
||||
assert result == "Replaced content"
|
||||
context_builder.replace_content_between_markers.assert_called_once_with(
|
||||
"Original text", "# Example", "# Instruction", "New example content"
|
||||
)
|
||||
context_builder.replace_content_of_example_tag.assert_called_once_with("Original text", "New example content")
|
||||
|
||||
def test_replace_content_between_markers(self):
|
||||
text = "Start\n# Example\nOld content\n# Instruction\nEnd"
|
||||
def test_replace_content_of_example_tag(self):
|
||||
text = "Start\n# Past Experience\n<experience>\n\n# Instruction\nEnd"
|
||||
new_content = "New content"
|
||||
result = RoleZeroContextBuilder.replace_content_between_markers(text, "# Example", "# Instruction", new_content)
|
||||
expected = "Start\n# Example\nNew content\n\n# Instruction\nEnd"
|
||||
result = RoleZeroContextBuilder.replace_content_of_example_tag(text, new_content)
|
||||
expected = "Start\n# Past Experience\nNew content\n\n# Instruction\nEnd"
|
||||
assert result == expected
|
||||
|
||||
def test_replace_content_between_markers_no_match(self):
|
||||
def test_replace_content_of_example_tag_no_match(self):
|
||||
text = "Start\nNo markers\nEnd"
|
||||
new_content = "New content"
|
||||
result = RoleZeroContextBuilder.replace_content_between_markers(text, "# Example", "# Instruction", new_content)
|
||||
result = RoleZeroContextBuilder.replace_content_of_example_tag(text, new_content)
|
||||
assert result == text
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue