File size: 751 Bytes
ad8f765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from smolagents import Tool

class FileReaderTool(Tool):
    name = "file_reader"
    description = "Read the content of a text or python code file."
    inputs = {
        "file_path": {
            "type": "string",
            "description": "The path of the file to read."
        }
    }
    output_type = "string"

    def forward(self, file_path: str) -> str:
        try:
            with open(file_path, "r", encoding="utf-8") as file:
                content = file.read()
            return f"File content:\n{content}"
        except FileNotFoundError:
            return f"Error file '{file_path}' not found."
        except Exception as e:
            return f"Error file_reader is not working properly, error: {e}, please skip this tool"