SGK86 commited on
Commit
1e165d6
·
verified ·
1 Parent(s): 1d70864

Create markdown_writer.py

Browse files
Files changed (1) hide show
  1. tools/markdown_writer.py +33 -0
tools/markdown_writer.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datetime
2
+ import os
3
+ from typing import Any, Dict, Union
4
+
5
+ from smolagents.tools import Tool
6
+
7
+
8
+ class 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 directory
23
+
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 = False
33
+