mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-09 08:08:32 +00:00
Merge pull request #19 from Tavernari/main
Improve File Reading Robustness in FileReadTool
This commit is contained in:
@@ -2,13 +2,19 @@ from typing import Optional, Type, Any
|
||||
from pydantic.v1 import BaseModel, Field
|
||||
from ..base_tool import BaseTool
|
||||
|
||||
|
||||
class FixedFileReadToolSchema(BaseModel):
|
||||
"""Input for FileReadTool."""
|
||||
pass
|
||||
|
||||
|
||||
class FileReadToolSchema(FixedFileReadToolSchema):
|
||||
"""Input for FileReadTool."""
|
||||
file_path: str = Field(..., description="Mandatory file full path to read the file")
|
||||
file_path: str = Field(
|
||||
...,
|
||||
description="Mandatory file full path to read the file"
|
||||
)
|
||||
|
||||
|
||||
class FileReadTool(BaseTool):
|
||||
name: str = "Read a file's content"
|
||||
@@ -16,7 +22,11 @@ class FileReadTool(BaseTool):
|
||||
args_schema: Type[BaseModel] = FileReadToolSchema
|
||||
file_path: Optional[str] = None
|
||||
|
||||
def __init__(self, file_path: Optional[str] = None, **kwargs):
|
||||
def __init__(
|
||||
self,
|
||||
file_path: Optional[str] = None,
|
||||
**kwargs
|
||||
):
|
||||
super().__init__(**kwargs)
|
||||
if file_path is not None:
|
||||
self.file_path = file_path
|
||||
@@ -28,6 +38,9 @@ class FileReadTool(BaseTool):
|
||||
self,
|
||||
**kwargs: Any,
|
||||
) -> Any:
|
||||
try:
|
||||
file_path = kwargs.get('file_path', self.file_path)
|
||||
with open(file_path, 'r') as file:
|
||||
return file.read()
|
||||
except Exception as e:
|
||||
return f"Fail to read the file {file_path}. Error: {e}"
|
||||
|
||||
Reference in New Issue
Block a user