From 1f08d74015877278cd48dd88aeb73a8037f1b8a3 Mon Sep 17 00:00:00 2001 From: Carlos Antunes Date: Mon, 20 May 2024 22:00:34 -0300 Subject: [PATCH 1/2] adding file writer tool and documentation --- .../tools/file_writer_tool/README.md | 35 +++++++++++++++++++ .../file_writer_tool/file_writer_tool.py | 22 ++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/crewai_tools/tools/file_writer_tool/README.md create mode 100644 src/crewai_tools/tools/file_writer_tool/file_writer_tool.py diff --git a/src/crewai_tools/tools/file_writer_tool/README.md b/src/crewai_tools/tools/file_writer_tool/README.md new file mode 100644 index 000000000..e93e5c682 --- /dev/null +++ b/src/crewai_tools/tools/file_writer_tool/README.md @@ -0,0 +1,35 @@ +Here's the rewritten README for the `FileWriterTool`: + +# FileWriterTool Documentation + +## Description +The `FileWriterTool` is a component of the crewai_tools package, designed to simplify the process of writing content to files. It is particularly useful in scenarios such as generating reports, saving logs, creating configuration files, and more. This tool supports creating new directories if they don't exist, making it easier to organize your output. + +## Installation +Install the crewai_tools package to use the `FileWriterTool` in your projects: + +```shell +pip install 'crewai[tools]' +``` + +## Example +To get started with the `FileWriterTool`: + +```python +from crewai_tools import FileWriterTool + +# Initialize the tool +file_writer_tool = FileWriterTool() + +# Write content to a file in a specified directory +result = file_writer_tool._run('example.txt', 'This is a test content.', 'test_directory') +print(result) +``` + +## Arguments +- `filename`: The name of the file you want to create or overwrite. +- `content`: The content to write into the file. +- `directory` (optional): The path to the directory where the file will be created. Defaults to the current directory (`.`). If the directory does not exist, it will be created. + +## Conclusion +By integrating the `FileWriterTool` into your crews, the agents can execute the process of writing content to files and creating directories. This tool is essential for tasks that require saving output data, creating structured file systems, and more. By adhering to the setup and usage guidelines provided, incorporating this tool into projects is straightforward and efficient. diff --git a/src/crewai_tools/tools/file_writer_tool/file_writer_tool.py b/src/crewai_tools/tools/file_writer_tool/file_writer_tool.py new file mode 100644 index 000000000..21db460cc --- /dev/null +++ b/src/crewai_tools/tools/file_writer_tool/file_writer_tool.py @@ -0,0 +1,22 @@ +import os +from crewai_tools import BaseTool + +class FileWriterTool(BaseTool): + name: str = "File Writer Tool" + description: str = "A tool to write content to a specified file. Accepts filename, content, and optionally a directory path as input." + + def _run(self, filename: str, content: str, directory: str = '.') -> str: + try: + # Create the directory if it doesn't exist + if directory and not os.path.exists(directory): + os.makedirs(directory) + + # Construct the full path + filepath = os.path.join(directory, filename) + + # Write content to the file + with open(filepath, 'w') as file: + file.write(content) + return f"Content successfully written to {filepath}" + except Exception as e: + return f"An error occurred while writing to the file: {str(e)}" From d5e6b95817755f10725f5059d4dba77918024dda Mon Sep 17 00:00:00 2001 From: Carlos Antunes Date: Mon, 15 Jul 2024 12:41:53 -0300 Subject: [PATCH 2/2] adding the appropriate imports on the __init__.py files --- src/crewai_tools/__init__.py | 3 ++- src/crewai_tools/tools/__init__.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/crewai_tools/__init__.py b/src/crewai_tools/__init__.py index faac5d37d..36d48b4a4 100644 --- a/src/crewai_tools/__init__.py +++ b/src/crewai_tools/__init__.py @@ -8,6 +8,7 @@ from .tools import ( DirectoryReadTool, EXASearchTool, FileReadTool, + FileWriterTool, GithubSearchTool, SerperDevTool, TXTSearchTool, @@ -23,4 +24,4 @@ from .tools import ( XMLSearchTool, YoutubeChannelSearchTool, YoutubeVideoSearchTool, -) \ No newline at end of file +) diff --git a/src/crewai_tools/tools/__init__.py b/src/crewai_tools/tools/__init__.py index 648671d97..e6dd234a5 100644 --- a/src/crewai_tools/tools/__init__.py +++ b/src/crewai_tools/tools/__init__.py @@ -6,6 +6,7 @@ from .directory_read_tool.directory_read_tool import DirectoryReadTool from .docx_search_tool.docx_search_tool import DOCXSearchTool from .exa_tools.exa_search_tool import EXASearchTool from .file_read_tool.file_read_tool import FileReadTool +from .file_writer_tool.file_writer_tool import FileWriterTool from .github_search_tool.github_search_tool import GithubSearchTool from .serper_dev_tool.serper_dev_tool import SerperDevTool from .txt_search_tool.txt_search_tool import TXTSearchTool