CoolFace
Apppublic

SGK86/First_agent_Agent_Course

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
markdown_writer.py34 linesDownload Raw Back to tools
1import datetime2import os3from typing import Any, Dict, Union4 5from smolagents.tools import Tool6 7 8class MarkdownWriterTool(Tool):9    name = "markdown_writer"10    description = """Writes the given text content, which should be a summary in Markdown format, to a Markdown file with a timestamped filename.11    The assistant should provide a summary as text in Markdown format.12    Args:13        markdown_content: The text content in Markdown format to be written to the file.14    """15    inputs = {"markdown_content": {"type": "string", "description": "The Markdown content to write to the file."}}16    output_type = "string"17 18    def forward(self, markdown_content: str) -> str:19        """Writes the given Markdown content to a file with a timestamped name."""20        timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")21        filename = f"summary_{timestamp}.md"22        filepath = os.path.join(os.getcwd(), filename)  # Save in the current working directory23 24        try:25            with open(filepath, "w", encoding="utf-8") as f:26                f.write(markdown_content)27            return f"Successfully wrote content to {filepath}"28        except Exception as e:29            return f"Error writing to file: {str(e)}"30 31    def __init__(self, *args, **kwargs):32        self.is_initialized = False33 34