From 96429040de61a9bb65692a660c8d6a82110ab371 Mon Sep 17 00:00:00 2001 From: Adan Butto Date: Sun, 20 Oct 2024 18:54:01 +0300 Subject: [PATCH 1/3] Fixed File Writer tool errors for better interaction with the agents --- .../file_writer_tool/file_writer_tool.py | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) 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 index ce0c4ebd9..428502d46 100644 --- a/src/crewai_tools/tools/file_writer_tool/file_writer_tool.py +++ b/src/crewai_tools/tools/file_writer_tool/file_writer_tool.py @@ -1,19 +1,16 @@ import os -from typing import Any, Optional, Type - +from typing import Any, Type from pydantic import BaseModel - -from ..base_tool import BaseTool - +from crewai_tools import BaseTool +from distutils.util import strtobool class FileWriterToolInput(BaseModel): filename: str content: str - directory: Optional[str] = None - overwrite: bool = False - - -class FileWriterTool(BaseTool): + directory: str = "./" + overwrite: str = "False" + +class FileWriterTool2(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 and overwrite flag as input." @@ -23,11 +20,14 @@ class FileWriterTool(BaseTool): def _run(self, **kwargs: Any) -> str: try: # Create the directory if it doesn't exist - if kwargs["directory"] and not os.path.exists(kwargs["directory"]): + if kwargs.get("directory") and not os.path.exists(kwargs["directory"]): os.makedirs(kwargs["directory"]) # Construct the full path - filepath = os.path.join(kwargs["directory"] or "", kwargs["filename"]) + filepath = os.path.join(kwargs.get("directory") or "", kwargs["filename"]) + + # Convert overwrite to boolean + kwargs["overwrite"] = bool(strtobool(kwargs["overwrite"])) # Check if file exists and overwrite is not allowed if os.path.exists(filepath) and not kwargs["overwrite"]: @@ -42,5 +42,7 @@ class FileWriterTool(BaseTool): return ( f"File {filepath} already exists and overwrite option was not passed." ) + except KeyError as e: + return f"An error occurred while accessing key: {str(e)}" except Exception as e: - return f"An error occurred while writing to the file: {str(e)}" + return f"An error occurred while writing to the file: {str(e)}" \ No newline at end of file From 857d6c135c2b7786211434140c3d66a3ef4b7ada Mon Sep 17 00:00:00 2001 From: Adan Butto Date: Sun, 20 Oct 2024 20:37:59 +0300 Subject: [PATCH 2/3] reverted directory variable back to optional --- .../tools/file_writer_tool/file_writer_tool.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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 index 428502d46..b3ca13c55 100644 --- a/src/crewai_tools/tools/file_writer_tool/file_writer_tool.py +++ b/src/crewai_tools/tools/file_writer_tool/file_writer_tool.py @@ -1,16 +1,17 @@ import os -from typing import Any, Type +from typing import Any, Optional, Type from pydantic import BaseModel -from crewai_tools import BaseTool +from ..base_tool import BaseTool from distutils.util import strtobool + class FileWriterToolInput(BaseModel): filename: str content: str - directory: str = "./" + directory: Optional[str] = "./" overwrite: str = "False" -class FileWriterTool2(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 and overwrite flag as input." From 6a7e917e1d9bd766945b390a07d1ccd9927a4c29 Mon Sep 17 00:00:00 2001 From: Adan Butto Date: Tue, 22 Oct 2024 18:47:52 +0300 Subject: [PATCH 3/3] Changed order of the arguments, placing 'content' last. It tends to forget context when it gets to filling other arguments when content is on the longer side. --- src/crewai_tools/tools/file_writer_tool/file_writer_tool.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 index b3ca13c55..a008e4a75 100644 --- a/src/crewai_tools/tools/file_writer_tool/file_writer_tool.py +++ b/src/crewai_tools/tools/file_writer_tool/file_writer_tool.py @@ -6,10 +6,10 @@ from distutils.util import strtobool class FileWriterToolInput(BaseModel): - filename: str - content: str + filename: str directory: Optional[str] = "./" overwrite: str = "False" + content: str class FileWriterTool(BaseTool): name: str = "File Writer Tool"