Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- examples/tools/html/actions.py +68 -0
- examples/tools/html/html.py +10 -0
examples/tools/html/actions.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding: utf-8
|
2 |
+
|
3 |
+
import os
|
4 |
+
import re
|
5 |
+
|
6 |
+
from typing import Tuple, Any
|
7 |
+
|
8 |
+
from aworld.core.tool.action_factory import ActionFactory
|
9 |
+
from aworld.core.common import ActionModel, ActionResult
|
10 |
+
from aworld.logs.util import logger
|
11 |
+
from aworld.core.tool.action import ExecutableAction
|
12 |
+
from aworld.models.llm import get_llm_model, call_llm_model
|
13 |
+
|
14 |
+
|
15 |
+
@ActionFactory.register(name="write_html",
|
16 |
+
desc="a tool use for write html.",
|
17 |
+
tool_name="html")
|
18 |
+
class WriteHTML(ExecutableAction):
|
19 |
+
def act(self, action: ActionModel, **kwargs) -> Tuple[ActionResult, Any]:
|
20 |
+
logger.info("start write html!")
|
21 |
+
goal = action.params.get("goal")
|
22 |
+
information = action.params.get("information")
|
23 |
+
|
24 |
+
llm_conf = kwargs.get("llm_config")
|
25 |
+
llm = get_llm_model(llm_conf)
|
26 |
+
sys_prompt = "you are a helpful html writer."
|
27 |
+
prompt = """Your task is to create a detailed and visually appealing HTML document based on the specified theme.
|
28 |
+
The document must meet the following requirements, and you should utilize the provided reference materials to ensure accuracy and aesthetic quality.
|
29 |
+
1) HTML Document Requirements
|
30 |
+
Design and write the HTML document according to the following specifications:
|
31 |
+
Theme : {goal}
|
32 |
+
Related Info: {information}
|
33 |
+
Structural Requirements :
|
34 |
+
Use semantic HTML tags (e.g., <header>, <main>, <footer>, <section>) to create a clear and organized structure.
|
35 |
+
Ensure the document includes a header, navigation bar, main content area, and footer.
|
36 |
+
If applicable, add additional sections such as a sidebar, or call-to-action buttons.
|
37 |
+
Styling Requirements :
|
38 |
+
Implement a visually appealing design using CSS, including color schemes, font choices, spacing adjustments, etc.
|
39 |
+
Ensure the page has a responsive layout that works well on different devices (use media queries or frameworks like Bootstrap).
|
40 |
+
Add animations or interactive features (e.g., hover effects on buttons, scroll-triggered animations) to enhance user experience.
|
41 |
+
please give me html code directly, no need other words
|
42 |
+
"""
|
43 |
+
|
44 |
+
messages = [{'role': 'system', 'content': sys_prompt},
|
45 |
+
{'role': 'user', 'content': prompt.format(goal=goal, information=information)}]
|
46 |
+
|
47 |
+
output = call_llm_model(llm,
|
48 |
+
messages=messages,
|
49 |
+
model=llm_conf.llm_model_name,
|
50 |
+
temperature=llm_conf.llm_temperature)
|
51 |
+
|
52 |
+
content = output.content
|
53 |
+
html_pattern = re.compile(r'<html.*?>.*?</html>', re.DOTALL)
|
54 |
+
matches = html_pattern.findall(content)
|
55 |
+
|
56 |
+
title_pattern = re.compile(r'<title.*?>.*?</title>', re.DOTALL)
|
57 |
+
filename = (title_pattern.findall(content)[0]
|
58 |
+
.replace("<title>", "")
|
59 |
+
.replace("</title>", "")
|
60 |
+
.replace(" ", "_") + ".html")
|
61 |
+
|
62 |
+
with open(filename, "a", encoding='utf-8') as f:
|
63 |
+
f.write(matches[0])
|
64 |
+
|
65 |
+
abs_file_path = os.path.abspath(filename)
|
66 |
+
msg = f'Successfully wrote html to {abs_file_path}'
|
67 |
+
|
68 |
+
return ActionResult(content=msg, keep=True, is_done=True), None
|
examples/tools/html/html.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding: utf-8
|
2 |
+
|
3 |
+
from aworld.tools.template_tool import TemplateTool
|
4 |
+
from examples.tools.tool_action import WriteAction
|
5 |
+
from aworld.core.tool.base import ToolFactory
|
6 |
+
|
7 |
+
|
8 |
+
@ToolFactory.register(name="html", desc="html tool", supported_action=WriteAction)
|
9 |
+
class HtmlTool(TemplateTool):
|
10 |
+
"""Html tool"""
|