almaze-api/tools/file/write_to_file.py
2025-02-11 23:14:01 +05:30

17 lines
561 B
Python

import os
from pathlib import Path
from langchain_core.tools import tool
@tool
def write_to_file(filepath: str, content: str) -> str:
"""Write content to a file, creating directories if they don't exist."""
try:
path = Path(filepath)
path.parent.mkdir(parents=True, exist_ok=True)
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
return f"Successfully wrote content to {filepath}"
except Exception as e:
return f"Error writing to file: {str(e)}"