First_agent_Agent_Course / tools /markdown_writer.py
SGK86's picture
Create markdown_writer.py
1e165d6 verified
Raw History Blame Contribute Delete
1.32 kB
import datetime
import os
from typing import Any, Dict, Union
from smolagents.tools import Tool
class MarkdownWriterTool(Tool):
name = "markdown_writer"
description = """Writes the given text content, which should be a summary in Markdown format, to a Markdown file with a timestamped filename.
The assistant should provide a summary as text in Markdown format.
Args:
markdown_content: The text content in Markdown format to be written to the file.
"""
inputs = {"markdown_content": {"type": "string", "description": "The Markdown content to write to the file."}}
output_type = "string"
def forward(self, markdown_content: str) -> str:
"""Writes the given Markdown content to a file with a timestamped name."""
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"summary_{timestamp}.md"
filepath = os.path.join(os.getcwd(), filename) # Save in the current working directory
try:
with open(filepath, "w", encoding="utf-8") as f:
f.write(markdown_content)
return f"Successfully wrote content to {filepath}"
except Exception as e:
return f"Error writing to file: {str(e)}"
def __init__(self, *args, **kwargs):
self.is_initialized = False