From 2846a462a06e9f5536973c2c3e1151047c98d9ed Mon Sep 17 00:00:00 2001 From: femto Date: Fri, 8 Sep 2023 17:21:42 +0800 Subject: [PATCH] fix test --- metagpt/actions/action.py | 9 ++++++--- metagpt/actions/design_api.py | 14 ++++++++++---- metagpt/actions/write_prd.py | 3 ++- tests/metagpt/actions/test_design_api.py | 10 ++++++---- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/metagpt/actions/action.py b/metagpt/actions/action.py index bf991233c..f563c3804 100644 --- a/metagpt/actions/action.py +++ b/metagpt/actions/action.py @@ -77,10 +77,13 @@ class Action(ABC): logger.debug(content) output_class = ActionOutput.create_model_class(output_class_name, output_data_mapping) - pattern = r"\[CONTENT\](.+?)\[/CONTENT\]" + pattern = r"\[CONTENT\](\s*\{.*?\}\s*)\[/CONTENT\]" + matches = re.findall(pattern, content, re.DOTALL) - # Use re.findall to extract content between the tags - extracted_content = re.search(pattern, content, re.DOTALL).group(1) + for match in matches: + if match: + extracted_content = match + break parsed_data = CustomDecoder(strict=False).decode(extracted_content) logger.debug(parsed_data) diff --git a/metagpt/actions/design_api.py b/metagpt/actions/design_api.py index 8e2ca3306..cb94b0426 100644 --- a/metagpt/actions/design_api.py +++ b/metagpt/actions/design_api.py @@ -38,7 +38,8 @@ Max Output: 8192 chars or 2048 tokens. Try to use them up. ## Anything UNCLEAR: Provide as Plain text. Make clear here. -Your job is to create a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like format example +output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like format example, +and only output the json inside this tag, nothing else """ FORMAT_EXAMPLE = """ [CONTENT] @@ -92,8 +93,10 @@ class WriteDesign(Action): def _save_prd(self, docs_path, resources_path, context): prd_file = docs_path / "prd.md" - quadrant_chart = context[-1].instruct_content.dict()["Competitive Quadrant Chart"] - mermaid_to_file(quadrant_chart, resources_path / "competitive_analysis") + if context[-1].instruct_content and context[-1].instruct_content.dict()["Competitive Quadrant Chart"]: + quadrant_chart = context[-1].instruct_content.dict()["Competitive Quadrant Chart"] + mermaid_to_file(quadrant_chart, resources_path / "competitive_analysis") + logger.info(f"Saving PRD to {prd_file}") prd_file.write_text(context[-1].content) @@ -125,7 +128,10 @@ class WriteDesign(Action): self._save_system_design(docs_path, resources_path, system_design) async def run(self, context): - prompt = PROMPT_TEMPLATE.format(context=context, format_example=FORMAT_EXAMPLE) + if isinstance(context, ActionOutput): + prompt = PROMPT_TEMPLATE.format(context=context.content, format_example=FORMAT_EXAMPLE) + else: # context is a string + prompt = PROMPT_TEMPLATE.format(context=context, format_example=FORMAT_EXAMPLE) # system_design = await self._aask(prompt) system_design = await self._aask_json_v1(prompt, "system_design", OUTPUT_MAPPING) self._save(context, system_design) diff --git a/metagpt/actions/write_prd.py b/metagpt/actions/write_prd.py index f259e786f..9a0df85d6 100644 --- a/metagpt/actions/write_prd.py +++ b/metagpt/actions/write_prd.py @@ -61,7 +61,8 @@ Requirements: According to the context, fill in the following missing informatio ## UI Design draft: Provide as Plain text. Be simple. Describe the elements and functions, also provide a simple style description and layout description. ## Anything UNCLEAR: Provide as Plain text. Make clear here. -Your job is to create a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like format example,output CONTENT json directly +output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like format example, +and only output the json inside this tag, nothing else """ FORMAT_EXAMPLE = """ [CONTENT] diff --git a/tests/metagpt/actions/test_design_api.py b/tests/metagpt/actions/test_design_api.py index e6a396ad0..46ef59c29 100644 --- a/tests/metagpt/actions/test_design_api.py +++ b/tests/metagpt/actions/test_design_api.py @@ -7,6 +7,7 @@ """ import pytest +from metagpt.actions import ActionOutput from metagpt.actions.design_api import WriteDesign from metagpt.logs import logger from tests.metagpt.actions.mock import PRD_SAMPLE @@ -18,9 +19,10 @@ async def test_design_api(): design_api = WriteDesign("design_api") - result = await design_api.run(prd) + result = await design_api.run([ActionOutput(content=prd, instruct_content=None)]) logger.info(result) - assert len(result) > 0 + + assert result @pytest.mark.asyncio @@ -28,7 +30,7 @@ async def test_design_api_calculator(): prd = PRD_SAMPLE design_api = WriteDesign("design_api") - result = await design_api.run(prd) + result = await design_api.run([ActionOutput(content=prd, instruct_content=None)]) logger.info(result) - assert len(result) > 10 + assert result