diff --git a/src/agentscope/parsers/code_block_parser.py b/src/agentscope/parsers/code_block_parser.py index df6341123..627d89a1f 100644 --- a/src/agentscope/parsers/code_block_parser.py +++ b/src/agentscope/parsers/code_block_parser.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- """Model response parser class for Markdown code block.""" +from typing import Optional + from agentscope.models import ModelResponse from agentscope.parsers import ParserBase @@ -22,15 +24,38 @@ class MarkdownCodeBlockParser(ParserBase): format_instruction: str = ( "You should generate {language_name} code in a {language_name} fenced " "code block as follows: \n```{language_name}\n" - "${{your_{language_name}_code}}\n```" + "{content_hint}\n```" ) """The instruction for the format of the code block.""" - def __init__(self, language_name: str) -> None: + def __init__( + self, + language_name: str, + content_hint: Optional[str] = None, + ) -> None: + """Initialize the parser with the language name and the optional + content hint. + + Args: + language_name (`str`): + The name of the language, which will be used + in ```{language_name} + content_hint (`Optional[str]`, defaults to `None`): + The hint used to remind LLM what should be fill between the + tags. If not provided, the default content hint + "${{your_{language_name}_code}}" will be used. + """ self.name = self.name.format(language_name=language_name) self.tag_begin = self.tag_begin.format(language_name=language_name) + + if content_hint is None: + self.content_hint = f"${{your_{language_name}_code}}" + else: + self.content_hint = content_hint + self.format_instruction = self.format_instruction.format( language_name=language_name, + content_hint=self.content_hint, ).strip() def parse(self, response: ModelResponse) -> ModelResponse: diff --git a/tests/parser_test.py b/tests/parser_test.py index 0adf3d728..384ef64cf 100644 --- a/tests/parser_test.py +++ b/tests/parser_test.py @@ -95,6 +95,13 @@ def setUp(self) -> None: "${your_python_code}\n" "```" ) + self.instruction_code_with_hint = ( + "You should generate python code in a python fenced code block as " + "follows: \n" + "```python\n" + "abc\n" + "```" + ) self.gt_code = """\nprint("Hello, world!")\n""" def test_markdownjsondictparser(self) -> None: @@ -151,6 +158,22 @@ def test_markdowncodeblockparser(self) -> None: self.assertEqual(res.parsed, self.gt_code) + def test_markdowncodeblockparser_with_hint(self) -> None: + """Test for MarkdownCodeBlockParser""" + parser = MarkdownCodeBlockParser( + language_name="python", + content_hint="abc", + ) + + self.assertEqual( + parser.format_instruction, + self.instruction_code_with_hint, + ) + + res = parser.parse(self.res_code) + + self.assertEqual(res.parsed, self.gt_code) + def test_multitaggedcontentparser(self) -> None: """Test for MultiTaggedContentParser""" parser = MultiTaggedContentParser(