🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: Add file write toolkit (#1774)
Sun Tao6842d1fe4f5e
diff --git a/camel/toolkits/__init__.py b/camel/toolkits/__init__.pyindex c56aa7c6b..b7f5ee859 100644--- a/camel/toolkits/__init__.py+++ b/camel/toolkits/__init__.py@@ -56,6 +56,7 @@ from .video_analysis_toolkit import VideoAnalysisToolkitfrom .image_analysis_toolkit import ImageAnalysisToolkitfrom .mcp_toolkit import MCPToolkitfrom .web_toolkit import WebToolkit+from .file_write_toolkit import FileWriteToolkitfrom .terminal_toolkit import TerminalToolkit@@ -101,5 +102,6 @@ __all__ = ['VideoAnalysisToolkit','ImageAnalysisToolkit','WebToolkit',+ 'FileWriteToolkit','TerminalToolkit',]diff --git a/camel/toolkits/file_write_toolkit.py b/camel/toolkits/file_write_toolkit.pynew file mode 100644index 000000000..fc7e8f207--- /dev/null+++ b/camel/toolkits/file_write_toolkit.py@@ -0,0 +1,371 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+++from datetime import datetime+from pathlib import Path+from typing import List, Optional, Union++from camel.logger import get_logger+from camel.toolkits.base import BaseToolkit+from camel.toolkits.function_tool import FunctionTool++logger = get_logger(__name__)++# Default format when no extension is provided+DEFAULT_FORMAT = '.md'+++class FileWriteToolkit(BaseToolkit):+ r"""A toolkit for creating, writing, and modifying text in files.++ This class provides cross-platform (macOS, Linux, Windows) support for+ writing to various file formats (Markdown, DOCX, PDF, and plaintext),+ replacing text in existing files, automatic backups, custom encoding,+ and enhanced formatting options for specialized formats.+ """++ def __init__(+ self,+ output_dir: str = "./",+ timeout: Optional[float] = None,+ default_encoding: str = "utf-8",+ backup_enabled: bool = True,+ ) -> None:+ r"""Initialize the FileWriteToolkit.++ Args:+ output_dir (str): The default directory for output files.+ Defaults to the current working directory.+ timeout (Optional[float]): The timeout for the toolkit.+ (default: :obj: `None`)+ default_encoding (str): Default character encoding for text+ operations. (default: :obj: `utf-8`)+ backup_enabled (bool): Whether to create backups of existing files+ before overwriting. (default: :obj: `True`)+ """+ super().__init__(timeout=timeout)+ self.output_dir = Path(output_dir).resolve()+ self.output_dir.mkdir(parents=True, exist_ok=True)+ self.default_encoding = default_encoding+ self.backup_enabled = backup_enabled+ logger.info(+ f"FileWriteToolkit initialized with output directory"+ f": {self.output_dir}, encoding: {default_encoding}"+ )++ def _resolve_filepath(self, file_path: str) -> Path:+ r"""Convert the given string path to a Path object.++ If the provided path is not absolute, it is made relative to the+ default output directory.++ Args:+ file_path (str): The file path to resolve.++ Returns:+ Path: A fully resolved (absolute) Path object.+ """+ path_obj = Path(file_path)+ if not path_obj.is_absolute():+ path_obj = self.output_dir / path_obj+ return path_obj.resolve()++ def _write_text_file(+ self, file_path: Path, content: str, encoding: str = "utf-8"+ ) -> None:+ r"""Write text content to a plaintext file.++ Args:+ file_path (Path): The target file path.+ content (str): The text content to write.+ encoding (str): Character encoding to use. (default: :obj: `utf-8`)+ """+ with file_path.open("w", encoding=encoding) as f:+ f.write(content)+ logger.debug(f"Wrote text to {file_path} with {encoding} encoding")++ def _create_backup(self, file_path: Path) -> None:+ r"""Create a backup of the file if it exists and backup is enabled.++ Args:+ file_path (Path): Path to the file to backup.+ """+ import shutil++ if not self.backup_enabled or not file_path.exists():+ return++ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")+ backup_path = file_path.parent / f"{file_path.name}.{timestamp}.bak"+ shutil.copy2(file_path, backup_path)+ logger.info(f"Created backup at {backup_path}")++ def _write_docx_file(self, file_path: Path, content: str) -> None:+ r"""Write text content to a DOCX file with default formatting.++ Args:+ file_path (Path): The target file path.+ content (str): The text content to write.+ """+ import docx++ # Use default formatting values+ font_name = 'Calibri'+ font_size = 11+ line_spacing = 1.0++ document = docx.Document()+ style = document.styles['Normal']+ style.font.name = font_name+ style.font.size = docx.shared.Pt(font_size)+ style.paragraph_format.line_spacing = line_spacing++ # Split content into paragraphs and add them+ for para_text in content.split('\n'):+ para = document.add_paragraph(para_text)+ para.style = style++ document.save(str(file_path))+ logger.debug(f"Wrote DOCX to {file_path} with default formatting")++ def _write_pdf_file(self, file_path: Path, content: str, **kwargs) -> None:+ r"""Write text content to a PDF file with default formatting.++ Args:+ file_path (Path): The target file path.+ content (str): The text content to write.++ Raises:+ RuntimeError: If the 'fpdf' library is not installed.+ """+ from fpdf import FPDF++ # Use default formatting values+ font_family = 'Arial'+ font_size = 12+ font_style = ''+ line_height = 10+ margin = 10++ pdf = FPDF()+ pdf.set_margins(margin, margin, margin)++ pdf.add_page()+ pdf.set_font(font_family, style=font_style, size=font_size)++ # Split content into paragraphs and add them+ for para in content.split('\n'):+ if para.strip(): # Skip empty paragraphs+ pdf.multi_cell(0, line_height, para)+ else:+ pdf.ln(line_height) # Add empty line++ pdf.output(str(file_path))+ logger.debug(f"Wrote PDF to {file_path} with custom formatting")++ def _write_csv_file(+ self,+ file_path: Path,+ content: Union[str, List[List]],+ encoding: str = "utf-8",+ ) -> None:+ r"""Write CSV content to a file.++ Args:+ file_path (Path): The target file path.+ content (Union[str, List[List]]): The CSV content as a string or+ list of lists.+ encoding (str): Character encoding to use. (default: :obj: `utf-8`)+ """+ import csv++ with file_path.open("w", encoding=encoding, newline='') as f:+ if isinstance(content, str):+ f.write(content)+ else:+ writer = csv.writer(f)+ writer.writerows(content)+ logger.debug(f"Wrote CSV to {file_path} with {encoding} encoding")++ def _write_json_file(+ self,+ file_path: Path,+ content: str,+ encoding: str = "utf-8",+ ) -> None:+ r"""Write JSON content to a file.++ Args:+ file_path (Path): The target file path.+ content (str): The JSON content as a string.+ encoding (str): Character encoding to use. (default: :obj: `utf-8`)+ """+ import json++ with file_path.open("w", encoding=encoding) as f:+ if isinstance(content, str):+ try:+ # Try parsing as JSON string first+ data = json.loads(content)+ json.dump(data, f)+ except json.JSONDecodeError:+ # If not valid JSON string, write as is+ f.write(content)+ else:+ # If not string, dump as JSON+ json.dump(content, f)+ logger.debug(f"Wrote JSON to {file_path} with {encoding} encoding")++ def _write_yaml_file(+ self,+ file_path: Path,+ content: str,+ encoding: str = "utf-8",+ ) -> None:+ r"""Write YAML content to a file.++ Args:+ file_path (Path): The target file path.+ content (str): The YAML content as a string.+ encoding (str): Character encoding to use. (default: :obj: `utf-8`)+ """+ with file_path.open("w", encoding=encoding) as f:+ f.write(content)+ logger.debug(f"Wrote YAML to {file_path} with {encoding} encoding")++ def _write_html_file(+ self, file_path: Path, content: str, encoding: str = "utf-8"+ ) -> None:+ r"""Write text content to an HTML file.++ Args:+ file_path (Path): The target file path.+ content (str): The HTML content to write.+ encoding (str): Character encoding to use. (default: :obj: `utf-8`)+ """+ with file_path.open("w", encoding=encoding) as f:+ f.write(content)+ logger.debug(f"Wrote HTML to {file_path} with {encoding} encoding")++ def _write_markdown_file(+ self, file_path: Path, content: str, encoding: str = "utf-8"+ ) -> None:+ r"""Write text content to a Markdown file.++ Args:+ file_path (Path): The target file path.+ content (str): The Markdown content to write.+ encoding (str): Character encoding to use. (default: :obj: `utf-8`)+ """+ with file_path.open("w", encoding=encoding) as f:+ f.write(content)+ logger.debug(f"Wrote Markdown to {file_path} with {encoding} encoding")++ def write_to_file(+ self,+ content: Union[str, List[List[str]]],+ filename: str,+ encoding: Optional[str] = None,+ ) -> str:+ r"""Write the given content to a file.++ If the file exists, it will be overwritten. Supports multiple formats:+ Markdown (.md, .markdown, default), Plaintext (.txt), CSV (.csv),+ DOC/DOCX (.doc, .docx), PDF (.pdf), JSON (.json), YAML (.yml, .yaml),+ and HTML (.html, .htm).++ Args:+ content (Union[str, List[List[str]]]): The content to write to the+ file. For all formats, content must be a string or list in the+ appropriate format.+ filename (str): The name or path of the file. If a relative path is+ supplied, it is resolved to self.output_dir.+ encoding (Optional[str]): The character encoding to use. (default:+ :obj: `None`)++ Returns:+ str: A message indicating success or error details.+ """+ file_path = self._resolve_filepath(filename)+ file_path.parent.mkdir(parents=True, exist_ok=True)++ # Create backup if file exists+ self._create_backup(file_path)++ extension = file_path.suffix.lower()++ # If no extension is provided, use the default format+ if extension == "":+ file_path = file_path.with_suffix(DEFAULT_FORMAT)+ extension = DEFAULT_FORMAT++ try:+ # Get encoding or use default+ file_encoding = encoding or self.default_encoding++ if extension in [".doc", ".docx"]:+ self._write_docx_file(file_path, str(content))+ elif extension == ".pdf":+ self._write_pdf_file(file_path, str(content))+ elif extension == ".csv":+ self._write_csv_file(+ file_path, content, encoding=file_encoding+ )+ elif extension == ".json":+ self._write_json_file(+ file_path,+ content, # type: ignore[arg-type]+ encoding=file_encoding,+ )+ elif extension in [".yml", ".yaml"]:+ self._write_yaml_file(+ file_path, str(content), encoding=file_encoding+ )+ elif extension in [".html", ".htm"]:+ self._write_html_file(+ file_path, str(content), encoding=file_encoding+ )+ elif extension in [".md", ".markdown"]:+ self._write_markdown_file(+ file_path, str(content), encoding=file_encoding+ )+ else:+ # Fallback to simple text writing for unknown or .txt+ # extensions+ self._write_text_file(+ file_path, str(content), encoding=file_encoding+ )++ msg = f"Content successfully written to file: {file_path}"+ logger.info(msg)+ return msg+ except Exception as e:+ error_msg = (+ f"Error occurred while writing to file {file_path}: {e}"+ )+ logger.error(error_msg)+ return error_msg++ def get_tools(self) -> List[FunctionTool]:+ r"""Return a list of FunctionTool objects representing the functions+ in the toolkit.++ Returns:+ List[FunctionTool]: A list of FunctionTool objects representing+ the available functions in this toolkit.+ """+ return [+ FunctionTool(self.write_to_file),+ ]diff --git a/examples/toolkits/file_write_toolkit.py b/examples/toolkits/file_write_toolkit.pynew file mode 100644index 000000000..bdb61db00--- /dev/null+++ b/examples/toolkits/file_write_toolkit.py@@ -0,0 +1,201 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# ruff: noqa: E501+import os++from camel.agents import ChatAgent+from camel.models import ModelFactory+from camel.toolkits import FileWriteToolkit+from camel.types import ModelPlatformType+from camel.types.enums import ModelType++# Create a model instance+model = ModelFactory.create(+ model_platform=ModelPlatformType.DEFAULT,+ model_type=ModelType.DEFAULT,+ model_config_dict={"temperature": 0},+)++# Define system message for the agent+sys_msg = "You are a helpful assistant that can create and modify files."++# Set up output directory+output_dir = "./file_write_outputs"+os.makedirs(output_dir, exist_ok=True)++# Initialize the FileWriteToolkit with the output directory+file_toolkit = FileWriteToolkit(output_dir=output_dir)++# Get the tools from the toolkit+tools_list = file_toolkit.get_tools()++# Initialize a ChatAgent with the tools+camel_agent = ChatAgent(+ system_message=sys_msg,+ model=model,+ tools=tools_list,+)++# Example 1: Write a Python script to a file+python_query = """Please generate a Python script that creates a simple+ web server using Flask and save it to a file."""++camel_agent.reset()+response = camel_agent.step(python_query)+print("Example 1: Writing a Python script")+print(response.msgs[0].content)+print("Tool calls:", response.info['tool_calls'])+print("\n")+'''+===============================================================================+Example 1: Writing a Python script+The Python script for a simple web server using Flask has been created and saved as `simple_flask_server.py`. You can run this script to start the server, and it will return "Hello, Flask!" when accessed at the root URL.+Tool calls: [ToolCallingRecord(tool_name='write_to_file', args={'content': 'from flask import Flask\n\napp = Flask(__name__)\n\[email protected](\'/\')\ndef home():\n return "Hello, Flask!"\n\nif __name__ == \'__main__\':\n app.run(debug=True)', 'filename': 'simple_flask_server.py', 'encoding': 'utf-8'}, result='Content successfully written to file: /Users/enrei/Desktop/camel0209/camel/file_write_outputs/simple_flask_server.py', tool_call_id='call_hCCxkjNkx4HKN9q6fuIpe8Bn')]+===============================================================================+'''++# Example 2: Create a JSON data file+json_query = """Generate a JSON file containing information about 3 fictional+ books, including title, author, publication year, and genre."""+camel_agent.reset()+response = camel_agent.step(json_query)+print("Example 2: Creating a JSON file")+print(response.msgs[0].content)+print("Tool calls:", response.info['tool_calls'])+print("\n")+'''+===============================================================================+Example 2: Creating a JSON file+The JSON file containing information about three fictional books has been successfully created. You can find it at the following location: **books.json**.++Here is the content of the file:++```json+[+ {+ "title": "The Whispering Shadows",+ "author": "Ava Sinclair",+ "publication_year": 2021,+ "genre": "Fantasy"+ },+ {+ "title": "Echoes of the Past",+ "author": "Liam Carter",+ "publication_year": 2019,+ "genre": "Historical Fiction"+ },+ {+ "title": "The Last Star",+ "author": "Maya Thompson",+ "publication_year": 2022,+ "genre": "Science Fiction"+ }+]+```+Tool calls: [ToolCallingRecord(tool_name='write_to_file', args={'content': '[ \n { \n "title": "The Whispering Shadows", \n "author": "Ava Sinclair", \n "publication_year": 2021, \n "genre": "Fantasy" \n }, \n { \n "title": "Echoes of the Past", \n "author": "Liam Carter", \n "publication_year": 2019, \n "genre": "Historical Fiction" \n }, \n { \n "title": "The Last Star", \n "author": "Maya Thompson", \n "publication_year": 2022, \n "genre": "Science Fiction" \n } \n]', 'filename': 'books.json', 'encoding': 'utf-8'}, result='Content successfully written to file: /Users/enrei/Desktop/camel0209/camel/file_write_outputs/books.json', tool_call_id='call_1ayRgujHhiWowz0jhtMCukgn')]+===============================================================================+'''++# Example 3: Create a CSV file with tabular data+csv_query = """Create a CSV file with data about 5 countries, including+ columns for name, capital, population, area, and continent."""+camel_agent.reset()+response = camel_agent.step(csv_query)+print("Example 3: Creating a CSV file")+print(response.msgs[0].content)+print("Tool calls:", response.info['tool_calls'])+print("\n")+'''+===============================================================================+Example 3: Creating a CSV file+The CSV file containing data about 5 countries has been successfully created. It includes the following columns: name, capital, population, area, and continent. If you need any further modifications or additional data, feel free to ask!+Tool calls: [ToolCallingRecord(tool_name='write_to_file', args={'content': [['Name', 'Capital', 'Population', 'Area (sq km)', 'Continent'], ['United States', 'Washington, D.C.', '331002651', '9833517', 'North America'], ['Brazil', 'Brasília', '212559417', '8515767', 'South America'], ['Germany', 'Berlin', '83783942', '357022', 'Europe'], ['Australia', 'Canberra', '25499884', '7692024', 'Oceania'], ['Japan', 'Tokyo', '126476461', '377975', 'Asia']], 'filename': 'countries_data.csv', 'encoding': 'utf-8'}, result='Content successfully written to file: /Users/enrei/Desktop/camel0209/camel/file_write_outputs/countries_data.csv', tool_call_id='call_yTgErI2TrV32ehs5LJCf6kW7')]+===============================================================================+'''++# Example 4: Create a Markdown document+md_query = """Write a markdown document that explains the basics of machine+ learning, including headings, bullet points, and code examples.+ """+camel_agent.reset()+response = camel_agent.step(md_query)+print("Example 4: Creating a Markdown document")+print(response.msgs[0].content)+print("Tool calls:", response.info['tool_calls'])+print("\n")+'''+===============================================================================+Example 4: Creating a Markdown document+The markdown document explaining the basics of machine learning has been successfully created. You can find it under the name **basics_of_machine_learning.md**. If you need any further modifications or additional information, feel free to ask!+Tool calls: [ToolCallingRecord(tool_name='write_to_file', args={'content': "# Basics of Machine Learning\n\nMachine Learning (ML) is a subset of artificial intelligence (AI) that focuses on building systems that learn from data and improve their performance over time without being explicitly programmed. Here are the key concepts and components of machine learning:\n\n## Key Concepts\n\n- **Data**: The foundation of machine learning. Data can be structured (like tables) or unstructured (like images or text).\n- **Model**: A mathematical representation of a process that is trained on data to make predictions or decisions.\n- **Training**: The process of feeding data into a model to help it learn patterns.\n- **Testing**: Evaluating the model's performance on unseen data to ensure it generalizes well.\n- **Features**: Individual measurable properties or characteristics used as input to the model.\n- **Labels**: The output or target variable that the model is trying to predict.\n\n## Types of Machine Learning\n\n1. **Supervised Learning**: The model is trained on labeled data.\n - **Examples**: Classification, Regression\n - **Use Cases**: Spam detection, House price prediction\n\n2. **Unsupervised Learning**: The model is trained on unlabeled data and tries to find patterns.\n - **Examples**: Clustering, Dimensionality Reduction\n - **Use Cases**: Customer segmentation, Anomaly detection\n\n3. **Reinforcement Learning**: The model learns by interacting with an environment and receiving feedback.\n - **Examples**: Game playing, Robotics\n - **Use Cases**: Self-driving cars, Game AI\n\n## Machine Learning Workflow\n\n1. **Data Collection**: Gather data from various sources.\n2. **Data Preprocessing**: Clean and prepare the data for analysis.\n3. **Model Selection**: Choose the appropriate algorithm for the task.\n4. **Training the Model**: Fit the model to the training data.\n5. **Model Evaluation**: Assess the model's performance using metrics like accuracy, precision, and recall.\n6. **Hyperparameter Tuning**: Optimize the model's parameters for better performance.\n7. **Deployment**: Implement the model in a production environment.\n\n## Code Example\n\nHere is a simple example of a supervised learning model using Python and the popular library `scikit-learn`:\n\n```python\n# Import necessary libraries\nimport numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import mean_squared_error\n\n# Load dataset\ndata = pd.read_csv('data.csv')\n\n# Define features and labels\nX = data[['feature1', 'feature2']]\nY = data['label']\n\n# Split the data into training and testing sets\nX_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=42)\n\n# Create a model\nmodel = LinearRegression()\n\n# Train the model\nmodel.fit(X_train, Y_train)\n\n# Make predictions\npredictions = model.predict(X_test)\n\n# Evaluate the model\nmse = mean_squared_error(Y_test, predictions)\nprint(f'Mean Squared Error: {mse}')\n```\n\n## Conclusion\n\nMachine learning is a powerful tool that can be applied to various fields, from healthcare to finance. Understanding the basics of machine learning is essential for anyone looking to leverage data for decision-making and predictive analytics.", 'filename': 'basics_of_machine_learning.md', 'encoding': 'utf-8'}, result='Content successfully written to file: /Users/enrei/Desktop/camel0209/camel/file_write_outputs/basics_of_machine_learning.md', tool_call_id='call_KKHcbPvmG8VWc4W2JeOWcg1B')]+===============================================================================+'''++# Example 5: Create a YAML configuration file+yaml_query = """Generate a YAML configuration file for a web application+ with settings for database connection, logging, and server+ parameters."""+camel_agent.reset()+response = camel_agent.step(yaml_query)+print("Example 5: Creating a YAML configuration file")+print(response.msgs[0].content)+print("Tool calls:", response.info['tool_calls'])+print("\n")+'''+===============================================================================+Example 5: Creating a YAML configuration file+The YAML configuration file for the web application has been successfully created. Here are the contents of the file:++```yaml+database:+ host: localhost+ port: 5432+ username: user+ password: password+ dbname: mydatabase++logging:+ level: info+ file: /var/log/myapp.log+ max_size: 10MB+ max_backups: 5++server:+ host: 0.0.0.0+ port: 8080+ timeout: 30s+ enable_https: true+```++The file is saved as `config.yaml`. If you need any modifications or additional settings, feel free to ask!+Tool calls: [ToolCallingRecord(tool_name='write_to_file', args={'content': 'database:\n host: localhost\n port: 5432\n username: user\n password: password\n dbname: mydatabase\n\nlogging:\n level: info\n file: /var/log/myapp.log\n max_size: 10MB\n max_backups: 5\n\nserver:\n host: 0.0.0.0\n port: 8080\n timeout: 30s\n enable_https: true\n', 'filename': 'config.yaml', 'encoding': 'utf-8'}, result='Content successfully written to file: /Users/enrei/Desktop/camel0209/camel/file_write_outputs/config.yaml', tool_call_id='call_svQbTh8tl1diDDYwxNDWUp2U')]+===============================================================================+'''++# Example 6: Create an HTML file+html_query = """Create a simple HTML webpage with a header, navigation menu,+ main content section, and footer."""+camel_agent.reset()+response = camel_agent.step(html_query)+print("Example 6: Creating an HTML file")+print(response.msgs[0].content)+print("Tool calls:", response.info['tool_calls'])+print("\n")+'''+===============================================================================+Example 6: Creating an HTML file+I have created a simple HTML webpage with a header, navigation menu, main content section, and footer. You can find the file named `simple_webpage.html` in the specified directory. If you need any modifications or additional features, feel free to ask!+Tool calls: [ToolCallingRecord(tool_name='write_to_file', args={'content': '<!DOCTYPE html>\n<html lang="en">\n<head>\n <meta charset="UTF-8">\n <meta name="viewport" content="width=device-width, initial-scale=1.0">\n <title>Simple Webpage</title>\n <link rel="stylesheet" href="styles.css">\n</head>\n<body>\n <header>\n <h1>Welcome to My Simple Webpage</h1>\n </header>\n <nav>\n <ul>\n <li><a href="#home">Home</a></li>\n <li><a href="#about">About</a></li>\n <li><a href="#services">Services</a></li>\n <li><a href="#contact">Contact</a></li>\n </ul>\n </nav>\n <main>\n <section id="home">\n <h2>Home</h2>\n <p>This is the home section of the webpage.</p>\n </section>\n <section id="about">\n <h2>About</h2>\n <p>This section contains information about us.</p>\n </section>\n <section id="services">\n <h2>Services</h2>\n <p>Details about our services can be found here.</p>\n </section>\n <section id="contact">\n <h2>Contact</h2>\n <p>Get in touch with us through this section.</p>\n </section>\n </main>\n <footer>\n <p>© 2023 My Simple Webpage. All rights reserved.</p>\n </footer>\n</body>\n</html>', 'filename': 'simple_webpage.html', 'encoding': 'utf-8'}, result='Content successfully written to file: /Users/enrei/Desktop/camel0209/camel/file_write_outputs/simple_webpage.html', tool_call_id='call_6FUwTx4gSAB8mtN7lety05SP')]+===============================================================================+'''diff --git a/pyproject.toml b/pyproject.tomlindex ec3f966b5..8cfd945aa 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -107,6 +107,8 @@ document_tools = ["xls2xlsx>=0.2.0","tabulate>=0.9.0","openpyxl>=3.1.5",+ "docx>=0.2.4",+ "fpdf>=1.7.2",]media_tools = ["imageio[pyav]>=2.34.2,<3",@@ -155,7 +157,7 @@ model_platforms = ["litellm>=1.38.1,<2","mistralai>=1.1.0,<2","reka-api>=3.0.8,<4",- "anthropic>=0.42.0,<0.43",+ "anthropic>=0.47.0,<0.50.0","cohere>=5.11.0,<6","fish-audio-sdk>=2024.12.5,<2025",]@@ -249,7 +251,7 @@ all = ["litellm>=1.38.1,<2","mistralai>=1.1.0,<2","fish-audio-sdk>=2024.12.5,<2025",- "anthropic>=0.42.0,<0.43",+ "anthropic>=0.47.0,<0.50.0","reka-api>=3.0.8,<4","redis>=5.0.6,<6","azure-storage-blob>=12.21.0,<13",@@ -275,6 +277,8 @@ all = ["scenedetect>=0.6.5.2","playwright>=1.50.0","html2text>=2024.2.26",+ "docx>=0.2.4",+ "fpdf>=1.7.2","mcp>=1.3.0","typer>=0.15.2",]@@ -448,6 +452,8 @@ module = ["scenedetect","playwright","html2text",+ "docx",+ "fpdf","mcp","typer",]diff --git a/test/toolkits/test_file_write_toolkit.py b/test/toolkits/test_file_write_toolkit.pynew file mode 100644index 000000000..f557bc9fc--- /dev/null+++ b/test/toolkits/test_file_write_toolkit.py@@ -0,0 +1,309 @@+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========++import json+import os+import tempfile+from pathlib import Path++import pytest+import yaml++from camel.toolkits import FileWriteToolkit+++def file_write_toolkit():+ r"""Create a FileWriteToolkit instance for testing."""+ with tempfile.TemporaryDirectory() as temp_dir:+ toolkit = FileWriteToolkit(output_dir=temp_dir)+ yield toolkit+++def temp_dir():+ r"""Create a temporary directory for file operations."""+ with tempfile.TemporaryDirectory() as temp_dir:+ yield temp_dir+++def test_initialization():+ r"""Test the initialization of FileWriteToolkit with default parameters."""+ toolkit = FileWriteToolkit()++ assert toolkit.output_dir == Path("./").resolve()+ assert toolkit.default_encoding == "utf-8"+ assert toolkit.backup_enabled is True+++def test_initialization_with_custom_parameters():+ r"""Test the initialization of FileWriteToolkit with custom parameters."""+ output_dir = "./custom_dir"+ default_encoding = "latin-1"+ backup_enabled = False++ toolkit = FileWriteToolkit(+ output_dir=output_dir,+ default_encoding=default_encoding,+ backup_enabled=backup_enabled,+ )++ assert toolkit.output_dir == Path(output_dir).resolve()+ assert toolkit.default_encoding == default_encoding+ assert toolkit.backup_enabled is backup_enabled+++def test_write_to_file_text(file_write_toolkit):+ r"""Test writing plain text to a file."""+ content = "Hello, world!"+ filename = "test.txt"++ result = file_write_toolkit.write_to_file(content, filename)++ # Check the result message+ assert "successfully written" in result++ # Check the file exists and has correct content+ file_path = file_write_toolkit._resolve_filepath(filename)+ assert file_path.exists()++ with open(file_path, "r", encoding="utf-8") as f:+ file_content = f.read()++ assert file_content == content+++def test_write_to_file_markdown(file_write_toolkit):+ r"""Test writing markdown content to a file."""+ content = "# Heading\n\nThis is a paragraph with **bold** text."+ filename = "test.md"++ result = file_write_toolkit.write_to_file(content, filename)++ # Check the result message+ assert "successfully written" in result++ # Check the file exists and has correct content+ file_path = file_write_toolkit._resolve_filepath(filename)+ assert file_path.exists()++ with open(file_path, "r", encoding="utf-8") as f:+ file_content = f.read()++ assert file_content == content+++def test_write_to_file_json(file_write_toolkit):+ r"""Test writing JSON data to a file."""+ data = {"name": "John", "age": 30, "city": "New York"}+ filename = "test.json"++ result = file_write_toolkit.write_to_file(data, filename)++ # Check the result message+ assert "successfully written" in result++ # Check the file exists and has correct content+ file_path = file_write_toolkit._resolve_filepath(filename)+ assert file_path.exists()++ with open(file_path, "r", encoding="utf-8") as f:+ file_content = json.load(f)++ assert file_content == data+++def test_write_to_file_yaml(file_write_toolkit):+ r"""Test writing YAML data to a file."""+ data = {"name": "John", "age": 30, "city": "New York"}+ filename = "test.yaml"++ result = file_write_toolkit.write_to_file(data, filename)++ # Check the result message+ assert "successfully written" in result++ # Check the file exists and has correct content+ file_path = file_write_toolkit._resolve_filepath(filename)+ assert file_path.exists()++ with open(file_path, "r", encoding="utf-8") as f:+ file_content = yaml.safe_load(f)++ assert file_content == data+++def test_write_to_file_csv_string(file_write_toolkit):+ r"""Test writing CSV content as a string to a file."""+ content = "name,age,city\nJohn,30,New York\nJane,25,San Francisco"+ filename = "test.csv"++ result = file_write_toolkit.write_to_file(content, filename)++ # Check the result message+ assert "successfully written" in result++ # Check the file exists and has correct content+ file_path = file_write_toolkit._resolve_filepath(filename)+ assert file_path.exists()++ with open(file_path, "r", encoding="utf-8") as f:+ file_content = f.read()++ assert file_content == content+++def test_write_to_file_csv_list(file_write_toolkit):+ r"""Test writing CSV content as a list of lists to a file."""+ content = [+ ["name", "age", "city"],+ ["John", 30, "New York"],+ ["Jane", 25, "San Francisco"],+ ]+ filename = "test_list.csv"++ result = file_write_toolkit.write_to_file(content, filename)++ # Check the result message+ assert "successfully written" in result++ # Check the file exists+ file_path = file_write_toolkit._resolve_filepath(filename)+ assert file_path.exists()++ # Read the CSV file and check content+ with open(file_path, "r", encoding="utf-8") as f:+ lines = f.readlines()++ assert lines[0].strip() == "name,age,city"+ assert lines[1].strip() == "John,30,New York"+ assert lines[2].strip() == "Jane,25,San Francisco"+++def test_write_to_file_html(file_write_toolkit):+ r"""Test writing HTML content to a file."""+ content = "<html><body><h1>Hello, world!</h1></body></html>"+ filename = "test.html"++ result = file_write_toolkit.write_to_file(content, filename)++ # Check the result message+ assert "successfully written" in result++ # Check the file exists and has correct content+ file_path = file_write_toolkit._resolve_filepath(filename)+ assert file_path.exists()++ with open(file_path, "r", encoding="utf-8") as f:+ file_content = f.read()++ assert file_content == content+++def test_write_to_file_no_extension(file_write_toolkit):+ r"""Test writing content to a file with no extension (should use default+ format).+ """+ content = "# Default format test"+ filename = "test_no_extension"++ result = file_write_toolkit.write_to_file(content, filename)++ # Check the result message+ assert "successfully written" in result++ # Check the file exists with default extension (.md) and has correct+ # content+ file_path = file_write_toolkit._resolve_filepath(filename + ".md")+ assert file_path.exists()++ with open(file_path, "r", encoding="utf-8") as f:+ file_content = f.read()++ assert file_content == content+++def test_write_to_file_custom_encoding(file_write_toolkit):+ r"""Test writing content with a custom encoding."""+ content = "Hello, world with custom encoding!"+ filename = "test_encoding.txt"+ encoding = "latin-1"++ result = file_write_toolkit.write_to_file(+ content, filename, encoding=encoding+ )++ # Check the result message+ assert "successfully written" in result++ # Check the file exists and has correct content when read with the same+ # encoding+ file_path = file_write_toolkit._resolve_filepath(filename)+ assert file_path.exists()++ with open(file_path, "r", encoding=encoding) as f:+ file_content = f.read()++ assert file_content == content+++def test_write_to_file_nested_directory(file_write_toolkit):+ r"""Test writing to a file in a nested directory that doesn't exist yet."""+ content = "Content in nested directory"+ filename = "nested/dir/test.txt"++ result = file_write_toolkit.write_to_file(content, filename)++ # Check the result message+ assert "successfully written" in result++ # Check the file exists and has correct content+ file_path = file_write_toolkit._resolve_filepath(filename)+ assert file_path.exists()++ with open(file_path, "r", encoding="utf-8") as f:+ file_content = f.read()++ assert file_content == content+++def test_write_to_file_absolute_path(temp_dir):+ r"""Test writing to a file using an absolute path."""+ toolkit = FileWriteToolkit(output_dir="./default")+ content = "Content with absolute path"+ filename = os.path.join(temp_dir, "absolute_path.txt")++ result = toolkit.write_to_file(content, filename)++ # Check the result message+ assert "successfully written" in result++ # Check the file exists and has correct content+ assert os.path.exists(filename)++ with open(filename, "r", encoding="utf-8") as f:+ file_content = f.read()++ assert file_content == content+++def test_get_tools(file_write_toolkit):+ r"""Test that get_tools returns the correct function tools."""+ tools = file_write_toolkit.get_tools()++ # Check that we have the expected number of tools+ assert len(tools) == 1++ # Check that the tool has the correct function name+ assert tools[0].get_function_name() == "write_to_file"diff --git a/uv.lock b/uv.lockindex 0518b8dd4..3fc842da8 100644--- a/uv.lock+++ b/uv.lock@@ -215,7 +215,7 @@ wheels = [[[package]]name = "anthropic"-version = "0.42.0"+version = "0.49.0"source = { registry = "https://pypi.org/simple" }dependencies = [{ name = "anyio" },@@ -226,9 +226,9 @@ dependencies = [{ name = "sniffio" },{ name = "typing-extensions" },]-sdist = { url = "https://files.pythonhosted.org/packages/e7/7c/91b79f5ae4a52497a4e330d66ea5929aec2878ee2c9f8a998dbe4f4c7f01/anthropic-0.42.0.tar.gz", hash = "sha256:bf8b0ed8c8cb2c2118038f29c58099d2f99f7847296cafdaa853910bfff4edf4", size = 192361 }+sdist = { url = "https://files.pythonhosted.org/packages/86/e3/a88c8494ce4d1a88252b9e053607e885f9b14d0a32273d47b727cbee4228/anthropic-0.49.0.tar.gz", hash = "sha256:c09e885b0f674b9119b4f296d8508907f6cff0009bc20d5cf6b35936c40b4398", size = 210016 }wheels = [- { url = "https://files.pythonhosted.org/packages/ba/33/b907a6d27dd0d8d3adb4edb5c9e9c85a189719ec6855051cce3814c8ef13/anthropic-0.42.0-py3-none-any.whl", hash = "sha256:46775f65b723c078a2ac9e9de44a46db5c6a4fabeacfd165e5ea78e6817f4eff", size = 203365 },+ { url = "https://files.pythonhosted.org/packages/76/74/5d90ad14d55fbe3f9c474fdcb6e34b4bed99e3be8efac98734a5ddce88c1/anthropic-0.49.0-py3-none-any.whl", hash = "sha256:bbc17ad4e7094988d2fa86b87753ded8dce12498f4b85fe5810f208f454a8375", size = 243368 },][[package]]@@ -548,6 +548,7 @@ all = [{ name = "diffusers" },{ name = "discord-py" },{ name = "docker" },+ { name = "docx" },{ name = "docx2txt" },{ name = "duckduckgo-search" },{ name = "e2b-code-interpreter" },@@ -555,6 +556,7 @@ all = [{ name = "ffmpeg-python" },{ name = "firecrawl-py" },{ name = "fish-audio-sdk" },+ { name = "fpdf" },{ name = "google-cloud-storage" },{ name = "googlemaps" },{ name = "gradio" },@@ -685,7 +687,9 @@ docs = []document-tools = [{ name = "beautifulsoup4" },+ { name = "docx" },{ name = "docx2txt" },+ { name = "fpdf" },{ name = "openapi-spec-validator" },{ name = "openpyxl" },{ name = "pandasai" },@@ -779,8 +783,8 @@ requires-dist = [{ name = "agentops", marker = "extra == 'dev-tools'", specifier = ">=0.3.21,<0.4" },{ name = "aiosqlite", marker = "extra == 'all'", specifier = ">=0.20.0,<0.21" },{ name = "aiosqlite", marker = "extra == 'data-tools'", specifier = ">=0.20.0,<0.21" },- { name = "anthropic", marker = "extra == 'all'", specifier = ">=0.42.0,<0.43" },- { name = "anthropic", marker = "extra == 'model-platforms'", specifier = ">=0.42.0,<0.43" },+ { name = "anthropic", marker = "extra == 'all'", specifier = ">=0.47.0,<0.50.0" },+ { name = "anthropic", marker = "extra == 'model-platforms'", specifier = ">=0.47.0,<0.50.0" },{ name = "apify-client", marker = "extra == 'all'", specifier = ">=1.8.1,<2" },{ name = "apify-client", marker = "extra == 'web-tools'", specifier = ">=1.8.1,<2" },{ name = "arxiv", marker = "extra == 'all'", specifier = ">=2.1.3,<3" },@@ -814,6 +818,8 @@ requires-dist = [{ name = "docker", marker = "extra == 'dev-tools'", specifier = ">=7.1.0,<8" },{ name = "docstring-parser", specifier = ">=0.15,<0.16" },{ name = "docutils", marker = "extra == 'docs'", specifier = "<0.20.0" },+ { name = "docx", marker = "extra == 'all'", specifier = ">=0.2.4" },+ { name = "docx", marker = "extra == 'document-tools'", specifier = ">=0.2.4" },{ name = "docx2txt", marker = "extra == 'all'", specifier = ">=0.8,<0.9" },{ name = "docx2txt", marker = "extra == 'document-tools'", specifier = ">=0.8,<0.9" },{ name = "duckduckgo-search", marker = "extra == 'all'", specifier = ">=6.3.5,<7" },@@ -829,6 +835,8 @@ requires-dist = [{ name = "firecrawl-py", marker = "extra == 'web-tools'", specifier = ">=1.0.0,<2" },{ name = "fish-audio-sdk", marker = "extra == 'all'", specifier = ">=2024.12.5,<2025" },{ name = "fish-audio-sdk", marker = "extra == 'model-platforms'", specifier = ">=2024.12.5,<2025" },+ { name = "fpdf", marker = "extra == 'all'", specifier = ">=1.7.2" },+ { name = "fpdf", marker = "extra == 'document-tools'", specifier = ">=1.7.2" },{ name = "google-cloud-storage", marker = "extra == 'all'", specifier = ">=2.18.0,<3" },{ name = "google-cloud-storage", marker = "extra == 'storage'", specifier = ">=2.18.0,<3" },{ name = "googlemaps", marker = "extra == 'all'", specifier = ">=4.10.0,<5" },@@ -1601,6 +1609,16 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/93/69/e391bd51bc08ed9141ecd899a0ddb61ab6465309f1eb470905c0c8868081/docutils-0.19-py3-none-any.whl", hash = "sha256:5e1de4d849fee02c63b040a4a3fd567f4ab104defd8a5511fbbc24a8a017efbc", size = 570472 },]+[[package]]+name = "docx"+version = "0.2.4"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "lxml" },+ { name = "pillow" },+]+sdist = { url = "https://files.pythonhosted.org/packages/4a/8e/5a01644697b03016de339ef444cfff28367f92984dc74eddaab1ed60eada/docx-0.2.4.tar.gz", hash = "sha256:9d7595eac6e86cda0b7136a2995318d039c1f3eaa368a3300805abbbe5dc8877", size = 54925 }+[[package]]name = "docx2txt"version = "0.8"@@ -1924,6 +1942,12 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/bf/ff/44934a031ce5a39125415eb405b9efb76fe7f9586b75291d66ae5cbfc4e6/fonttools-4.56.0-py3-none-any.whl", hash = "sha256:1088182f68c303b50ca4dc0c82d42083d176cba37af1937e1a976a31149d4d14", size = 1089800 },]+[[package]]+name = "fpdf"+version = "1.7.2"+source = { registry = "https://pypi.org/simple" }+sdist = { url = "https://files.pythonhosted.org/packages/37/c6/608a9e6c172bf9124aa687ec8b9f0e8e5d697d59a5f4fad0e2d5ec2a7556/fpdf-1.7.2.tar.gz", hash = "sha256:125840783289e7d12552b1e86ab692c37322e7a65b96a99e0ea86cca041b6779", size = 39504 }+[[package]]name = "free-proxy"version = "1.1.3"