Merge branch 'code_intepreter_fix_gpt_v_generator' into 'code_intepreter'

fix bug of save file and update prompt for gpt_v_generator tool

See merge request agents/data_agents_opt!52
This commit is contained in:
林义章 2024-01-22 02:10:18 +00:00
commit e1a37ca76c
4 changed files with 21 additions and 17 deletions

View file

@ -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 provided.
"""

View file

@ -3,13 +3,15 @@
"""
@Time : 2024/01/12
@Author : mannaandpoem
@File : vision.py
@File : gpt_v_generator.py
"""
import base64
import os
from pathlib import Path
import requests
from metagpt.const import DEFAULT_WORKSPACE_ROOT
from metagpt.tools.tool_data_type import ToolTypeEnum
from 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" + layout
result = self.get_result(image_path, prompt)
@ -81,15 +83,16 @@ class GPTvGenerator:
@staticmethod
def 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" + webpages
try:
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 e

View file

@ -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:

View file

@ -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 mocker
def 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 rsp
assert "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()