Create read_file_tool.py
Browse files- tools/read_file_tool.py +25 -0
tools/read_file_tool.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class ReadFileTool(Tool):
|
| 5 |
+
"""
|
| 6 |
+
Tool to read a file and return its content.
|
| 7 |
+
Args:
|
| 8 |
+
file_path (str): Path to the file to read.
|
| 9 |
+
Returns:
|
| 10 |
+
str: Content of the file or error message.
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
name = "read_file"
|
| 14 |
+
description = "Reads a file and returns its content"
|
| 15 |
+
inputs = {
|
| 16 |
+
"file_path": {"type": "string", "description": "Path to the file to read"},
|
| 17 |
+
}
|
| 18 |
+
output_type = "string"
|
| 19 |
+
|
| 20 |
+
def forward(self, file_path: str) -> str:
|
| 21 |
+
try:
|
| 22 |
+
with open(file_path, "r") as file:
|
| 23 |
+
return file.read()
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"Error reading file: {str(e)}"
|