write.py
1 """ 2 Write imports 3 """ 4 5 from smolagents import Tool 6 7 8 class WriteTool(Tool): 9 """ 10 The WriteTool writes file content. 11 """ 12 13 # pylint: disable=W0231 14 def __init__(self): 15 """ 16 Creates a WriteTool. 17 """ 18 19 # Tool parameters 20 self.name = "write" 21 self.description = "Implementation of a file write tool. Writes content to file." 22 self.inputs = { 23 "path": {"type": "string", "description": "Output file path"}, 24 "content": {"type": "string", "description": "File content to write"}, 25 } 26 self.output_type = "any" 27 28 # Validate parameters and initialize tool 29 super().__init__() 30 31 # pylint: disable=W0221 32 def forward(self, path, content): 33 """ 34 Writes content to path. 35 36 Args: 37 path: output file path 38 content: content to write 39 """ 40 41 with open(path, "w", encoding="utf-8") as f: 42 f.write(content)