Skip to content

🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming

remove replace_content_between_markers function of RoleZeroContextBuilder and update tests

黄伟韬0a1c965933e5
diff --git a/metagpt/exp_pool/context_builders/role_zero.py b/metagpt/exp_pool/context_builders/role_zero.py
index 3145ca998..4a9042765 100644
--- a/metagpt/exp_pool/context_builders/role_zero.py
+++ b/metagpt/exp_pool/context_builders/role_zero.py
@@ -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
diff --git a/tests/metagpt/exp_pool/test_context_builders/test_rolezero_context_builder.py b/tests/metagpt/exp_pool/test_context_builders/test_rolezero_context_builder.py
index b7182602d..795e8c26b 100644
--- a/tests/metagpt/exp_pool/test_context_builders/test_rolezero_context_builder.py
+++ b/tests/metagpt/exp_pool/test_context_builders/test_rolezero_context_builder.py
@@ -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