🌟 The Multi-Agent Framework: First AI Software Company, Towards Natural Language Programming
fix bug of save file and update prompt for gpt_v_generator tool
mannaandpoem06b35a34a6e1
diff --git a/metagpt/prompts/tool_types.py b/metagpt/prompts/tool_types.pyindex c01a80310..718eefd51 100644--- a/metagpt/prompts/tool_types.py+++ b/metagpt/prompts/tool_types.py@@ -42,4 +42,5 @@ The current task is about evaluating a model, please note the following:IMAGE2WEBPAGE_PROMPT = """The current task is about converting image into webpage code. please note the following:- Single-Step Code Generation: Execute the entire code generation process in a single step, encompassing HTML, CSS, and JavaScript. Avoid fragmenting the code generation into multiple separate steps to maintain consistency and simplify the development workflow.+- Save webpages: Be sure to use the save method inside Vision."""diff --git a/metagpt/tools/libs/gpt_v_generator.py b/metagpt/tools/libs/gpt_v_generator.pyindex 58e547840..adc3b1051 100644--- a/metagpt/tools/libs/gpt_v_generator.py+++ b/metagpt/tools/libs/gpt_v_generator.py@@ -3,13 +3,15 @@"""@Time : 2024/01/12@Author : mannaandpoem-@File : vision.py+@File : gpt_v_generator.py"""import base64+import osfrom pathlib import Pathimport requests+from metagpt.const import DEFAULT_WORKSPACE_ROOTfrom metagpt.tools.tool_data_type import ToolTypeEnumfrom metagpt.tools.tool_registry import register_tool@@ -45,7 +47,7 @@ class GPTvGenerator:def analyze_layout(self, image_path):return self.get_result(image_path, ANALYZE_LAYOUT_PROMPT)- def generate_web_pages(self, image_path):+ def generate_webpages(self, image_path):layout = self.analyze_layout(image_path)prompt = GENERATE_PROMPT + "\n\n # Context\n The layout information of the sketch image is: \n" + layoutresult = self.get_result(image_path, prompt)@@ -81,15 +83,16 @@ class GPTvGenerator:@staticmethoddef save_webpages(image_path, webpages) -> Path:- # 在当前目录下创建一个名为webpages的文件夹,用于存储html、css和js文件- webpages_path = Path(image_path).parent / "webpages"- webpages_path.mkdir(exist_ok=True)+ # 在workspace目录下,创建一个名为下webpages的文件夹,用于存储html、css和js文件+ webpages_path = DEFAULT_WORKSPACE_ROOT / "webpages" / Path(image_path).stem+ os.makedirs(webpages_path, exist_ok=True)++ index_path = webpages_path / "index.html"try:- index_path = webpages_path / "index.html"index = webpages.split("```html")[1].split("```")[0]except IndexError:- raise ValueError("No html code found in the result, please check your image and try again.")+ index = "No html code found in the result, please check your image and try again." + "\n" + webpagestry:if "styles.css" in index:@@ -111,13 +114,13 @@ class GPTvGenerator:raise ValueError("No css or js code found in the result, please check your image and try again.")try:- with open(index_path, "w") as f:+ with open(index_path, "w", encoding="utf-8") as f:f.write(index)if style_path:- with open(style_path, "w") as f:+ with open(style_path, "w", encoding="utf-8") as f:f.write(style)if js_path:- with open(js_path, "w") as f:+ with open(js_path, "w", encoding="utf-8") as f:f.write(js)except FileNotFoundError as e:raise FileNotFoundError(f"Cannot save the webpages to {str(webpages_path)}") from ediff --git a/metagpt/tools/schemas/image2webpage/GPTvGenerator.yml b/metagpt/tools/schemas/image2webpage/GPTvGenerator.ymlindex 4087f7c12..1ba2c2b08 100644--- a/metagpt/tools/schemas/image2webpage/GPTvGenerator.yml+++ b/metagpt/tools/schemas/image2webpage/GPTvGenerator.yml@@ -1,12 +1,12 @@GPTvGenerator:type: class- description: "Class for generating web pages at once."+ description: "Class for generating webpages at once."methods:__init__:description: "Initialize Vision class with default values."- generate_web_pages:- description: "Generate web pages including all code(HTML, CSS and JavaScript) in one go based on the image."+ generate_webpages:+ description: "Generate webpages including all code(HTML, CSS and JavaScript) in one go based on the image."parameters:properties:image_path:diff --git a/tests/metagpt/tools/libs/test_gpt_v_generator.py b/tests/metagpt/tools/libs/test_gpt_v_generator.pyindex 360ca4a75..d686d38ba 100644--- a/tests/metagpt/tools/libs/test_gpt_v_generator.py+++ b/tests/metagpt/tools/libs/test_gpt_v_generator.py@@ -3,7 +3,7 @@"""@Time : 2024/01/15@Author : mannaandpoem-@File : test_vision.py+@File : test_gpt_v_generator.py"""import pytest@@ -17,14 +17,14 @@ def mock_webpages(mocker):<link rel="stylesheet" href="styles.css(">\n</html>\n```\n```css\n.class { ... }\n```\n```javascript\nfunction() { ... }\n```\n"""- mocker.patch("metagpt.tools.libs.gpt_v_generator.GPTvGenerator.generate_web_pages", return_value=mock_data)+ mocker.patch("metagpt.tools.libs.gpt_v_generator.GPTvGenerator.generate_webpages", return_value=mock_data)return mockerdef test_vision_generate_webpages(mock_webpages):image_path = "image.png"generator = GPTvGenerator()- rsp = generator.generate_web_pages(image_path=image_path)+ rsp = generator.generate_webpages(image_path=image_path)logs.logger.info(rsp)assert "html" in rspassert "css" in rsp@@ -34,7 +34,7 @@ def test_vision_generate_webpages(mock_webpages):def test_save_webpages(mock_webpages):image_path = "image.png"generator = GPTvGenerator()- webpages = generator.generate_web_pages(image_path)+ webpages = generator.generate_webpages(image_path)webpages_dir = generator.save_webpages(image_path=image_path, webpages=webpages)logs.logger.info(webpages_dir)assert webpages_dir.exists()